全球一统世界和谐 發表於 2023-9-8 00:00:00

wordpress随机文章/随机推荐的实现思路与用法

<div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<strong>wordpress实现随机文章</strong> <br>
ralix曾发布过关于wordpress随机文章的相关插件的点评文章(“wordpress插件之随机文章类插件点评”),百度一下也能搜出很多其他纯代码的方式,大致代码如下: <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>
<p>
代码如下:</p>
</div>
<div id="phpcode3" 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>
&lt;?php <br>
$query = array( <br>
'post_type' =&gt; 'post', <br>
'orderby' =&gt; 'rand' <br>
); <br>
$posts = new WP_Query( $query ); <br>
if ( $posts-&gt;have_posts() ) { <br>
while( $posts-&gt;have_posts() ) : <br>
$posts-&gt;the_post(); <br>
the_content(); <br>
endwhile; <br>
} <br>
wp_reset_query(); <br>
?&gt; </div>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>回头来看看这一段代码,其实很简单,在理解了的前提下,提出需要实现“随机推荐”,该怎么实现呢? </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>wordpress实现随机推荐</strong><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><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";'>
<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>
<p>
代码如下:</p>
</div>
<div id="phpcode4" 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>
&lt;?php <br>
//获取置顶文章的ID串 <br>
$rand_id = get_option( 'sticky_posts' ); <br>
$query = array( <br>
'post__in' =&gt; $rand_id, <br>
'post_type' =&gt; 'post', <br>
'orderyby' =&gt; 'rand', <br>
'numberposts' =&gt; 2 <br>
); <br>
$posts = new WP_Query( $query ); <br>
if ( $posts-&gt;have_posts() ) { <br>
while( $posts-&gt;have_posts() ) : <br>
$posts-&gt;the_post(); <br>
the_content(); <br>
endwhile; <br>
} <br>
wp_reset_query(); <br>
?&gt; </div>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>至于添加到widgets这里就不详说了。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>进阶应用:随便看看的功能实现 </strong><br><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>现在来看看“随便看看”是怎么实现的?大家不防到我的博客(jokerliang.com)看看导航栏“随便看看”的效果。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>然后在当前皮肤下的functions.php里添加如下代码: </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";'>
<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>
<p>
代码如下:</p>
</div>
<div id="phpcode5" 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>
&lt;?php <br>
add_action('init','random_add_rewrite'); <br>
add_action('template_redirect','random_template'); <br>
function random_add_rewrite() { <br>
global $wp; <br>
$wp-&gt;add_query_var('random'); <br>
add_rewrite_rule('random/?$', 'index.php?random=1', 'top'); <br>
} <br>
function random_template() { <br>
if (get_query_var('random') == 1) { <br>
$posts = get_posts('post_type=post&amp;orderby=rand&amp;numberposts=1'); <br>
foreach($posts as $post) { <br>
$random_link = get_permalink($post); <br>
} <br>
wp_redirect($random_link,307); //307临时跳转 <br>
exit; <br>
} <br>
} <br>
?&gt; </div>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>至此,效果已经实现(注:貌似3.3以前的老版本,需要进固定链接,执行一下“保存更改”才有效果)!当然网上也有通过新建页面,然后用javascript跳转的方法,大家也可以借鉴借鉴。</span></p>
</div>
<p>
 </p>
頁: [1]
查看完整版本: wordpress随机文章/随机推荐的实现思路与用法