WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem with previous/next links for my page of posts [catchbox] (14 posts)

  1. hrcwebmaster
    Member
    Posted 1 year ago #

    I created a new page template for my page of posts. I used this code to pull the desired posts in this page ("Newsroom"):

    <?php
        $args=array(
                    'cat' => 7,
                    'orderby' =>'post_date',
                    'order' => 'DESC',
        );
    $my_query = new WP_Query($args);
    
        if( $my_query->have_posts() ) {
    
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <br /><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a the_title_attribute(); ?></a></strong>
    <div style="text-align:right;"><em><?php echo get_the_date(); ?></em></div>
    <?php the_excerpt(); ?></p>
    
       <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    </div><!-- extra padding -->
    
    <div class="nextprev">
     <span class="prev"><?php previous_post('&lsaquo; %', '', 'yes', 'no'); ?></span>
     <span class="next"><?php next_post('% &rsaquo;', '', 'yes', 'no'); ?></span>
     </div>
    
    				<?php endwhile; ?>		
    
    			<?php else : ?>
    
    				<article id="post-0" class="post no-results not-found">
    					<header class="entry-header">
    						<?php _e( 'Nothing Found', 'catchbox' ); ?>
    					</header><!-- .entry-header -->
    
    					<div class="entry-content">
    						<h1 class="entry-title"><p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'catchbox' ); ?></p></h1>
    						<?php get_search_form(); ?>
    					</div><!-- .entry-content -->
    				</article><!-- #post-0 -->
    
    			<?php endif; ?>

    I am using the Catchbox theme, and I found this (in the parent search template):
    <?php catchbox_content_nav( 'nav-below' ); ?>
    which I believe is supposed to create previous/next links, if there are more than 1 page.
    However, those previous/next links aren't showing up (nothing is), so I'm wondering if the code I used to pull the posts is preventing the page from thinking there is more than one page of these posts.
    Oh, and this http://codex.wordpress.org/Next_and_Previous_Links did not help because it takes you literally to the next page ("About" or "Contact Us"), rather than to the next set of posts.
    ANY help would be appreciated - I am racking my brain and wasting a LOT of time!!

  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    Placing the recommended PHP into these page templates (and removing the built in themes pagination) by using the popular plugin WP-PageNavi has been very useful to me...

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

  4. hrcwebmaster
    Member
    Posted 1 year ago #

    Seacoast - I'm trying to avoid more plugins, but if it comes down to it I will check that one out, thank you!

    Alchymyth - I tried <?php previous_posts_link(); ?><?php next_posts_link(); ?> before and after the <?php endwhile; ?> and neither worked (displayed nothing). From that page you linked, I found <?php echo get_previous_posts_link(); ?><?php echo get_next_posts_link(); ?> but those also displayed nothing (again, tried placing before then after the <?php endwhile; ?>).

    Still stumped...

  5. Jose Castaneda
    Member
    Posted 1 year ago #

    It could be that I'm still fairly new to WordPress and how the loop works but do I see a second endwhile and i'm wondering if you are using wp_reset_postdata() too soon if it's supposed to be the second endwhile.

  6. hrcwebmaster
    Member
    Posted 1 year ago #

    I see what you mean - though the link you provided had two queries and two endwhile lines.
    I tried:
    -adding the reset code down to the second endwhile and got an error for my template (unexpected T_ENDWHILE)
    -deleted the second endwhile and got an error (unexpected T_ELSE)
    -moved the <?php previous_posts_link(); ?><?php next_posts_link(); ?> above the first endwhile and got no links (but didn't get an error)

    I'm very new to WordPress and the Loop as well; I'm really playing a puzzle blind at this point...

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

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

    also, integrate the 'paged' parameter into the query args;

    'paged' => get_query_var('paged')

    please post the full code of the template for any further suggestions.

  8. hrcwebmaster
    Member
    Posted 1 year ago #

    Ok I tried the 'paged' line you suggested, as well as the max num pages suggested on the link you provided. The large code replacement suggested on that page doesn't exactly work with what I have in my code, so I didn't know how to translate it to my code.
    Here's my entire php template file:

    <?php
    
    /**
    * Template Name: Newsroom
    * The newsroom template file.
    * This is the template file used for displaying the main newsroom page.
    * @package Catch Themes
    * @subpackage Catch_Box
    */
    
    get_header(); ?>
    </div><!-- site-logo -->
    
    <div id="content100" div style="text-align:left;">
    <h1 class="entry-title">Newsroom <hr></h1>
    
    <?php if ( have_posts() ) : ?>
    
    				<?php /* Start the Loop */ ?>
    				<?php while ( have_posts() ) : the_post(); ?>
    
    <div><?php dynamic_sidebar('Opinions'); ?></div>
    <div style="padding: 0.1em 0% 3em;">				
    
    <?php
        $args=array(
    		'cat' => 7,
    		'orderby' =>'post_date',
    		'order' => 'DESC',
    		'posts_per_page' => 10,
    		'paged' => get_query_var('page')
        );
    $my_query = new WP_Query($args);
    
        if( $my_query->have_posts() ) {
    
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <br /><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a the_title_attribute(); ?></a></strong>
    <div style="text-align:right;"><em><?php echo get_the_date(); ?></em></div>
    <?php the_excerpt(); ?></p>
    
       <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    </div><!-- extra padding -->
    
    <?php next_posts_link(); ?> 
    
    				<?php endwhile; ?>
    
    			<?php else : ?>
    
    				<article id="post-0" class="post no-results not-found">
    					<header class="entry-header">
    						<?php _e( 'Nothing Found', 'catchbox' ); ?>
    					</header><!-- .entry-header -->
    
    					<div class="entry-content">
    						<h1 class="entry-title"><p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'catchbox' ); ?></p></h1>
    						<?php get_search_form(); ?>
    					</div><!-- .entry-content -->
    				</article><!-- #post-0 -->
    
    			<?php endif; ?>
    
    </div><!-- #content100 -->
    
    <?php previous_posts_link(); ?><?php next_posts_link(); ?> 
    
    		</div><!-- #primary -->
    <?php get_footer(); ?>

    I appreciate your help!!

  9. hrcwebmaster
    Member
    Posted 1 year ago #

    Any ideas out there? I am still looking...

  10. Catch Themes
    Member
    Posted 1 year ago #

    check this code

    <?php
    
    /**
    * Template Name: Newsroom
    * The newsroom template file.
    * This is the template file used for displaying the main newsroom page.
    * @package Catch Themes
    * @subpackage Catch_Box
    */
    
    get_header(); ?>
    </div><!-- site-logo -->
    
    	<div id="content100" div style="text-align:left;">
    
            <div><?php dynamic_sidebar('Opinions'); ?></div>
    
            <div style="padding: 0.1em 0% 3em;">				
    
                <?php
                    $temp = $wp_query; //save old query
                    $wp_query= null; //clear $wp_query
    
                    $args=array(
                        'cat' => 1,
                        'orderby' =>'post_date',
                        'order' => 'DESC',
                        'posts_per_page' => 10
                    );
                    $wp_query = new WP_Query($args);
    
                    if( $wp_query->have_posts() ) {
    
                        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                            <br />
                            <strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong>
                            <div style="text-align:right;"><em><?php echo get_the_date(); ?></em></div>
                            <p><?php the_excerpt(); ?></p>
    
                        <?php endwhile; ?>
    
                        <?php previous_posts_link(); ?><?php next_posts_link(); ?> 
    
                    <?php
                    } //if ($my_query)
                    wp_reset_postdata();
                ?>
            </div><!-- extra padding -->
    
    	</div><!-- #content100 -->
    
    </div><!-- #primary -->
    
    <?php get_footer(); ?>
  11. hrcwebmaster
    Member
    Posted 1 year ago #

    It worked!!!! I cannot thank you enough! Thank you so much Sakin!!

  12. hrcwebmaster
    Member
    Posted 1 year ago #

    I spoke too soon. I was so excited that it showed "previous" and "next" links, but didn't realize that when I click on "next," it basically refreshes the page and adds "previous" to it. It doesn't actually pull the next page of posts: http://hrc.wastateleg.org/news/. I pasted in your code above, I only changed the "cat" number, because it was supposed be 7 and not 1.
    Sorry, can you help me again??

  13. Catch Themes
    Member
    Posted 1 year ago #

    Oh Sorry I missed it. I tested the code and it is working for me. Add the code as below:

    <?php
    
    /**
    * Template Name: Newsroom
    * The newsroom template file.
    * This is the template file used for displaying the main newsroom page.
    * @package Catch Themes
    * @subpackage Catch_Box
    */
    
    get_header(); ?>
    </div><!-- site-logo -->
    
    	<div id="content100" div style="text-align:left;">
    
            <div><?php dynamic_sidebar('Opinions'); ?></div>
    
            <div style="padding: 0.1em 0% 3em;">				
    
                <?php
                    $temp = $wp_query; //save old query
                    $wp_query= null; //clear $wp_query
    
                    $args=array(
                        'cat'				=> 7,
                        'orderby'			=>'post_date',
                        'order'				=> 'DESC',
                        'posts_per_page'	=> 10,
    					'paged'				=> $paged
                    );
                    $wp_query = new WP_Query($args);
    
                    if( $wp_query->have_posts() ) {
    
                        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                            <br />
                            <strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong>
                            <div style="text-align:right;"><em><?php echo get_the_date(); ?></em></div>
                            <p><?php the_excerpt(); ?></p>
    
                        <?php endwhile; ?>
    
                        <?php previous_posts_link(); ?><?php next_posts_link(); ?> 
    
                    <?php
                    } //if ($my_query)
                    wp_reset_postdata();
                ?>
            </div><!-- extra padding -->
    
    	</div><!-- #content100 -->
    
    </div><!-- #primary -->
    
    <?php get_footer(); ?>
  14. hrcwebmaster
    Member
    Posted 1 year ago #

    Yes, that definitely worked. Thank you again, so much. I have been struggling with this for weeks!

Topic Closed

This topic has been closed to new replies.

About this Topic