Support » Fixing WordPress » Random display of post meta based on custom fields?

  • Resolved jsoningram


    Not sure I worded the topic correctly. Basically I have a need to randomly display 3 sets of text and a photo, both of which are stored in a page as custom fields. When the page refreshes, it displays 3 new sets of data.

    Here’s an example page:

    Many thanks, Jason – new to this WordPress stuff

Viewing 3 replies - 1 through 3 (of 3 total)
  • 1. Indexed your meta datas
    2. Each time when you wp_query your post, use rand() function to get 3 indexes, and load the corresponding meta data to display.

    Thanks ColorVilla. Unfortunately I’m so new I don’t know how to implement that. I’m looking for either the code or a really good tutorial.

    I figured it out so I thought I’d post back here. I’m sure there’s a better way but here goes:

    $current_parent = $post->ancestors; // Get parent's ID
    $current_page = $post->ID; // Get current page's ID
    shuffle(query_posts(array('post__not_in'=>array($current_page), 'post_parent'=>$current_parent[0], 'post_type'=>page, 'posts_per_page'=>-1)));
    	while (have_posts()) : the_post(); ?>
    		<a href='<?php the_permalink(); ?>'>
    		<?php echo "<div class='suggestions'>";
    		echo "<img src='" . get_post_meta($post->ID, 'product_image_lg', true) . "' />";
    		echo "<p class='popcorn_id'>";
    		echo "<span>#";
    		echo get_post_meta($post->ID, 'product_id', true);
    		echo "</span> - ";
    		echo the_title();
    		echo "</p>";
    		echo "</div>"; ?>
    		</a>  <!-- end suggestions -->
    	<?php endwhile; wp_reset_query();

    First I get the IDs of the current page and it’s parent page. Then use query_posts() based on those two findings, shuffle the results and display.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Random display of post meta based on custom fields?’ is closed to new replies.