怎么调用最新,随机,热门,指定分类文章? - wordpress开发

访客看完文章后,如果我们在wordpress的文章页底部或是侧边栏给访客提供这些内容,就能留下更多访客,降低跳出率

在开发wordpress主题的时候,我们需要考虑到的一点是,访客看完这篇文章后是不会直接就走掉的,而是会看看有没有类似的或是相关的文章,如果这个时候我们在文章页的底部或是侧边栏给访客提供这些内容,就能让访客在网站里待的更久,大大降低跳出率,这样对SEO也是很有好处的。

WP 文章调用方法,包括调用最新,指定分类,随机,热文等代码,经测试(Muze也测试了下),支持当前最新版的WordPress,这里我把代码记录下来,方便自己日后查看,也给大家参考之用。

调用最新文章

<?php query_posts('showposts=6&cat=-111'); ?>  <!--显示篇数和排除分类-->
<ul>  
<?php while (have_posts()) : the_post(); ?>  
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
<?php endwhile;?>  
</ul>

调用指定分类文章

<ul>
<?php
    $args=array(
        'cat' => 1,   // 分类ID
        'posts_per_page' => 10, // 显示篇数
    );
    query_posts($args);
    if(have_posts()) : while (have_posts()) : the_post();
?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
    </li>
<?php  endwhile; endif; wp_reset_query(); ?>
</ul>

调用整站随机文章

<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

调用同分类随机文章

<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>

调用整站热门文章(按评论数)

<ul>
<?php
$post_num = 10; // 显示篇数
$args = array(
'post_password' => "",
'post_status' => 'publish', // 只选公开的文章.
'post__not_in' => array($post->ID),//排除当前文章
'caller_get_posts' => 1, // 排除置顶文章.
'orderby' => 'comment_count', // 依评论数排序.
'posts_per_page' => $post_num
);
$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 } wp_reset_query();?>
</ul>

但以上函数好像只能获取到日志的标题和链接,如果能获取到图片就好了。

如果你还需要一些统计功能,那么还可以看看这篇wordpress开发文章:

怎么调用最新,随机,热门,指定分类文章? - wordpress开发-Npcink
怎么调用最新,随机,热门,指定分类文章? - wordpress开发-Npcink

统计WordPress分类目录的浏览量 - wordpress开发

如果只是想获取上下文的相关信息的话,可以看看这篇文章:

怎么调用最新,随机,热门,指定分类文章? - wordpress开发-Npcink
怎么调用最新,随机,热门,指定分类文章? - wordpress开发-Npcink

wordpress开发:获取上下篇日志的链接、标题、特色图像

实用的首页四格模块 - WordPress区块

2020-3-15 15:48:41

Typecho

Spzac个人资讯下载类主题 - Typecho主题

2020-5-27 8:29:21

⚠️
Npcink上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。
无意侵害您的权益,请发送邮件至 1355471563#qq.com 或点击右侧 私信:Muze 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索