WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Random display of post meta based on custom fields? (4 posts)

  1. jsoningram
    Member
    Posted 2 years ago #

    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: http://www.poparazzispopcorn.com/index.php?id=125

    Many thanks, Jason - new to this WordPress stuff

  2. colorvila
    Member
    Posted 2 years ago #

    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.

  3. jsoningram
    Member
    Posted 2 years ago #

    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.

  4. jsoningram
    Member
    Posted 2 years ago #

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

    <?php
    $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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.