网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   WordPress (https://www.0058.net/forumdisplay.php?f=68)
-   -   wordpress无插件和使用插件文章置顶的实现方法 (https://www.0058.net/showthread.php?t=5184)

Jaylyn 2015-06-14 06:33 AM

wordpress无插件和使用插件文章置顶的实现方法
 
使用wordpress 3.0.1时,WP本身是带有置顶文章功能,可是好像只有默认主题能看出变化,如果自定义主题就看不出来,当然也可以自己在主题中提取某分类的最新置顶文章。
代码如下:
<?php query_posts(array('cat'=>73,'posts_per_page' => 5,'post__in' => get_option('sticky_posts'),'caller_get_posts' => 1));?>
<?php if(have_posts()):while(have_posts()):the_post(); ?>
<li><span class="grayz">·</span><a href="<?php the_permalink(); ?>" target="_blank" class="black "><?php echo cut_str($post->post_title,36); ?></a></li>
<?php endwhile;?>
<?php else:?>
<?php endif;wp_reset_query();?>

置顶文章功能引入于WordPress 2.7。在查询中,被设为“置顶”的文章会显示在其它文章之前,除非该文章已经被caller_get_posts=1参数排除。
array('post__in'=>get_option('sticky_posts')) —— 返回所有置顶文章的数组
caller_get_posts=1 —— 排除返回的文章上方的置顶文章,但在返回文章列表时,以自然顺序将曾经置顶的文章安插在列表中。

返回第一篇置顶文章

$sticky=get_option('sticky_posts') ;
query_posts('p=' . $sticky[0]);

$args = array(
'posts_per_page' => 1,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1
);
query_posts($args);

返回第一篇置顶文章;若无,则不返回任何内容

$sticky = get_option('sticky_posts');
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'caller_get_posts' => 1
);
query_posts($args);
if($sticky[0]) {
// insert here your stuff...
}

从查询中排除所有置顶文章
query_posts(array("post__not_in" =>get_option("sticky_posts")));

返回某一分类下所有文章,但不在文章列表上方显示置顶文章。所有设为“置顶”的文章以正常顺序(如日期顺序)显示
query_posts('caller_get_posts=1&posts_per_page=3&cat=6');

返回某一分类下所有文章,完全不显示置顶文章,保留分页
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
'cat'=>3,
'caller_get_posts'=>1,
'post__not_in' => $sticky,
'paged'=>$paged,
);
query_posts($args);

可是我做的是一个门户网站,希望如果有置顶文章就显示置顶文章,如果没有的就显示最新文章,如果有一部分置顶文章,就让置顶文章显示在最新文章前面,可是弄了大半天也没有能实现这功能,谷歌也没有找到。最后只能用WP-sticky插件了。将wp-sticky这个文章置顶插件上传到根目录下的wp-content/plugins目录,然后去后台激活。激活后,需要对这个插件进行相关的设置,你可以设置文章置顶的方式、设置显示的日期,你可以根据自己的需求进行适当的设置,设置完后当你发布新文章或更改文章时,就会在页面的右下方出现一个“Post Sticky Status”的单选框,有三人选项:
Announcement:如果想长期置顶,就选这个选项
Sticky:如果只是当天置顶,就选这个选项
Normal:不置顶,如果想取消置顶,就选这个选项,默认这个选项
用了这个wp-sticky置顶插件,调整了提取分类文章的展示顺序。同时会在标题前加上Announcement:或Sticky:


All times are GMT +8. The time now is 04:10 AM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.