这是一款子比主题的右上角随机访问文章功能,利用的子比自带的颜色和跳转图标,给自己的右上角加一个随机文章还是比较美观的,喜欢的自行部署吧!

 

代码部署:

[hidecontent type=”reply” desc=”隐藏内容:评论后查看”]

定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢里面即可:

function register_random_post_redirect_endpoint() {
    register_rest_route('wp/v2', '/random-post', array(
        'methods' => 'GET',
        'callback' => 'handle_random_post_redirect',
        'permission_callback' => '__return_true'
    ));
}
add_action('rest_api_init', 'register_random_post_redirect_endpoint');

function handle_random_post_redirect($request) {
    // 获取所有已发布的文章
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'fields' => 'ids',
        'posts_per_page' => -1,
    );
    
    $posts = get_posts($args);
    
    if (empty($posts)) {
        return new WP_Error('no_posts', '没有找到文章', array('status' => 404));
    }
    
    // 随机选择一篇文章
    $random_post_id = $posts[array_rand($posts)];
    $random_post_url = get_permalink($random_post_id);
    
    // 直接重定向
    if (!headers_sent()) {
        wp_redirect($random_post_url);
        exit;
    }
    
    // 如果无法重定向,返回JSON响应
    return array(
        'post_id' => $random_post_id,
        'post_url' => $random_post_url,
        'message' => '请手动访问上面的URL'
    );
}
function zib_get_random_post_button($args = array()) {
    if (!_pz('random_post_button', true)) {
        return '';
    }

    $random_icon = '<svg t="1751112388719" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5410" width="1000" height="1000" data-spm-anchor-id="a313x.search_index.0.i12.4dfa3a81Mc8lKy"><path d="M166.464 448.704l212.64 128.736a32 32 0 1 1-33.152 54.72L79.424 470.848a32 32 0 0 1 4.256-56.928l832-347.456a32 32 0 0 1 43.744 35.52l-145.76 768a32 32 0 0 1-48.576 21.056L494.88 719.712a32 32 0 0 1 34.24-54.08l230.464 146.112L885.44 148.48 166.464 448.704z" p-id="5411" data-spm-anchor-id="a313x.search_index.0.i14.4dfa3a81Mc8lKy"></path><path d="M416 632.096V896a32 32 0 0 1-64 0V617.376a32 32 0 0 1 11.2-24.32L752.192 260a32 32 0 0 1 41.6 48.64L416 632.064z" p-id="5412" data-spm-anchor-id="a313x.search_index.0.i13.4dfa3a81Mc8lKy"></path></svg>';

    $defaults = array(
        'class' => 'jb-blue radius-circle', 
        'text' => '随机文章',
        'icon' => $random_icon,
        'tooltip' => '随机阅读一篇文章',
        'echo' => true
    );
    
    $args = wp_parse_args($args, $defaults);
    
    $button = '<a href="'%20.%20esc_url(home_url('/wp-json/wp/v2/random-post'))%20.%20'" rel="external nofollow"  
          class="random-post-button but ' . esc_attr($args['class']) . ' ml10" 
          data-toggle="tooltip" 
          data-placement="bottom" 
          title="' . esc_attr($args['tooltip']) . '">'
          . $args['icon']
          . esc_html($args['text'])
          . '</a>';

    if ($args['echo']) {
        echo $button;
    }
    
    return $button;
}

定位:/wp-content/themes/zibll/inc/functions/zib-header.php文件,我们到了这个文件之后搜代码

function zib_menu_button($layout = 1)

将下面的代码放到里面,如下图我圈的地方即可!

$random_button = zib_get_random_post_button(array(
        'echo' => false 
    ));
    if ($random_button) {
        $button .= $random_button;
    }

可以看到我们func代码里面有一行'class' => 'jb-blue radius-circle',里面的jb-blue,我们可以按照下面来自己弄自己喜欢的颜色即可!

class 样式 class 样式 class 样式
c-red 红色文字 b-theme 主题背景色 jb-red 渐变红色背景
c-yellow 橙色文字 b-red 红色背景 jb-yellow 渐变橙色背景
c-blue 蓝色文字 b-yellow 橙色背景 jb-blue 渐变蓝色背景
c-blue-2 深蓝色文字 b-blue 蓝色背景 jb-green 渐变绿色背景
c-green 绿色文字 b-green 深蓝色背景 jb-purple 渐变紫色背景
c-purple 紫色文字 b-purple 紫色背景 jb-vip1 渐变金色背景
jb-vip2 渐变黑色背景

[/hidecontent]

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。