• Resolved risu89

    (@risu89)


    Hey, I have a problem with adding self-referencing canonical on a blog subpage. I tried different options. Plugins like Yoast, Rank Math, do not work, i.e. the option with canonical is active but canonical links are not added to the page code. I have tested various options and the only one that fairly works is to add the following code in the header.php:

    <?php
    	   if ( is_front_page() ) {
    		  $canonical_url = get_home_url();
    		  if ( ! is_main_site() ) { // add trailing slash for subsite home directories
    			 $canonical_url = $canonical_url . '/';
    		  }
    	   } else {
    		  $canonical_url = get_permalink();
    	   }
    	?>
    	<link rel="canonical" href="<?php echo $canonical_url ?>" />

    The problem with this solution is that the blog subpage or categories from the blog as canonical have… the only article that has been published on the blog.

    I have run out of ideas and I don’t know what to do about it. I would prefer to avoid installing any more plugins.

    I would appreciate any help or guidance on what I can do to make it work.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Since version 6.0.3 wordpress has pagination issue with all SEO plugins or all plugins are wrong or wordpress is the cause. This issue prevents some plugins from being reconfigured and also causes a very significant server overhead when /page/v2 appears on the front page. I have solved it on many websites by inserting this in functions.php:

    function temporarily_disable_pagination( $query ) {
        if ( $query->is_main_query() && !is_admin() ) {
            $query->set( 'nopaging', true );
        }
    }
    add_action('pre_get_posts','temporarily_disable_pagination');
    

    until someone fixes the bug in wordpress or seo plugins.

    Note. When inserting this function if in permalinks you have for example: /%category%/%postname%/ the function automatically changes it to /%postname%/ so you will have to enter quickly and put it back correctly: /%category% / %postname%/ to avoid SEO problems with search engine indexing.

    • This reply was modified 1 year, 11 months ago by tseo.pro.

    Another workaround to not completely remove pagination and only work on archive.php and category.php but not on the rest of wordpress:

    function disable_pagination_on_other_pages( $query ) {
        if ( ! is_admin() && ( is_archive() || is_category() ) && $query->is_main_query() ) {
            return;
        }
        if ( $query->is_main_query() ) {
            $query->set( 'no_found_rows', true );
        }
    }
    add_action('pre_get_posts','disable_pagination_on_other_pages');
    
    Thread Starter risu89

    (@risu89)

    Hey, thanks for the reply!
    I’m not convinced that this is the problem. I previously had WordPress 5.4, I just upgraded to a newer version a while ago. I also updated all plugins and nothing helped.

    I think I found the solution. It’s not perfect, but it works ??

    	<?php
    	   if ( is_front_page() ) {
    		  $canonical_url = get_home_url();
    	   } else {
    		  $canonical_url = get_permalink();
    	   }
    
    		/*if(is_category()) {
        		$categories = get_the_category();
       			 	$category_id = $categories[0]->term_id;
        		echo '<link rel="canonical" href="' .  get_category_link($category_id) . '" />';
    		}*/
    		if ( is_singular() ) { ?> <link rel="canonical" href="<?php the_permalink(); ?>" /> <?php } 
    		if ( is_archive() || is_home() || is_category() ) { ?> <link rel="canonical" href="domain-URL<?php echo $_SERVER['REQUEST_URI'];?>"> <?php } 
    	?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Self-referencing canonical’ is closed to new replies.