查看目录
wordpress建站时我们经常会用到置顶功能,不过一般wordpress自带的置顶功能只能显示在首页,如果想要实现分类显示置顶文章,那就需要我们自己动手做一些优化了。一般来说,wordpress分类置顶功能可以通过三种方式来实现。
1、主题自带分类置顶:
目前有一些主题开发者已经意识到了建站用户有分类文章置顶的需求,所以在自己开发的wordpress主题中内置了分类置顶功能。这样,使用这些主题模板的用户就可以轻松惬意的实现自己网站分类文章置顶。但是这种主题并不多见,而且往往价格不菲,适用于“土豪”用户。
2、插件实现分类置顶:
如果我们不想破费太多去更换主题的话,可以考虑使用插件实现wordpress分类置顶的需求,目前楚狂人所知道的可以实现文章分类置顶的插件有“WP-Sticky ”、“Category Sticky Pos”、“Sticky Posts – Switch”。
具体的安装和使用方式我们在前面的一篇文章中已经分享过,有兴趣的朋友可以移步观看:
wordpress分类文章置顶使用插件和代码哪一种方式好?
发布时间: 阅读(250)
3、纯代码实现分类置顶:
大家可以直接把下面的代码添加到当前wordpress建站主题的functions.php文件中保存,这样网站就会自动在当前分类显示本分类的置顶文章。【代码来自露兜博客】露兜是很早就分享wordpress建站技巧的一位博客作者,以前我曾经从他那里受益匪浅,虽然他已经很久不怎么更新wordpress方面的文章了,但是这些以前分享的内容依然在帮助着那些个人站长。
add_filter('the_posts', 'putStickyOnTop' ); function putStickyOnTop( $posts ) { if(is_home() || !is_main_query() || !is_archive()) return $posts; global $wp_query; // 获取所有置顶文章 $sticky_posts = get_option('sticky_posts'); if ( $wp_query->query_vars['paged'] <= 1 && !empty($sticky_posts) && is_array($sticky_posts) && !get_query_var('ignore_sticky_posts') ) { $stickies1 = get_posts( array( 'post__in' => $sticky_posts ) ); foreach ( $stickies1 as $sticky_post1 ) { // 判断当前是否分类页 if($wp_query->is_category == 1 && !has_category($wp_query->query_vars['cat'], $sticky_post1->ID)) { // 去除不属于本分类的置顶文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_tag == 1 && !has_tag($wp_query->query_vars['tag'], $sticky_post1->ID)) { // 去除不属于本标签的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_year == 1 && date_i18n('Y', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) { // 去除不属于本年份的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_month == 1 && date_i18n('Ym', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) { // 去除不属于本月份的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_day == 1 && date_i18n('Ymd', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) { // 去除不属于本日期的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } if($wp_query->is_author == 1 && $sticky_post1->post_author != $wp_query->query_vars['author']) { // 去除不属于本作者的文章 $offset1 = array_search($sticky_post1->ID, $sticky_posts); unset( $sticky_posts[$offset1] ); } } $num_posts = count($posts); $sticky_offset = 0; // Loop over posts and relocate stickies to the front. for ( $i = 0; $i < $num_posts; $i++ ) { if ( in_array($posts[$i]->ID, $sticky_posts) ) { $sticky_post = $posts[$i]; // Remove sticky from current position array_splice($posts, $i, 1); // Move to front, after other stickies array_splice($posts, $sticky_offset, 0, array($sticky_post)); // Increment the sticky offset. The next sticky will be placed at this offset. $sticky_offset++; // Remove post from sticky posts array $offset = array_search($sticky_post->ID, $sticky_posts); unset( $sticky_posts[$offset] ); } } // If any posts have been excluded specifically, Ignore those that are sticky. if ( !empty($sticky_posts) && !empty($wp_query->query_vars['post__not_in'] ) ) $sticky_posts = array_diff($sticky_posts, $wp_query->query_vars['post__not_in']); // Fetch sticky posts that weren't in the query results if ( !empty($sticky_posts) ) { $stickies = get_posts( array( 'post__in' => $sticky_posts, 'post_type' => $wp_query->query_vars['post_type'], 'post_status' => 'publish', 'nopaging' => true ) ); foreach ( $stickies as $sticky_post ) { array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) ); $sticky_offset++; } } } return $posts; }
如果你是一位使用wp建站的朋友,那么看完这两篇文章,相信你已经找到了令自己满意的wordpress分类置顶方法,这样可以让我们的用户更多的阅读到我们希望展现的文章内容,提高用户的粘性和转化率,相信可以让网站做的更好,祝大家财源广进!