Site logo

Archived topic

Nav Links Disappeared

3 replies · Started by Todd on March 31, 2019

Viewing posts 1–4 of 4

I have a webcomic that used to have a nav menu above and below the comic (you guys helped a lot with getting it working). Suddenly, sometime in the last 24 hours or so, the second menu (the one below the webcomic) disappeared. I'm not quite sure what happened, especially since I haven't been fiddling with any of the php or css files recently. Hoping you guys can take a quick look and point me in the right direction. Note, I have this code in my function.php (and haven't edited it in a couple weeks).

edit: One thought I just had is that I recently set up the site to force https. I don't see why that would impact the second nav bar, but I wanted to mention it since that's the only significant thing I've done recently.

<?php

add_action( 'generate_before_main_content', function() {
    if ( function_exists( 'previous_post_link' ) && function_exists( 'next_post_link' ) ) {
		echo '<div class="post-navigation">';
		echo  previous_post_link(); //Older Link using max_num_pages
		echo  next_post_link(); //Newer Link using max_num_pages
		echo '</div>'; 
    }
} );

add_action( 'generate_paging_navigation', function() {
    if ( function_exists( 'previous_post_link' ) && function_exists( 'next_post_link' ) ) {
		echo '<div class="post-navigation">';
		echo  previous_post_link(); //Older Link using max_num_pages
		echo  next_post_link(); //Newer Link using max_num_pages
		echo '</div>'; 
    }
} );

add_filter( 'next_post_link', function( $output, $format, $link, $post ) {
	
	$currentID = get_the_ID();
  	$args = array(
	  	'numberposts' => 1,
	  	'order'    => 'ASC',
	  	'orderby' => 'rand',
	  	'post__not_in' => array( $currentID ),
	);
  
    $random_post = get_posts( $args );
	
    if ( ! $post ) {
       	  $next = sprintf(
        '<a class="next-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $post ),
        $post->post_title
	);

	  $random_post = sprintf(
        '<a class="random-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $random_post[0]->ID ),
        get_the_title( $random_post[0]->ID )
    );
	
	$last_post = get_posts( 'numberposts=1&order=ASC' );

    $last_post = sprintf(
        '<a class="last-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $last_post[0]->ID ),
        get_the_title( $last_post[0]->ID )
    );
	      return $random_post . $next . $last_post;
    }
	
	else {
		  $next = sprintf(
        '<a class="next-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $post ),
        $post->post_title
    );
  
    $random_post = sprintf(
        '<a class="random-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $random_post[0]->ID ),
        get_the_title( $random_post[0]->ID )
    );
    
	$last_post = get_posts( 'numberposts=1&order=ASC' );

    $last_post = sprintf(
        '<a class="last-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $last_post[0]->ID ),
        get_the_title( $last_post[0]->ID )
    );
	   return $random_post . $next . $last_post;
	}
	
}, 10, 4 );

add_filter( 'previous_post_link', function( $output, $format, $link, $post ) {
    if ( ! $post ) {
        $prev = sprintf(
        '<a class="previous-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $post ),
        $post->post_title
    );

 $latest_post = get_posts( 'numberposts=1' );

    $latest_post = sprintf(
        '<a class="latest-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $latest_post[0]->ID ),
        get_the_title( $latest_post[0]->ID )
    );
	
	    return $latest_post . $prev;
    }
  else{
    $prev = sprintf(
        '<a class="previous-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $post ),
        $post->post_title
    );

 $latest_post = get_posts( 'numberposts=1' );

    $latest_post = sprintf(
        '<a class="latest-post" href="%1$s" title="%2$s"></a>',
        get_permalink( $latest_post[0]->ID ),
        get_the_title( $latest_post[0]->ID )
    );
	
    return $latest_post . $prev;
  }
}, 10, 4 );

That's strange.. Instead of:

add_action( 'generate_paging_navigation', function() {
    if ( function_exists( 'previous_post_link' ) && function_exists( 'next_post_link' ) ) {
		echo '<div class="post-navigation">';
		echo  previous_post_link(); //Older Link using max_num_pages
		echo  next_post_link(); //Newer Link using max_num_pages
		echo '</div>'; 
    }
} );

Try this:

add_action( 'generate_after_main_content', function() {
    if ( function_exists( 'previous_post_link' ) && function_exists( 'next_post_link' ) ) {
		echo '<div class="post-navigation">';
		echo  previous_post_link(); //Older Link using max_num_pages
		echo  next_post_link(); //Newer Link using max_num_pages
		echo '</div>'; 
    }
} );

Thanks, Tom. That worked! No hurry on a response, but do you know why that would have fixed it? I always like to understand things in the hopes that I can avoid asking a question in the future. :)

I didn't know for sure, but the generate_after_main_content hook should always exist. generate_paging_navigation only exists if WP thinks it needs it (which it should in your case).

This archived topic is closed to new replies.