奸臣胡 發表於 2023-11-22 00:00:00

WordPress分类与标签等存档页实现置顶的方法

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
本文实例讲述了WordPress分类与标签等存档页实现置顶的方法。分享给大家供大家参考。具体分析如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
在wordpress中默认能置顶文章就是只有首页了,如果我们希望分类/标签等存档页也能置顶文章我们需要二次开发.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
现在参考wp-includes/query.php中首页置顶的代码,稍微修改一下,可以让分类页、标签页、作者页和日期页等存档页面也能像首页一样在顶部显示其范围内的置顶文章,把下面的代码放到当前主题下的functions.php中就可以了.<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode6" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
add_filter('the_posts', 'putStickyOnTop' ); <br>
function putStickyOnTop( $posts ) { <br>
if(is_home() || !is_main_query() || !is_archive()) <br>
return $posts; <br><br>
global $wp_query; <br>
$sticky_posts = get_option('sticky_posts'); <br><br>
if ( $wp_query-&gt;query_vars['paged'] &lt;= 1 &amp;&amp; is_array($sticky_posts) &amp;&amp; !emptyempty($sticky_posts) &amp;&amp; !get_query_var('ignore_sticky_posts') ) { $stickies1 = get_posts( array( 'post__in' =&gt; $sticky_posts ) ); <br>
foreach ( $stickies1 as $sticky_post1 ) { <br>
// 判断当前是否分类页 <br>
if($wp_query-&gt;is_category == 1 &amp;&amp; !has_category($wp_query-&gt;query_vars['cat'], $sticky_post1-&gt;ID)) { <br>
// 去除不属于本分类的文章 <br>
$offset1 = array_search($sticky_post1-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset1] ); <br>
} <br>
if($wp_query-&gt;is_tag == 1 &amp;&amp; has_tag($wp_query-&gt;query_vars['tag'], $sticky_post1-&gt;ID)) { <br>
// 去除不属于本标签的文章 <br>
$offset1 = array_search($sticky_post1-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset1] ); <br>
} <br>
if($wp_query-&gt;is_year == 1 &amp;&amp; date_i18n('Y', strtotime($sticky_post1-&gt;post_date))!=$wp_query-&gt;query['m']) { <br>
// 去除不属于本年份的文章 <br>
$offset1 = array_search($sticky_post1-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset1] ); <br>
} <br>
if($wp_query-&gt;is_month == 1 &amp;&amp; date_i18n('Ym', strtotime($sticky_post1-&gt;post_date))!=$wp_query-&gt;query['m']) { <br>
// 去除不属于本月份的文章 <br>
$offset1 = array_search($sticky_post1-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset1] ); <br>
} <br>
if($wp_query-&gt;is_day == 1 &amp;&amp; date_i18n('Ymd', strtotime($sticky_post1-&gt;post_date))!=$wp_query-&gt;query['m']) { <br>
// 去除不属于本日期的文章 <br>
$offset1 = array_search($sticky_post1-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset1] ); <br>
} <br>
if($wp_query-&gt;is_author == 1 &amp;&amp; $sticky_post1-&gt;post_author != $wp_query-&gt;query_vars['author']) { <br>
// 去除不属于本作者的文章 <br>
$offset1 = array_search($sticky_post1-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset1] ); <br>
} <br>
} <br><br>
$num_posts = count($posts); <br>
$sticky_offset = 0; <br>
// Loop over posts and relocate stickies to the front. <br>
for ( $i = 0; $i &lt; $num_posts; $i++ ) { <br>
if ( in_array($posts[$i]-&gt;ID, $sticky_posts) ) { <br>
$sticky_post = $posts[$i]; <br>
// Remove sticky from current position <br>
array_splice($posts, $i, 1); <br>
// Move to front, after other stickies <br>
array_splice($posts, $sticky_offset, 0, array($sticky_post)); <br>
// Increment the sticky offset. The next sticky will be placed at this offset. <br>
$sticky_offset++; <br>
// Remove post from sticky posts array <br>
$offset = array_search($sticky_post-&gt;ID, $sticky_posts); <br>
unset( $sticky_posts[$offset] ); <br>
} <br>
} <br>
// If any posts have been excluded specifically, Ignore those that are sticky. <br>
if ( !emptyempty($sticky_posts) &amp;&amp; !emptyempty($wp_query-&gt;query_vars['post__not_in'] ) ) <br>
$sticky_posts = array_diff($sticky_posts, $wp_query-&gt;query_vars['post__not_in']); <br>
// Fetch sticky posts that weren't in the query results <br>
if ( !emptyempty($sticky_posts) ) { <br>
$stickies = get_posts( array( <br>
'post__in' =&gt; $sticky_posts, <br>
'post_type' =&gt; $wp_query-&gt;query_vars['post_type'], <br>
'post_status' =&gt; 'publish', <br>
'nopaging' =&gt; true <br>
) ); <br>
foreach ( $stickies as $sticky_post ) { <br>
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) ); <br>
$sticky_offset++; <br>
} <br>
} <br>
} <br><br>
return $posts; <br>
}</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>代码说明:</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
1、如果你想让存档页也都显示全部置顶文章,那么就删掉11-43行的代码;</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
2、如果不想在某分类页显示置顶文章,将第 3 行的<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode7" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
if( <br>
//改成: <br>
// abc是分类名称 <br>
if ( is_category( 'abc' ) ||</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>3、如果不想某标签页显示置顶文章,将第 3 行的代码</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode8" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
if( <br>
//改成: <br>
// abc是标签名称 <br>
if ( is_tag( 'abc' ) ||</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>4、如果不想某作者页显示置顶文章,将第 3 行的</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode9" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
if( <br>
//改成: <br>
// abc是作者昵称 <br>
if ( is_author( 'abc' ) ||</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>5、以上代码只对主循环有效,如果你在存档页使用WP_Query或query_posts来获取文章列表,又像让这些列表顶部显示置顶文章,可以把第3行代码中的以下代码删掉(注意:可能会导致文章显示数量跟你设置的不一样):</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
代码如下:</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode10" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
|| !is_main_query()</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
置顶样式:如果你想给置顶文章添加样式,将以下代码添加到functions.php中,会给置顶文章添加一个名为 sticky 的class,具体的css代码,再自行自定义:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode11" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
add_filter('post_class', 'addStickyClass' ,10,3 ); <br>
function addStickyClass( $classes, $class, $post_id ){ <br>
if( is_sticky() &amp;&amp; is_category() &amp;&amp; !isset( $classes['sticky'] ) ){ <br>
$classes[] = 'sticky'; <br>
} <br>
return $classes; <br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家的WordPress建站有所帮助。</p>
頁: [1]
查看完整版本: WordPress分类与标签等存档页实现置顶的方法