分享学习记录
互联网技术知识

WordPress调用同分类随机文章的方法

在适当的位置显示调用随机文章可以促进网站内链,增加文章阅读点击量,有利于SEO,网上大部分Wordpress调用随机文章代码都是基于全站文章,这里发一个调用同分类随机文章的代码。

将下面代码放到主题文章页面single模板或者边栏sidebar模板适当位置即可:

<ul>
    <?php
    $cat = get_the_category();
    foreach($cat as $key=>$category){
        $catid = $category->term_id;
    }
    $args = array(‘orderby’ => ‘rand’,’showposts’ => 8,’cat’ => $catid );
    $query_posts = new WP_Query();
    $query_posts->query($args);
    while ($query_posts->have_posts()) : $query_posts->the_post();
    ?>
    <li><a href=“<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>
</ul>
赞(0)
文章名称:《WordPress调用同分类随机文章的方法》
文章链接:https://www.bailuze.com/18436.html
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本站专注于百度、搜狗、360、谷歌、bing等常见搜索引擎的优化,关键词排名的提高,诚意咨询邮箱526009505@qq.com
分享到