首页 论坛 wordpress置顶文章调用与设置
帖子详情

wordpress网站后台允许我们设置一些置顶文章,然后通过调用置顶文章的代码把它调用到自己的网站前台。

下面是特别常用的wordpress调用置顶文章代码,适用于所有模板使用。

<?php
$sticky = get_option(‘sticky_posts’);
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );
if (have_posts()) :while (have_posts()) : the_post();
?>

<li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>

<?php endwhile; endif; ?>

代码解释:

  • rsort( $sticky ); 对置顶文章数组逆向排序,即大ID在前;
  • $sticky = array_slice( $sticky, 0, 5);控制显示置顶文章的数量,输出置顶文章数,请修改5,0不要动,如果需要全部置顶文章输出,可以把这句注释掉;
  • ‘post__in’ => get_option(‘sticky_posts’)确定了该LOOP调用的是置顶文章列表。
  • ‘caller_get_posts’的作用是排除非指定性文章,即除了置顶文章之外,不显示其他的文章。

wordpress文章置顶方法

【后台】–【文章】–【编辑】—【将文章置顶到顶部】

wordpress调用置顶文章

补充:调用了置顶文章之后,其余的循环调用文章,就要排除置顶文章。排队置顶文章,使用下面的代码:

<?php $the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();?>
<li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php endif; ?>

相关教程:WP程序调用最新文章 热门文章 相关文章 随机文章代码

版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.52diyhome.com/forum/65827.html

发表评论
暂无评论
  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    资源总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

资源在于分享,创作来源想象