Solved

how to put canonical tag on wordpress category page?

Posted on 2011-09-09
11
260 Views
Last Modified: 2012-06-27
Hey,

does anybody know how to put a canonical tag on wordpress blog category page?

currently the urls are like below, which is creating a duplicate content issues in webmaster tool...

site.com/blog/category/name1
site.com/blog/category/name2
and so on..

These all urls have same meta title and content, I think putting canonical on this page should resolve the problem?

Thanks
0
Comment
Question by:david_php
  • 6
  • 4
11 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36515164
To ensure that every page uses a unique URL, you should put this code after the opening <header> tag and before it closes:
<?php 
    if ( is_singular() ) 
    echo '<link rel="canonical" href="' . get_permalink() . '" />'; 
?>

Open in new window


There may be additional steps required depending on your permalink settings or the .htaccess file (if you;re on Linux Server, and it's being utilized).
0
 

Author Comment

by:david_php
ID: 36534221
sorry where exactly? in which file? and i think if i add this code manually, then it will be overwritten whenever blog is upgraded next time?
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36534582
It would go in the header.php file. Notice where I said

"To ensure that every page uses a unique URL, you should put this code after the opening <header> tag and before it closes:"


You can create a child theme to avoid it being over-written. To create a child theme you only need to create two files for now, the header.php file and a style.css file.

The style.css file would require this in the top of the file:
/* 
Theme Name: Conanical
Theme URI: http://www.yoursite.com/
Description: Not Required
Author: YOU
Author URI: Your URL
Template: twentyten
Version: 1.1
Tags: fixed
*/

/*Nothing required here, but is the optimal place for making edits to CSS so it doesn't get overwritten.*/

Open in new window


Just be sure to replace twentyten with the exact theme name for your current theme.

The header.php file would be your header.php copied and pasted into the new file. Then replace the code between the <title>THE CODE I SHOW ABOVE GOES HERE</title>. Save the file as header.php. Now create a folder and name it whatever. Place both the new files in this folder and upload it to the theme directory. Activate the theme. Now changes won't be overwritten.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36534592
CORRECTION:


The code doesn't go in the title tags, it goes just after the closing </title> tag.
EXAMPLE:
<title>SOME TITLE STUFF HERE</title>
<?php 
    if ( is_singular() ) 
    echo '<link rel="canonical" href="' . get_permalink() . '" />'; 
?>

Open in new window

0
 

Author Comment

by:david_php
ID: 36543498
it din't work, any idea why?
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36544289
How are your permalinks structured? If you go to Settings>Permalinks, what do you have them set to?
0
 

Author Comment

by:david_php
ID: 36547482
custom structure = /%postname%/
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36555105
I'm not sure why it's not working for you. Maybe you are better off going with a plug-in. All in One SEO is a very good Plug-in. It will help with more than your titles.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36557153
Try this for the title tag in the header.php file:
<title><?php if (is_home() ) { 
  bloginfo( 'name' ); 
  }
elseif (is_category( ) ) { 
  single_cat_title( ) ; echo ' - ' ; 
  bloginfo( 'name' ) ; 
  }
elseif (is_single() ) { 
  single_post_title() ; 
  }
elseif (is_page( ) ) { 
  bloginfo('name' ) ; echo ' : ' ; 
  single_post_title() ; 
  } else { 
  wp_title( '' , true ); 
  } 
?></title>

Open in new window

0
 
LVL 31

Accepted Solution

by:
gwkg earned 500 total points
ID: 36582877
It didn't work because jeremy inadvertently used is_singular() instead of is_category().

That said, I don't think using a canonical link is a solution.  You need to make unique titles and descriptions for each category.

I prefer the SEO Plugin from Yoast.com http://yoast.com/wordpress/canonical/ 
0
 

Author Closing Comment

by:david_php
ID: 36947582
ok thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are looking for plug-ins to add functions to your WordPress small business web site, take some time to read though this comprehensive list.  These are all the plugins I use for my customers WordPress web sites, as well as my own.  Be sure to …
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question