• Resolved MrThunder

    (@mrthunder)


    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();
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    (@alchymyth)

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

    Thread Starter MrThunder

    (@mrthunder)

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

    (@alchymyth)

    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>

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

    Thread Starter MrThunder

    (@mrthunder)

    That worked great thanks!! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help with category loop’ is closed to new replies.