Site logo

Archived topic

Customizing next/previous links in CPT w/o affecting archive

15 replies · Started by wekhter on September 7, 2017

Viewing posts 1–15 of 16

Hi, I adapted this gist: https://gist.github.com/generatepress/db03ed9158a57799a20e to customize the next/prev links for one of my CPTs.

That worked great, but I just realized that it's changed the paging navigation in my archives from the normal "1 2 ... 4 Next" navigation to Older/newer posts.

How do I a) keep the custom next/prev links on my CPT while b) leaving archive navigation alone? I never formally learned PHP so I have trouble with if : endif syntax (as opposed to if{}) and I think that's getting in my way

What exactly did you need to customize? Overwriting the entire function like that probably isn't the best method.

Let me know :)

Hi Tom, basically I just needed to customize the next/previous links on one CPT (named 'programpage'). I was also using this to remove next/previous links on posts. Basically, I need the next/previous links customized and also need those links to not show up on any other post type.

@Tom Well, here goes:

programpage is basically a lesson in our psuedo-LMS. I set it up so that we could have automatic next/previous buttons for lessons, but needed to customize the text so that it 1) doesn't include the content title of the next/previous lessons and 2) has some extra CSS classes for styling purposes. I also set it up to disable either next, previous, or both links on a per-lesson basis (we have lessons in different courses that aren't supposed to link to each other).

I set all that up based on this gist: https://gist.github.com/generatepress/db03ed9158a57799a20e w/ relevant changes below.

		<?php elseif ( is_singular( 'programpage' ) ) : ?>

			<h6 class="screen-reader-text"><?php _e( 'Post navigation', 'generatepress' ); ?></h6>
			<?php if ( get_field( 'lesson_links' ) == 'next_only' ):
  				// null
  				else:
		 		previous_post_link( '<div class="nav-previous">%link</div>', '<span class="lesson-nav">Previous</span>' );
  				endif; ?>
			<?php if ( get_field( 'lesson_links' ) == 'previous_only' ):
  				// null
  				else:
  				next_post_link( '<div class="nav-next">%link</div>', '<span class="lesson-nav">Next</span>' );
  				endif; ?>

The problem I'm having is that it's also affecting CPT archives. I just want the normal "1, 2 ... 5, 6" pagination at the bottom of archives, but it's showing up as newer/older posts. I tried to get rid of the bottom section of the code in that gist but clearly I'm going about it wrong because it's not working.

Is there any way you can show me your entire function? Either in your own gist or a pastebin?

@Tom Here is the snippet I'm using to change the text: https://pastebin.com/A2sJJf1z

I know that the last section is overriding the archive pagination but every time I've tried to remove that section it rendered the code invalid so I'm clearly doing something wrong there.

Hmm, your code doesn't look like it should be removing pagination on archives. Can you link me to an archive page possible so I can take a look at the code?

@Tom There is pagination, but it's not the kind I want. Here's a link to the archive on a site I use for testing: http://unsightly-gerbil.w3.poopy.life/animation/

Note: I'm totally aware that the bottom looks blank because of CSS, but that's not the issue I'm having here.

What I have right now w/ the CSS disabled:

Screenshot_21

What I actually want:

Screenshot_22

Hmm, try replacing this line:

max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>

With this:

<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() || is_post_type_archive() ) ) : // navigation links for home, archive, and search pages ?>

@Tom Didn't work, but I finally got it working--I had <?php the_posts_navigation(); ?> when I needed<?php generate_paging_nav(); ?>

Thanks for your help!

Aha! Glad you found it :)

@Tom I know it's been a few days, but I realize that I'm seeing the following PHP notice in my debug.log:

PHP Notice: generate_paging_nav is <strong>deprecated</strong> since version 1.3.45! Use the_posts_navigation() instead.

As I said above, it turned out that the_posts_navigation() was my problem, since it showed "Older/Newer" navigation instead of the "1, 2 ... 5, Next" archive navigation I wanted. Is there something I should be using instead of generate_paging_nav?

I also saw this (unrelated) PHP notice in my debug.log:

PHP Notice:  Undefined index: merge in /wp-content/plugins/gp-premium/page-header/functions/page-header.php on line 170
PHP Notice:  Undefined index: merge in /wp-content/plugins/gp-premium/page-header/functions/page-header.php on line 172

@Tom I'm using GP Premium 1.4.3 which is the most recent.

Using the latest version of the function from what you linked, it's still the same issue where there is no numeric pagination on the archives. Actually, it looks like there's no navigation whatsoever on the archives if I look in the source code.

This archived topic is closed to new replies.