• 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 15 replies - 1 through 15 (of 29 total)
  • Thread Starter arturocivit

    (@arturocivit)

    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!

    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.

    Thread Starter arturocivit

    (@arturocivit)

    Hey doc4, nope, it shows the same thing.

    Try this just to see if it separates the posts:

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

    Thread Starter arturocivit

    (@arturocivit)

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

    <?php get_footer(); ?>

    Thread Starter arturocivit

    (@arturocivit)

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

    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; ?>
    Thread Starter arturocivit

    (@arturocivit)

    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.

    Okay, hang on a second

    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.

    Thread Starter arturocivit

    (@arturocivit)

    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?

    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; ?>
    Thread Starter arturocivit

    (@arturocivit)

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

    <?php endwhile; endif; ?>

    Moderator keesiemeijer

    (@keesiemeijer)

    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' );

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

    Thread Starter arturocivit

    (@arturocivit)

    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!

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