wordpress完成随机文章
ralix曾公布过对于wordpress随机文章的相干插件的点评文章(“wordpress插件之随机文章类插件点评”),百度一下也能搜出很多其余纯代码的形式,大抵代码如下:
<?php
$query = array(
'post_type' => 'post',
'orderby' => 'rand'
);
$posts = new WP_Query( $query );
if ( $posts->have_posts() ) {
while( $posts->have_posts() ) :
$posts->the_post();
the_content();
endwhile;
}
wp_reset_query();
?>
<?php
//获取置顶文章的ID串
$rand_id = get_option( 'sticky_posts' );
$query = array(
'post__in' => $rand_id,
'post_type' => 'post',
'orderyby' => 'rand',
'numberposts' => 2
);
$posts = new WP_Query( $query );
if ( $posts->have_posts() ) {
while( $posts->have_posts() ) :
$posts->the_post();
the_content();
endwhile;
}
wp_reset_query();
?>
<?php
add_action('init','random_add_rewrite');
add_action('template_redirect','random_template');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$random_link = get_permalink($post);
}
wp_redirect($random_link,307); //307暂时跳转
exit;
}
}
?>
以上就是安达网络工作室关于《wordpress随机文章/随机推荐的实现思路与用法》的一些看法。更多内容请查看本栏目更多内容!
上一篇,咱们应用 Meta Box 为文章增加了一个“推荐指数”字段。增加后用了一段工夫,仿佛不那么...
有意中发现一款性能弱小的Wordpress代码高亮插件 – Crayon Syntaxer,我以为能够算是目前款式最多、易...
因为Google被彻底墙掉,不翻墙就无奈加载谷歌字体。很多老手冤家常常埋怨本人买的主机如何如何慢,给的示例...
要害字形容:心得 建站 应用 菜鸟 能够 这个 边栏 主题 文章 一个 这一周都在学用wordpress来做独立博...
复制代码代码如下: //该办法为向曾经存在的菜单中增加子菜单 function add_submenu() { add_submenu_page( ...
给一篇好的文章增加一些分享按钮分享到一些知名网站,很利于网站的推行。给站点增加分享按钮的办法很多,你...