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随机文章/随机推荐的实现思路与用法》的一些看法。更多内容请查看本栏目更多内容!
Berita是一个专为此类需要设计的收费theme。较之于同类产品,它同时为了添加定制化水平还革新了wp零碎,应用...
WordPress 4.1 正式版公布,这次更新带来了2015主题(Twenty Fifteen),免干扰写作模式,暗藏各种芜杂以及...
要求 如,在桌面设施上,图片应用的是以下的HTML代码: 复制代码代码如下: <img src="abc.png" alt="abc" w...
随着 WordPress 一版一版的晋级,性能越来越多,代码量也越来越宏大,对 WordPress 速度慢的埋怨声也越来越...
在以前的代码中,咱们仅仅应用add_shortcode函数来把短代码注册到wordpress中,在文章页应用the_content来显...
之前我都是用二级域名的方式来存储图片、JS 和 CSS ,前些天在推特上的一位顺序员 yoursunny 通知我,光用二...