Solved

how to put canonical tag on wordpress category page?

Posted on 2011-09-09
11
259 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I want to start by talking about the use of plug-ins for WordPress. I started a web-site for a company I was working for a few years ago; I had extremely basic knowledge of HTML. I am a Graphic Designer by trade so I invited the opportunity as a cha…
Transferring a wordpress site from a host or local dev server to another host can be a pain. So I've included my steps on how I have accomplished this task. Steps include an assumption that you have Cpanel access or Ftp access.. If you do not hav…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now