中江古店良凤电器 發表於 2023-9-4 00:00:00

如何给wordpress创建动态的置顶文章长时间引起注意

<p>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>从WordPress 2.7 开始加入了“置顶文章(Sticky Posts)”。置顶文章的作用是希望长时间引起读者注意,可以表明站点的简介、版权声明、友情链接的交换原则等。但太长时间的不变也会引起阅读反感。本文将介绍如何给wordpress创建动态的置顶文章。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>从wordpress help forums关于多重循环的各种讨论中让我想到一个使用两次循环的方法。一个用来查询并仅显示某个分类中的文章。另一个用来查询在所有分类中的所有的文章。第一个查询是从the codex中复制来的,很容易理解: </span></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";'>
<p 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></p>
<p>
代码如下:</p>
</div>
<p 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";'>
<br>
$my_query = new WP_Query('category_name=frontpage&amp;showposts=1'); <br>
while ($my_query-&gt;have_posts()) : <br>
$my_query-&gt;the_post(); <br>
$do_not_duplicate = $post-&gt;ID; </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>上述代码的意思是,变量my_query赋值为查询分类名为frontpage的所有文章的结果,并仅得到一篇文章。关键点在最后部分,给变量 do_not_duplicate赋值为返回的单独文章的ID值。我们在下一步需要这个值。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>下面一段代码是很重要很标准的,可以在许多主题中找到。它能获取所有日志: </span></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";'>
<p 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></p>
<p>
代码如下:</p>
</div>
<p 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";'>
<br>
if (have_posts()) : while (have_posts()) : the_post(); //do stuff like format each postendwhile; </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>这段代码获取所有的日志,并根据//do stuff这行(依赖于模板)的格式显示。如果仅仅这样编写代码,就会发生你所选的文章(置顶文章)被显示两次的情况(一次在置顶区域,另一次则是在所有其他文章区域)。显示这样并不好,所以这里要使用到do_not_duplicate函数了。添加到一个if语句中阻止制定文章显示两次。下面是代码: </span></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";'>
<p 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></p>
<p>
代码如下:</p>
</div>
<p 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";'>
<br>
if (have_posts()) : while (have_posts()) : the_post(); <br>
if( $post-&gt;ID == $do_not_duplicate ) <br>
continue;//do stuff like format each post endwhile; </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>代码中的第二个if语句的意思是,获取所有日志,且当发现一篇文章的ID值与do_not_duplicate的值相等时中断执行(continue语句),否则显示根据//do stuff这行的格式显示所有其他的文章。记住,do_not_duplicate函数值所代表的文章已经被显示过了。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>就这样当你把所有内容放到一起得到了什么?一个动态的置顶文章!且最棒的部分是当置顶文章被新的一篇代替,之前的文章会在下面所有文章的区域显示出来(取决于你选择了多少文章显示以及文章的发表频率)。</span></p>
頁: [1]
查看完整版本: 如何给wordpress创建动态的置顶文章长时间引起注意