标签: 随机文章

  • 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();
    ?>

    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调用全站随机文章

    wordpress调用全站随机文章代码

    <?php
    
    global $post;
    
    $postid = $post->ID;
    
    $args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’ => 10); // 显示篇数
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    ?>
    
    <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
  • wordpress调用同分类随机文章

    wordpress调用同分类随机文章代码

    <?php
    
    $cat = get_the_category();
    
    foreach($cat as $key=>$category){
    
    $catid = $category->term_id;}
    
    $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); // 显示篇数
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    while ($query_posts->have_posts()) : $query_posts->the_post();?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile;?>
    
    <?php wp_reset_query(); ?>