• Resolved Bluetera

    (@afemaleprodigy)


    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;
    			?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • 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'
                   );

    Thread Starter Bluetera

    (@afemaleprodigy)

    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); 
    
                ?>
    Thread Starter Bluetera

    (@afemaleprodigy)

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

    Thread Starter Bluetera

    (@afemaleprodigy)

    Or would be something like this…

    if (is_page('224') ) {
                $cat = array(7,8);
                } else {
                $cat = '';
                }

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

    Thread Starter Bluetera

    (@afemaleprodigy)

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘change this code to random post?’ is closed to new replies.