WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help with category loop (5 posts)

  1. MrThunder
    Member
    Posted 1 year ago #

    Hi I am trying to get info from a category loop but I cannot get anything to display.

    Any advice would be appreciated, Thanks

    <?php 
    
              $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
              $my_query = new WP_Query(array('category__in' => array(4), 'posts_per_page' => 2,'paged'=>$paged)); ?>
                <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) :the_post(); ?>
                        <?php $currentid = get_the_id(); ?>
    
                        <div class="project-box grid-box grid-block mod-box width100">
                                <div class="project-img">
                                    <?php if (get_the_post_thumbnail($currentid) != "") { ?>
                                       <a href="<?php the_permalink(); ?>" ><?php echo get_the_post_thumbnail($currentid,array(300,600)); ?></a>
                                    <?php } else { ?>
                                        <a href="<?php the_permalink(); ?>" ><img src='<?php bloginfo('template_url') ?>/images/default.png' alt='Killen Civlil Engineering'/></a>
                                    <?php } ?>
                                </div> 
    
                                <div class="project-info">
                                    <h2><a href="<?php the_permalink(); ?>"><?php echo get_the_title($currentid); ?></a></h2>
                                    <p><?php echo get_the_excerpt(); ?></p>
                                     <a href="<?php the_permalink(); ?>" class="info">More  »</a>
                                </div>
                                            </div> 
    
                    <?php endwhile; ?>
                  <div class="project-pagination">
    
                      <div class="pagination page-btn"><?php next_posts_link( '« Prev' ); ?></div>
                        <div class="pagination page-btn-1"><?php previous_posts_link( 'Next »' ); ?></div>
    
                  </div>  
    
                <?php
                endif;
                wp_reset_query();
    ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you have missed to add $my_query-> to the_post();

    i.e. change the start of the loop to:

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

    and replace

    wp_reset_query();

    with

    wp_reset_postdata();
  3. MrThunder
    Member
    Posted 1 year ago #

    Thank you very much, that worked great, except I now my pagination has disappeared.

    Any suggestions?

    <?php 
    
              $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
              $my_query = new WP_Query(array('category__in' => array(4), 'posts_per_page' => 2,'paged'=>$paged)); ?>
              <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
                        <?php $currentid = get_the_id(); ?>
    
                        <div class="project-box grid-box grid-block mod-box width100">
                                <div class="project-img">
                                    <?php if (get_the_post_thumbnail($currentid) != "") { ?>
                                       <a href="<?php the_permalink(); ?>" ><?php echo get_the_post_thumbnail($currentid,array(300,600)); ?></a>
                                    <?php } else { ?>
                                        <a href="<?php the_permalink(); ?>" ><img src='<?php bloginfo('template_url') ?>/images/default.png' alt='Killen Civlil Engineering'/></a>
                                    <?php } ?>
                                </div> 
    
                                <div class="project-info">
                                    <h2><a href="<?php the_permalink(); ?>"><?php echo get_the_title($currentid); ?></a></h2>
                                    <p><?php echo get_the_excerpt(); ?></p>
                                     <a href="<?php the_permalink(); ?>" class="info">More  »</a>
                                </div>
                          </div> 
    
                    <?php endwhile; ?>
                  <div class="project-pagination">
    
                      <div class="pagination page-btn"><?php next_posts_link( '« Prev' ); ?></div>
                        <div class="pagination page-btn-1"><?php previous_posts_link( 'Next »' ); ?></div>
    
                  </div>  
    
                <?php
                endif;
                wp_reset_postdata();
    ?>
  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    pagination:

    <div class="pagination page-btn"><?php next_posts_link( '« Prev', $my_query->max_num_pages  ); ?></div>
                        <div class="pagination page-btn-1"><?php previous_posts_link( 'Next »', $my_query->max_num_pages ); ?></div>

    http://wordpress.stackexchange.com/questions/20424/wp-query-and-next-posts-link

  5. MrThunder
    Member
    Posted 1 year ago #

    That worked great thanks!! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags