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

给TA打赏
共{{data.count}}人
人已打赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索