WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] change this code to random post? (7 posts)

  1. AFemaleProdigy
    Member
    Posted 3 years ago #

    Hello everyone! I am using the code below to display the most recent post from a certain category on a specific page. How can I modify this to display a random post from multiple specific categories instead? Or is there some entirely different code I should use?

    <?php
                // page id 224 will get category ID 7 posts
                if (is_page('224') ) {
                $cat = array(7);
                } else {
                $cat = '';
                }
    
                $showposts = 1; // -1 shows all posts
                $do_not_show_stickies = 1; // 0 to show stickies
                $args=array(
                   'category__in' => $cat,
                   'showposts' => $showposts,
                   'caller_get_posts' => $do_not_show_stickies
                   );
                $my_query = new WP_Query($args); 
    
                ?>
    
    	<?php if( $my_query->have_posts() ) : ?>
    
    		<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    			<?php
    			//necessary to show the tags
    			global $wp_query;
    			$wp_query->in_the_loop = true;
    			?>
  2. wpua
    Member
    Posted 3 years ago #

    Hi,

    Try change:

    $args=array(
                   'category__in' => $cat,
                   'showposts' => $showposts,
                   'caller_get_posts' => $do_not_show_stickies
                   );

    To:

    $args=array(
                   'category__in' => array($cat1, $cat2), //your multiple categories
                   'showposts' => $showposts,
                   'caller_get_posts' => $do_not_show_stickies,
                   'orderby' => 'rand'
                   );
  3. AFemaleProdigy
    Member
    Posted 3 years ago #

    Hmmm... so should the whole thing look like this then...

    <?php
                // page id 224 will get category ID 7 posts
                if (is_page('224') ) {
                $cat1 = array(7);
    			$cat2 = array(8);
                } else {
                $cat = '';
                }
    
                $showposts = 1; // -1 shows all posts
                $do_not_show_stickies = 1; // 0 to show stickies
                $args=array(
    			   'category__in' => array($cat1, $cat2), //your multiple categories
                   'showposts' => $showposts,
                   'caller_get_posts' => $do_not_show_stickies,
                   'orderby' => 'rand'
                   );
                $my_query = new WP_Query($args); 
    
                ?>
  4. AFemaleProdigy
    Member
    Posted 3 years ago #

    I get an "not found" error when I do that. Not sure if I did that right.

  5. AFemaleProdigy
    Member
    Posted 3 years ago #

    Or would be something like this...

    if (is_page('224') ) {
                $cat = array(7,8);
                } else {
                $cat = '';
                }
  6. wpua
    Member
    Posted 3 years ago #

    Sorry, dont noticed you alredy have array in $cat.

    <?php
                // page id 224 will get category ID 7 posts
                if (is_page('224') ) {
                $cat = array(7,8); //will get posts for 7 and 8 categories
                } else {
                $cat = '';
                }
    
                $showposts = 1; // -1 shows all posts
                $do_not_show_stickies = 1; // 0 to show stickies
                $args=array(
    			   'category__in' => $cat,
                   'showposts' => $showposts,
                   'caller_get_posts' => $do_not_show_stickies,
                   'orderby' => 'rand'
                   );
                $my_query = new WP_Query($args); 
    
                ?>
  7. AFemaleProdigy
    Member
    Posted 3 years ago #

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.