• Hi,

    I am playing with the query_posts(‘cat=3’) I am building a results system on wordpress, like this one

    anywayz, When I use that querypost to select a post from a category, and then go the get the next post, from the next category, it still fetches the post from the fist cat? probably a very simple thing, but i’m (not yet) a php programmer, so i don’t know what I do wrong. Here’s the code, please help so I can repeat this 64 times and complete my system ??

    (i get the title (name) and a customfield image from the post for the results overview, the html will be replaced by css if i get the coding done)

    <?php
    // retrieve one post  from Cat of 3
    query_posts('cat=3');
    ?>
    
      <table>
      <tr>
      <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></td><td>
    <?php if (!get_post_meta($post->ID, "image-portfolio", true)) : // Show no image if page doesn't contain image in custom field ?>
    <?php else : ?>
    <div class="portfolio-border">
    <a href="<?php echo get_post_meta($post->ID, "url-portfolio", true); ?>" title="View Project Details for <?php the_title(); ?>">
    <img src="<?php echo get_post_meta($post->ID, "image-portfolio", $single = true); ?>" width="25" alt="Preview - <?php the_title(); ?>" /></a>
    </div>
    <?php endif; ?></td>
    </tr>
    </table>            
    
    <?php
    // retrieve one post  from Cat of 4
    query_posts('cat=4');
    ?>
    
      <table>
      <tr>
      <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></td><td>
    <?php if (!get_post_meta($post->ID, "image-portfolio", true)) : // Show no image if page doesn't contain image in custom field ?>
    <?php else : ?>
    <div class="portfolio-border">
    <a href="<?php echo get_post_meta($post->ID, "url-portfolio", true); ?>" title="View Project Details for <?php the_title(); ?>">
    <img src="<?php echo get_post_meta($post->ID, "image-portfolio", $single = true); ?>" width="25" alt="Preview - <?php the_title(); ?>" /></a>
    </div>
    <?php endif; ?></td>
    </tr>
    </table>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Don’t use multiple query_posts in a single page. Use get_posts instead.

    https://codex.www.ads-software.com/Template_Tags/query_posts

    Important note

    The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

    The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

    https://codex.www.ads-software.com/Template_Tags/get_posts

    Thread Starter djavin

    (@djavin)

    thanks, I’ll dive into that info ??

    Thread Starter djavin

    (@djavin)

    I think I did it ?? (will I be ninja one day too? haha :P)

    I now have it like this, would you agree or do you have another pointer for me on my way to woosha?

    <?php
     $lastposts = get_posts('category=3');
     foreach($lastposts as $post) :
        setup_postdata($post);
     ?>
    
      <table>
      <tr>
      <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></td><td>
    <?php if (!get_post_meta($post->ID, "image-portfolio", true)) : // Show no image if page doesn't contain image in custom field ?>
    <?php else : ?>
    <div class="portfolio-border">
    <a href="<?php echo get_post_meta($post->ID, "url-portfolio", true); ?>" title="View Project Details for <?php the_title(); ?>">
    <img src="<?php echo get_post_meta($post->ID, "image-portfolio", $single = true); ?>" width="25" alt="Preview - <?php the_title(); ?>" /></a>
    </div>
    <?php endif; ?></td>
    </tr>
    </table>
     <?php endforeach; ?>
    
    <?php
     $lastposts = get_posts('category=4');
     foreach($lastposts as $post) :
        setup_postdata($post);
     ?>
    
      <table>
      <tr>
      <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></td><td>
    <?php if (!get_post_meta($post->ID, "image-portfolio", true)) : // Show no image if page doesn't contain image in custom field ?>
    <?php else : ?>
    <div class="portfolio-border">
    <a href="<?php echo get_post_meta($post->ID, "url-portfolio", true); ?>" title="View Project Details for <?php the_title(); ?>">
    <img src="<?php echo get_post_meta($post->ID, "image-portfolio", $single = true); ?>" width="25" alt="Preview - <?php the_title(); ?>" /></a>
    </div>
    <?php endif; ?></td>
    </tr>
    </table>
     <?php endforeach; ?>

    Would you recommende to use categories to determine which post goes where on the competition chart?

    My plan would be to have 32 posts (every competitor a post) and then on the template select 1a as the competitor for round 1 (competing against 1b) If 1a would win, I would asign category 8a to him, being a competitor in the next round too.

    I know it’s not really building a blog, but I love WP, would be cool if I can add this chart to an event site that would have the regular blog and pages functionality too.

    thanks already ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Newby question on selecting a single post from a category’ is closed to new replies.