当前位置: 首页 > 阿里云优惠 > 阿里云服务器优惠>> WordPress分类归档页面调用本分类置顶文章 – WordPress 教程

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

通常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="https://xyy1.com/<?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="https://xyy1.com/<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>    <?php the_excerpt(); ?><?php } endwhile;?>

服务器购买/咨询热线:1563700917115617636856

本文链接:http://www.sqxnmj.com/alfwq/28236.html
tags:
腾讯云服务器特惠

  • 关注微信
是否收录:

猜你喜欢

微信公众号