Self-referencing canonical | WordPress.org

[ad_1]

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="https://projectdmc.org/support/topic/self-referencing-canonical-2/<?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]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer