• Resolved arturocivit

    (@arturocivit)


    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

Viewing 14 replies - 16 through 29 (of 29 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    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.
    https://codex.www.ads-software.com/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)
    Thread Starter arturocivit

    (@arturocivit)

    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?

    Moderator keesiemeijer

    (@keesiemeijer)

    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;

    Thread Starter arturocivit

    (@arturocivit)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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)?

    Thread Starter arturocivit

    (@arturocivit)

    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?

    Thread Starter arturocivit

    (@arturocivit)

    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!!

    Moderator keesiemeijer

    (@keesiemeijer)

    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)?

    Thread Starter arturocivit

    (@arturocivit)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter arturocivit

    (@arturocivit)

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

    Category: https://pastebin.com/ikvHrigj
    Sidebar: https://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!!

    Moderator keesiemeijer

    (@keesiemeijer)

    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;?>

    Thread Starter arturocivit

    (@arturocivit)

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

    Thread Starter arturocivit

    (@arturocivit)

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

Viewing 14 replies - 16 through 29 (of 29 total)
  • The topic ‘Confused with The Loop’ is closed to new replies.