Cannot redeclare the_posts_navigation() – link-template.php
-
Hello,
I am trying to use WP-PageNavi with the Gazette theme (https://wordpress.org/themes/gazette/).
In Gazette, the calls to
next_posts_link()
andprevious_posts_link()
are located in the file\inc\template-tags.php
(underfunction the_posts_navigation()
). In this file,the_posts_navigation()
is a “pluggable” function (it is enclosed byif ( ! function_exists( 'the_posts_navigation' ) ) :
)So I tried to simply copy the function
the_posts_navigation()
to my child theme and replace the necessary lines in order to use WP-PageNavi. “Sincethe_posts_navigation()
is a pluggable function,” I thought, “I won’t have to do anything else”.Unfortunately, I was wrong. It’s not working.
I use the plugin Code Snippets (https://wordpress.org/plugins/code-snippets/) to manage my custom snippets, and when I try to activate the snippet below…
function the_posts_navigation() { // Don't print empty markup if there's only one page. if ( $GLOBALS['wp_query']->max_num_pages < 2 ) { return; } ?> <nav class="navigation posts-navigation" role="navigation"> <h2 class="screen-reader-text"><?php _e( 'Posts navigation', 'gazette' ); ?></h2> <div class="nav-links"> <?php if ( get_next_posts_link() ) : ?> <?php wp_pagenavi(); ?> <?php endif; ?> <?php if ( get_previous_posts_link() ) : ?> <?php wp_pagenavi(); ?> <?php endif; ?> </div><!-- .nav-links --> </nav><!-- .navigation --> <?php }
I get this error:
The code snippet you are trying to save produced a fatal error on line 22:
Cannot redeclare the_posts_navigation() (previously declared in /[redacted]/public_html/wp-includes/link-template.php:2462)Interestingly, the problem is related to the file
public_html/wp-includes/link-template.php
, not a theme file.What should I do to use WP-PageNavi in my theme?
Thanks in advance.
-
This looks like a theme problem rather than a plugin one. You should contact your theme author about it. Might be the order of the_posts_navigation() loaded incorrect and hence the problem. I am not sure, the best person to ask is your theme author.
Thanks for your prompt answer. I am already waiting for a response from the theme author, but I thought that perhaps the problem wasn’t exactly theme-related, since the error mentions the function in the file
public_html/wp-includes/link-template.php
.I’ll wait for a response from them.
I got this all wrong. This is not a theme problem.
The function
the_posts_navigation
in my theme is actually doing nothing. To confirm that, I removed the function from the theme file, and the posts navigation on my website is still working with no problems.So my website is actually using the function
the_posts_navigation
frompublic_html/wp-includes/link-template.php
, which is a core file.WP-PageNavi instructions says:
In your theme, you need to find calls to next_posts_link() and previous_posts_link() and replace them.
But, in my website, the only file with these calls is
link-template.php
(https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-includes/link-template.php).What should I do to use WP-PageNavi in my site, then?
Thanks in advance.
Find and replace the text in your theme
next_posts_link()
withwp_pagenavi()
?The problem is that
next_posts_link()
is not present in any of my theme files.The only file (in my website) that I could find
next_posts_link()
andprevious_posts_link()
ispublic_html/wp-includes/link-template.php
, which is a core file.Here is how
link-template.php
looks: https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-includes/link-template.phpAnd here is my
index.php
. It simply callsthe_posts_navigation()
.<?php /** * The main template file. * * This is the most generic template file in a WordPress theme * and one of the two required files for a theme (the other being style.css). * It is used to display a page when nothing more specific matches a query. * E.g., it puts together the home page when no home.php file exists. * Learn more: http://codex.wordpress.org/Template_Hierarchy * * @package Gazette */ get_header(); ?> <?php if ( is_home() ) { // Include the featured content template. get_template_part( 'featured-content' ); } ?> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php if ( have_posts() ) : ?> <?php /* Start the Loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php /* Include the Post-Format-specific template for the content. * If you want to override this in a child theme, then include a file * called content-___.php (where ___ is the Post Format name) and that will be used instead. */ get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php the_posts_navigation(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?> </main><!-- #main --> </div><!-- #primary --> <?php get_footer(); ?>
Replace
<?php the_posts_navigation(); ?>
With
<nav class="navigation posts-navigation" role="navigation"> <h2 class="screen-reader-text"><?php _e( 'Posts navigation', 'gazette' ); ?></h2> <div class="nav-links"> <?php wp_pagenavi(); ?> </div><!-- .nav-links --> </nav><!-- .navigation --> <?php
That is probably the best I can do since I am not using your theme. The best person to ask is really your theme author.
This worked! Thanks a lot!
- The topic ‘Cannot redeclare the_posts_navigation() – link-template.php’ is closed to new replies.