让WordPress分类/标签等存档页也能置顶文章


------正文内容展示,开始汲取新知识啦------

WordPress有原生的文章置顶功能,不过只支持在首页让置顶文章在顶部显示,其他如分类页、标签页、作者页和日期页等存档页面,就没法让置顶文章在顶部显示了,只能按默认的顺序显示。如何才能实现存档页面置顶文章呢?今天分享来自 百度的方法。

将下面的代码添加到主题的 functions.php 文件即可:

代码说明

1、如果你想让存档页也都显示全部置顶文章,那么就删掉12-45行的代码;
2、如果不想在某分类页显示置顶文章,将第 3 行的

<span class="token keyword">if</span><span class="token punctuation">(</span>

改成:

<span class="token comment">// abc是分类名称</span><span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">is_category</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'abc'</span> <span class="token punctuation">)</span> <span class="token operator">||</span>

3、如果不想某标签页显示置顶文章,将第 3 行的

<span class="token keyword">if</span><span class="token punctuation">(</span>

改成:

<span class="token comment">// abc是标签名称</span><span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">is_tag</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'abc'</span> <span class="token punctuation">)</span> <span class="token operator">||</span>

4、如果不想某作者页显示置顶文章,将第 3 行的

<span class="token keyword">if</span><span class="token punctuation">(</span>

改成:

<span class="token comment">// abc是作者昵称</span><span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">is_author</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'abc'</span> <span class="token punctuation">)</span> <span class="token operator">||</span>

5、以上代码只对主循环有效,如果你在存档页使用WP_Query或query_posts来获取文章列表,又想让这些列表顶部显示置顶文章,可以把第3行代码中的以下代码删掉(注意:可能会导致文章显示数量跟你设置的不一样):

<span class="token operator">||</span> <span class="token operator">!</span><span class="token function">is_main_query</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

置顶样式

     如果你想给置顶文章添加样式,将以下代码添加到functions.php中,会给置顶文章添加一个名为 sticky 的class。具体的css代码,再自行自定义:

<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post_class'</span><span class="token punctuation">,</span>  <span class="token string single-quoted-string">'addStickyClass'</span> <span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">function</span> <span class="token function-definition function">addStickyClass</span><span class="token punctuation">(</span> <span class="token variable">$classes</span><span class="token punctuation">,</span> <span class="token variable">$class</span><span class="token punctuation">,</span> <span class="token variable">$post_id</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>  <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token function">is_sticky</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token function">is_category</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token operator">!</span><span class="token keyword">isset</span><span class="token punctuation">(</span> <span class="token variable">$classes</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sticky'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>    <span class="token variable">$classes</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'sticky'</span><span class="token punctuation">;</span>  <span class="token punctuation">}</span>    <span class="token keyword">return</span> <span class="token variable">$classes</span><span class="token punctuation">;</span><span class="token punctuation">}</span>

原文来自子比主题官网的用户50914202

温馨提示:本文最后更新于2023-07-27 23:39:19,某些文章具有时效性,若有错误或已失效,请私信客服或联系知新社长
------本文内容已结束,喜欢请分享------

感谢您的访问,Ctrl+D收藏本站吧。

让WordPress分类/标签等存档页也能置顶文章-知新网
让WordPress分类/标签等存档页也能置顶文章
此内容为付费阅读,请付费后查看
200积分
供学习和研究使用,请在下载后24小时内删除
购买前可以联系作者确认资源信息,防止交易矛盾
会员特权
技术支持
自动发货
网络收集
付费阅读
© 版权声明
七月 27

知岛上的今时往日

THE END
点赞0投币 分享
评论 抢沙发
头像
善语结善缘,恶言伤人心。(禁止发送无意义的字符)
提交
头像

昵称

夸夸Ta
夸夸
还有吗!没看够!
取消
昵称表情代码图片

    暂无评论内容

社区求救信号帮助是一种美德