1、首先我们在本地用网页编辑器打开需要调用置顶文章的页面,首页的话通常是index.php。
2、接着在需要添加置顶文章的位置添加相应的代码(列入边栏)代码如下:<?php // https://www.17naw.com置顶文章示范 $sticky = get_option('sticky_posts'); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 1); query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1,'showposts' => 1) ); ?><?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <li><a title="<?php the_title();?>" href="<?php the_permalink();?>">?php the_title();?></a></li> <?php endwhile; ?><?php else : ?><p class="center">没有数据。</p><?php endif; ?>
3、重要代码解释:$sticky = array_slice( $sticky, 0, 1);是表示需要调用的置顶文章的开始接受位置的0和1代码调用1篇,其中0表示从第一篇开始,1表示1篇。query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1,'showposts' => 1) );中也有两个数字,这里的1和1表示一篇如果在$sticky = array_slice( $sticky, 0, 1);中是0和2那么按照要求query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1,'showposts' => 1) );中的'showposts' => 1则需要填写成2才能调出两篇文章。
4、当代码添加好后我们还需要添加相应的div css代码才能显示出美观的效果,添加css一般写在style.css文件中。
5、做好以上步骤保存后,我们还需要在程序的后台将需要置顶的文章设置成置顶,这样在前台才能显示出置顶文章。