<?php if ( is_single() || is_page() ) { ?>
<link rel="canonical" href="<?php the_permalink(); ?>" />
<?php } ?>
Occurs to me now that you’d have to hardcode main url, then get the slug and other url info dynamically:
<?php if ( is_single() || is_page() ) { ?>
<link rel="canonical" href="http://mainurl.com/<?php echo the_time('Y'); ?>/<?php echo the_time('m'); ?>/<?php echo the_time('d'); ?>/<?php echo $post->post_name; ?>" />
<?php } ?>
There is probably a prettier and/or more efficient way to do that, but can’t think of one at the moment.
Update: Just read some more about the tag and realized 2nd method (hardcode url) will not work in the way I was thinking. I thought it would work for sites with more than one domain name pointed at them, but doesn’t look like that is possible:
From here: http://ysearchblog.com/2009/02/12/fighting-duplication-adding-more-arrows-to-your-quiver/
“A tag can only point to a canonical URL form within the same domain and not across domains. For example, a tag on http://test.example.com can point to a URL on http://www.example.com but not on http://yahoo.com or any other domain.”
In that case, the first way (permalink) is probably better. In the event that pretty permalinks change to something other than the index.php?p=123 type, the 2nd way will produce links to 404 pages.
Hi Lori-
Thanks for this. I copied it word for word in my post, with attribution. Please let me know if that’s OK/ not OK.
it is ok