njav 大佬有话说 :
有没有可以随机文章的wordpress模板介绍?
本帖最后由 njav 于 2020-10-1 03:10 编辑
有没有可以随机文章的wordpress模板介绍?
市面上挺少这类的模板
或者有没有相关的外挂? 可以在一个分类/页面可以做到随机展示文章
5000000 大佬有话说 :
多的是,随便百度学一下 修改下模板添加个函数钩子就行
shc 大佬有话说 :
function wpb_rand_posts() {
$args = array(
‘post_type’ => ‘post’,
‘orderby’ => ‘rand’,
‘posts_per_page’ => 5,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$string .= ‘<ul>’;
while ( $the_query->have_posts() ) {
$the_query->the_post();
$string .= ‘<li><a href="’. get_permalink() .’">’. get_the_title() .'</a></li>’;
}
$string .= ‘</ul>’;
/* Restore original Post Data */
wp_reset_postdata();
} else {
$string .= ‘no posts found’;
}
return $string;
}
add_shortcode(‘insert-random-post’,’wpb_rand_posts’);
add_filter(‘widget_text’, ‘do_shortcode’);
然后在需要放随机文章的地方插短代码