WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Confused with The Loop (30 posts)

  1. arturocivit
    Member
    Posted 9 months ago #

    Hi there guys, quick question, I have a category.php page. How it should work its pretty easy, when you click a category you'll land on this page with all the posts associated to that category, however, there's something wrong because it is printing all the posts no matter what category you are clicking on, this is part of my code, or at least, the code relevant to this question:

    First I'm printing the Category name - This is actually working -

    <div class="breadcrumbs-categories"><h1>NOTICIAS DE <?php echo single_cat_title(); ?> EN ESPAÑOL</h1></div>

    The The Loop - Which is not working -

    <?php
    
    global $post;
    
    $args = array( 'numberposts' => 3 );
    
    $myposts = get_posts( $args );
    
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    
    <!-- START CONTENTS 1 -->
    
    <div class="title-categories"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    <div class="date-categories"><?php the_time('F jS, Y') ?> Escrito por <?php the_author() ?></div>
    
    <!-- Start Contents -->
    
    <div class="maincontents-categories">
    
    <p><?php echo substr(get_the_excerpt(), 0,500); ?></p>
    
    <p> </p>
    
    <a href="<?php the_permalink(); ?>" class="readmore">Leer más...</a>
    
    </div>
    
    <?php endforeach; ?>

    What Am I missing?? When I call a category it prints the category name but prints the first 4 posts no matter the category, any idea?

    Thanks!

    Arturo

  2. arturocivit
    Member
    Posted 9 months ago #

    Just noticed that I pasted the wrong code, so here I go again, same problem and this time this is the right code I'm using in my category page:

    <div id="content-categories">
    
    <div class="breadcrumbs-categories"><h1>NOTICIAS DE <?php printf( __( '%s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?> EN ESPAÑOL</h1></div>
    
    <?php query_posts('showposts=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div class="title-categories"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    
    <div class="date-categories"><?php the_time('F jS, Y') ?> Escrito por <?php the_author() ?></div>
    
    <div class="maincontents-categories">
    
    <p><?php echo substr(get_the_excerpt(), 0,500); ?></p>
    
    <p>&nbsp;</p>
    
    <a href="<?php the_permalink(); ?>" class="readmore">Leer m&aacute;s...</a>
    
    </div>
    
    <?php endwhile;?>
    
    <?php else : ?>
    
    <h1>Nothing Found</h1>
    
    <?php endif; wp_reset_query(); ?>

    Thanks in advance!

  3. doc4
    Member
    Posted 9 months ago #

    Might be a little overkill but try this and let me know if it works:

    <?php $my_query = new WP_Query('showposts=3');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID ?>

    Almost forgot, end it with this:

    <?php endwhile; ?>

    instead of what you currently have.

  4. arturocivit
    Member
    Posted 9 months ago #

    Hey doc4, nope, it shows the same thing.

  5. doc4
    Member
    Posted 9 months ago #

    Try this just to see if it separates the posts:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

  6. arturocivit
    Member
    Posted 9 months ago #

    Well not sure because it throws me an error on line 64, I have this on that line:

    <?php get_footer(); ?>

  7. arturocivit
    Member
    Posted 9 months ago #

    Actually, having the following it separates the posts, but, now I have 10 posts instead the 3 I need in that section:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <!-- START CONTENTS 1 -->
    
    <div class="title-categories"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    
    <div class="date-categories"><?php the_time('F jS, Y') ?> Escrito por <?php the_author() ?></div>
    
    <!-- Start Contents -->
    
    <div class="maincontents-categories">
    
    <p><?php echo substr(get_the_excerpt(), 0,500); ?></p>
    
    <p>&nbsp;</p>
    
    <a href="<?php the_permalink(); ?>" class="readmore">Leer m&aacute;s...</a>
    
    </div>
    
    <?php endwhile;?>
    
    <?php else : ?>
    
    <h1>Nothing Found</h1>
    
    <?php endif; wp_reset_query(); ?>
  8. doc4
    Member
    Posted 9 months ago #

    Okay, save your code in a text editor and replace it with this just to see if it's working. I'm trying to get rid of anything not related and break this down to the basics. Leave the header and footer code of course but if you have another loop remove it for now.

    <div id="content-categories">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="maincontents-categories">
    
    <p><?php the_title(); ?></p>
    
    </div>
    
    <?php endwhile; endif; ?>
  9. arturocivit
    Member
    Posted 9 months ago #

    Yup, that one works but works like in my past reply, just throws me all 10 posts, breaking my head here and don't understand what might be wrong.

  10. doc4
    Member
    Posted 9 months ago #

    Okay, hang on a second

  11. doc4
    Member
    Posted 9 months ago #

    You probably have the max posts set to ten under settings which is why you're seeing ten total.

    Anyway try this:

    <div id="content-categories">
    
    <?php query_posts('showposts=3'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="maincontents-categories">
    
    <p><?php the_title(); ?></p>
    
    </div>
    
    <?php endwhile; endif; ?>

    Yes, I realize this is basically what you had before.

  12. arturocivit
    Member
    Posted 9 months ago #

    Yup I know, the thing is, in a few pages I'll need more than 10, in others just 3 or 5 of them, anyway, tried that one and no, it just output 3 posts but it doesn't separate them, always the same 3 posts, the weird thing is this, it prints the right category name, for that I'm using:

    <?php printf( __( '%s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?>

    But that's it, gee!, where's the vodka when needed?

  13. doc4
    Member
    Posted 9 months ago #

    Wait a minute, I just realized to make this work we probably need to tell the page what categories we are specifically wanting to see. So you would normally split this page into several IF/THEN statements. IF category 1 show posts from category 1. Then another loop for another category.

    Try this, hopefully my code is somewhat sound. Replace the two names of the categories with the categories you're wanting to display.

    <?php
    if (in_category('1')) {
      $category_name = 'your_category_name';
    } else if (in_category('2')) {
      $category_name = 'your_second_category_name';
    }
    query_posts( "category_name=$category_name&posts_per_page=3"); ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <div class="maincontents-categories">
    
    <p><?php the_title(); ?></p>
    
    </div>
    
    <?php endwhile; endif; ?>
  14. arturocivit
    Member
    Posted 9 months ago #

    It is throwing me an error on line 40 which is:

    <?php endwhile; endif; ?>

  15. keesiemeijer
    moderator
    Posted 9 months ago #

    Try it by removing the query from the category.php file and query with this in your theme's functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
    
        if(is_category()){
          $query->set('posts_per_page', 3);
        }
    
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    http://www.billerickson.net/customize-the-wordpress-query/
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

  16. arturocivit
    Member
    Posted 9 months ago #

    Hi keesiemeijer, good day!

    That did the trick, and seems like it works nicely, however, in that document, where the 3 posts are appearing there's a sidebar with a different design elements where it needs to have 8 posts of the same category, calling the posts there just output 3 because of the function, what should I do, create a new function with a different name with more posts to be displayed? Thanks so much for the help!

  17. keesiemeijer
    moderator
    Posted 9 months ago #

    there's a sidebar with a different design elements where it needs to have 8 posts of the same category,

    I assume this is a secondary loop. Try using get_posts for it.
    http://codex.wordpress.org/Template_Tags/get_posts

    You can get the current category with this:

    $current_cat = get_query_var('cat'); // category ID
    $current_cat_slug = get_query_var('category_name'); // category slug (only on category page)
  18. arturocivit
    Member
    Posted 9 months ago #

    Thanks a lot, reasearching a little bit seems like i can use something like this on that sidebar:

    <?php global $post; // required
    $args = array('numberposts'=>3, 'category'=>-6,-9, 'order'=>'ASC');
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post);
    ...
    endforeach;
    ?>

    But the thing is, I need that page to place only posts on selected category, if people click on category B the sidebar will display 8 posts on category B, but people can click on any category they want and if the admin creates a new category this automatically needs to be available to be displayed, in the example above categories are pre defined, how to automatically display any given category?

  19. keesiemeijer
    moderator
    Posted 9 months ago #

    Not sure why you want to exclude categories but try this:

    $current_cat = get_query_var('cat'); // category ID
    $args = array('posts_per_page' => 3, 'cat' => $current_cat, 'order' => 'ASC');
    $custom_posts = get_posts($args);
    // the rest

    I don't think you need global $post;

  20. arturocivit
    Member
    Posted 9 months ago #

    No no, I don't want to exclude categories, that was just an example I found around, but the question is still the same, in your example above you are only calling category 3, what I need is to call any given category, no matter which one, just catch the category and post 8 posts different than the 3 featured posts in the main area.

  21. keesiemeijer
    moderator
    Posted 9 months ago #

    you are only calling category 3,

    I don't think so. Does it get the posts for the current category archive page (but not different from the main loop)?

  22. arturocivit
    Member
    Posted 9 months ago #

    Sorry, you are right, didn't see that one properly.....

    Ok, just to be sure, I'll place this first:

    <?php global $post; // required
    $current_cat = get_query_var('cat'); // category ID
    $args = array('posts_per_page' => 3, 'cat' => $current_cat, 'order' => 'ASC');
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post);
    endforeach;
    ?>

    And then, my regular loop?

  23. arturocivit
    Member
    Posted 9 months ago #

    Well, that worked actually, it works like a charm to be honest, but just one detail, it is printing the same 3 posts I have on my main area, in my main area where I have the 3 posts you helped me to with the function code I want to have some kind of featured posts on that category and then at the side bar the next 3 posts, but they needs to be different than the first 3 posts in the main area, is there a filter or something I can play with?

    Also, the sidebar only shows 3 posts and I need 8, but I think it is because that function code you sent me?

    This is my code on the sidebar:

    <div id="sidebar-categories">
    
    <?php global $post; // required
    $current_cat = get_query_var('cat'); // category ID
    $args = array('posts_per_page' => 8, 'cat' => $current_cat, 'order' => 'ASC');
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post);
    endforeach;
    ?>
    
    <div id="title-sidebar-categories"><?php printf( __( '%s' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></div>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div id="date-sidebar-categories"><?php the_time('F jS, Y') ?></div>
    <div id="title-sidebar-categories"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></div>
    <div id="contents-sidebar-categories"><?php echo substr(get_the_excerpt(), 0,66); ?></div>
    
    <?php endwhile;?>
    
    <?php else : ?>
    
    <h1>Nothing Found</h1>
    
    <?php endif; wp_reset_query(); ?>
    
    </div>

    But other than that works really cool, thanks so much keesiemeijer!!

  24. keesiemeijer
    moderator
    Posted 9 months ago #

    Try it with this in your sidebar:

    $args = array('posts_per_page' => 8, 'cat' => $current_cat, 'order' => 'ASC', 'offset' => 3);
    $custom_posts = get_posts($args);

    Do you need pagination on your main loop (not your sidebar)?

  25. arturocivit
    Member
    Posted 9 months ago #

    Hi there keesiemeijer, thanks,

    Well tried what you suggested and no, didn't worked, still displaying only 3 posts and repeating the 3 posts on the main area, and I don't think I'm going to use pagination there because that's a link below those 3 main posts that leads to the category archive.

  26. keesiemeijer
    moderator
    Posted 9 months ago #

    Can you try it without:

    global $post; // required

    Can you post the full code of your category.php template and your sidebar code. see the Forum Rules on how the post code.

  27. arturocivit
    Member
    Posted 9 months ago #

    Sure, I took off the global thing but didn't worked as well, here are the source codes:

    Category: http://pastebin.com/ikvHrigj
    Sidebar: http://pastebin.com/Ni0D4huT

    Maybe the 3 posts is because the code in the functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
    
        if(is_category()){
          $query->set('posts_per_page', 3);
        }
    
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Thanks!!

  28. keesiemeijer
    moderator
    Posted 9 months ago #

    You're using a normal loop in your sidebar. Try it with the get_posts loop:

    <?php
    $current_cat = get_query_var( 'cat' ); // category ID
    $args = array( 'posts_per_page' => 8, 'cat' => $current_cat, 'order' => 'ASC', 'offset' => 3 );
    $custom_posts = get_posts( $args );
    foreach ( $custom_posts as $post ) : setup_postdata( $post ); ?>
    <div id="date-sidebar-categories"><?php the_time( 'F jS, Y' ) ?></div>
    <div id="title-sidebar-categories"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></div>
    <div id="contents-sidebar-categories"><?php echo substr( get_the_excerpt(), 0, 66 ); ?></div>
    <?php endforeach;?>
  29. arturocivit
    Member
    Posted 9 months ago #

    Got to say, that worked wonderfully, thank you so much keesiemeijer for your help!

  30. arturocivit
    Member
    Posted 9 months ago #

    This topic has been resolved, thanks to doc4 and super special thanks to keesiemeijer for his time. Thanks buddy!!!

Reply

You must log in to post.

About this Topic

Tags

No tags yet.