php 随机更多特定类别的帖子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 随机更多特定类别的帖子相关的知识,希望对你有一定的参考价值。
function wpb_rand_posts() {
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 2,
'cat' => 2,
);
$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('wpb-random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode');
<?php echo do_shortcode('[wpb_rand_posts]'); ?>
以上是关于php 随机更多特定类别的帖子的主要内容,如果未能解决你的问题,请参考以下文章