最近访客
百度未收录

WordPress分类归档页面调用本分类置顶文章

通常wordpress置顶文章只显示在首页,分类归档页面不显示置顶文章,内容丰富栏目较多的网站会对推荐的文章进行置顶显示,以便访客打开该分类时第一时间能看到管理员推荐的内容,如果分类列表显示网站所有的置顶文章,显然影响用户体验,因此只显示该分类的置顶推荐文章会更加友好。如想在分类归档页面调用本分类置顶文章,可以用本文的方法实现。

WordPress分类归档页面调用本分类置顶文章-狐狸库

将下面的代码添加到主题archive.php或者category.php模板主循环上面:

<?php
    query_posts(array(
        \"category__in\" => array(get_query_var(\"cat\")),
        \"post__in\" => get_option(\"sticky_posts\"),
        \'showposts\' => 3,
        )
    );
    while(have_posts()) : the_post();
?>
<h1>置顶<a href=\"<?php the_permalink(); ?>\" title=\"<?php the_title(); ?>\"><?php the_title(); ?></a></h1>
<?php
    endwhile;
    wp_reset_query();
?>

其中: ‘showposts’ => 3,  是显示数量。

在正常的文章列表中排除已置顶的文章:

<?php while(have_posts()) : the_post(); ?>
    <?php if(!is_sticky()){?>
    <h2><a href=\"<?php the_permalink(); ?>\" title=\"<?php the_title(); ?>\"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
<?php } endwhile;?>
温馨提示:本文最后更新于2022/10/20 04:35:32。若文章内容或图片失效,请留言联系站长反馈!
!
也想出现在这里? 联系我们
创意广告
© 版权声明
THE END
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容