WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display "get_pages" Array in Random Order? (5 posts)

  1. jeffmckeand
    Member
    Posted 5 years ago #

    Hello everyone.

    I've written the code below to pull three testimonials from the portfolio section of a site and display them on the home page. Everything is working fine, except I can't seem to get them to display in a random order.

    Is there a way to do this using "get_pages", or am I stuck always showing them in a specific order?

    Here's the code:

    <?php
    
    	$pages = get_pages(array('child_of' => 15, 'meta_key' => 'testimonial', 'number' => 3));
    
    	foreach($pages as $child) {
    
    		$testimonial = get_post_meta($child->ID, 'testimonial', false);
    		$projName = get_the_title($child->ID);
    		$projLink = get_permalink($child->ID);
    
    	if ($testimonial) {
    
    		foreach ($testimonial as $testimony) {
    
    			$fullValue = explode("|", $testimony);
    
    			$docPic = $fullValue[0];
    			$docQuote = $fullValue[1];
    			$docName = $fullValue[2];
    
    			?>
    
    			<li class="testimonial">
    
    				<img src="<?php echo $docPic; ?>" alt="<?php echo $docName; ?>" title="<?php echo $docName; ?>" />
    
    				<p class="quote">&ldquo;<?php echo $docQuote; ?>&rdquo;</p>
    
    				<p class="cite"><span class="doc"><?php echo $docName; ?></span> | <a href="<?php echo $projLink; ?>" title="<?php echo $projName; ?>"><?php echo $projName; ?></a></p>
    
    			</li><!-- /.testimonial -->
    
    			<?php
    
    		}
    
    	}
    
    	}
    
    ?>

    Thanks for whatever help you can provide! :-)

  2. Mark / t31os
    Moderator
    Posted 5 years ago #

    After..

    $pages = get_pages(array('child_of' => 15, 'meta_key' => 'testimonial', 'number' => 3));

    Add..

    shuffle($pages);

    Does that help?

  3. jeffmckeand
    Member
    Posted 5 years ago #

    It kind of works. It shuffles the results, but those results are still the first three pages (in alpha-by-title order) that the function finds.

    I'm wanting the function to pull random results.

    Can't figure out how...

  4. jeffmckeand
    Member
    Posted 5 years ago #

    Got it. I don't know why I tried it with "get_pages". Switched to "query_posts", reconfigured the variables, added "'orderby' => rand", and now it works just fine.

    Thanks anyway for your help t31os_.

    Here's the working code:

    <?php
    
    	$pages = query_posts(array('post_parent' => 15, 'post_type' => 'page', 'meta_key' => 'testimonial', 'showposts' => 3, 'orderby' => rand));
    
    	foreach($pages as $child) {
    
    		$testimonial = get_post_meta($child->ID, 'testimonial', false);
    		$projName = get_the_title($child->ID);
    		$projLink = get_permalink($child->ID);
    
    	if ($testimonial) {
    
    		foreach ($testimonial as $testimony) {
    
    			$fullValue = explode("|", $testimony);
    
    			$docPic = $fullValue[0];
    			$docQuote = $fullValue[1];
    			$docName = $fullValue[2];
    
    			?>
    
    			<li class="testimonial">
    
    				<img src="<?php echo $docPic; ?>" alt="<?php echo $docName; ?>" title="<?php echo $docName; ?>" />
    
    				<p class="quote">&ldquo;<?php echo $docQuote; ?>&rdquo;</p>
    
    				<p class="cite"><span class="doc"><?php echo $docName; ?></span> | <a href="<?php echo $projLink; ?>" title="<?php echo $projName; ?>"><?php echo $projName; ?></a></p>
    
    			</li><!-- /.testimonial -->
    
    			<?php
    
    		}
    
    	}
    
    	}
    
    ?>
  5. Mark / t31os
    Moderator
    Posted 5 years ago #

    Good stuff...
    A good example of a user posting back their solution.

    I was going to suggest get_posts, but if it's the only thing happening on the page, then yes, just rewrite the parameters for query_posts...

Topic Closed

This topic has been closed to new replies.

About this Topic