我的知识记录

wordpress如何调用随机文章和随机推荐文章

wordpress怎样调用随机文章
方法一:foreach循环
<?php $rand_posts = get_posts('numberposts=10&orderby=rand'); foreach( $rand_posts as $post ) : ?>

  • “><?php the_title(); ?>
  • <?php endforeach; ?>
    方法二:
    <?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();
    ?>
    wordpress随机调用置顶推荐的文章代码:
    <?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();
    ?>

    wordpress如何调用随机文章和随机推荐文章

    标签:

    更新时间:2025-12-07 21:15:34

    上一篇:PbootCMS列表内容ajax无刷新加载数据

    下一篇:pbootcms列表页输出多样式判断方法