Archived topic
Double pagination on post
3 replies · Started by Todd on May 2, 2019
Hi Tom / all--
You've helped a bit on this is in the past, so hopefully you have an idea here. I'm not sure when it broke, unfortunately, and I can't seem to figure out the fix. My blog is a webcomic, where it's important to have pagination above and below the post. I have this, but I somehow now have double pagination below the post.
If you go to the main page of the site, you'll see the pagination is correct. But if you click any of the pagination links (which takes you to a post) you'll see the double pagination below the post.
This is what's in my child theme functions.php:
<?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_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>';
}
} );
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' );
$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' );
$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&order=ASC' );
$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&order=ASC' );
$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 );
Thanks!
Hi there,
Try this CSS:
.inside-article .post-navigation {
display: none;
}
Let me know :)
Thanks, Tom. That worked when I stuck it at the end of my CSS (though not at the beginning). So, I need to go through some of my spaghetti styling and figure out what is happening exactly. :)
This tool might help: https://jigsaw.w3.org/css-validator/
Glad I could help :)