issue canonical
-
Hi in category page i don’t have canonical
how can i add canonical in category page ?
best regards
-
Hi
I would recommend to use Yoast SEO (it allows to set-up canonical tags), but alternatively you can use this code snippet (you should paste it in theme functions.php file):
function bis_canonical_url() { global $wp; $object = get_queried_object(); // Get current page if(!empty($wp->query_vars['paged'])) { $page = $wp->query_vars['paged']; } else if (!empty($wp->query_vars['page'])) { $page = $wp->query_vars['page']; } else { $page = 1; } if(!empty($object->taxonomy)) { $url = trim(get_term_link($object, $object->taxonomy), "/"); $url = ($page > 1) ? "{$url}/page/{$page}" : $url; } else if(!empty($object->post_type)) { $url = trim(get_permalink($object->ID), "/"); $url = ($page > 1) ? "{$url}/{$page}" : $url; } echo (!empty($url)) ? sprintf("<link rel=\"%s\" href=\"%s\" />\n", esc_attr('canonical'), user_trailingslashit(esc_url($url))) : ""; } add_action('wp_head', 'bis_canonical_url', 3);Best regards,
MaciejHi Maciej thanks for your message i have put in functions.php in my theme this function but nothing changes
best regards
hi sorry i have this alert This post has been held for moderation by our automated system. It will be reviewed within 72 hours.can you see my url ?
best regards
As far as I can see, the canonical tag is present:
http://prntscr.com/ibgsiayes but if i go to post or category i have a slash at the end of url in canonical
best regards
Please replace:
echo (!empty($url)) ? sprintf("<link rel=\"%s\" href=\"%s\" />\n", esc_attr('canonical'), user_trailingslashit(esc_url($url))) : "";with:
echo (!empty($url)) ? sprintf("<link rel=\"%s\" href=\"%s\" />\n", esc_attr('canonical'), esc_url($url)) : "";Hi i have replace but no change
best regards
What version of Permalink Manager do you use?
Could you try to replace the first snippet with this one:
function bis_filter_canonical_links($link) { return preg_replace("/(\.[a-z]{3,4})\/$/i", "$1", $link); } add_filter('wpseo_canonical', 'bis_filter_canonical_links');Regards,
Maciej-
This reply was modified 8 years, 4 months ago by
Maciej Bis.
Version 2.0.5.6
thanks i have put this and now it’s good on post page thanks but not category
<?php
// Dev mode enabled
// Use this for uncompressed custom css codes
//if ( ! defined( ‘BF_DEV_MODE’ ) ) {
// define( ‘BF_DEV_MODE’, TRUE );
//}
function wpseo_canonical_home_url_fix( $canonical_url ) {
// Add a trailing slash if it’s missing
return trailingslashit( $canonical_url );
}
add_filter( ‘wpseo_canonical’, ‘wpseo_canonical_home_url_fix’ );
function bis_filter_canonical_links($link) {
return preg_replace(“/(\.[a-z]{3,4})\/$/i”, “$1”, $link);
}
add_filter(‘wpseo_canonical’, ‘bis_filter_canonical_links’);
function bis_canonical_url() {
global $wp;$object = get_queried_object();
// Get current page
if(!empty($wp->query_vars[‘paged’])) {
$page = $wp->query_vars[‘paged’];
} else if (!empty($wp->query_vars[‘page’])) {
$page = $wp->query_vars[‘page’];
} else {
$page = 1;
}if(!empty($object->taxonomy)) {
$url = trim(get_term_link($object, $object->taxonomy), “/”);
$url = ($page > 1) ? “{$url}/page/{$page}” : $url;
} else if(!empty($object->post_type)) {
$url = trim(get_permalink($object->ID), “/”);
$url = ($page > 1) ? “{$url}/{$page}” : $url;
}echo (!empty($url)) ? sprintf(“<link rel=\”%s\” href=\”%s\” />\n”, esc_attr(‘canonical’), esc_url($url)) : “”;
}
add_action(‘wp_head’, ‘bis_canonical_url’, 3);The other snippet overwrites my snippet, please replace the whole code with this:
// Dev mode enabled // Use this for uncompressed custom css codes //if ( ! defined( ‘BF_DEV_MODE’ ) ) { // define( ‘BF_DEV_MODE’, TRUE ); //} function wpseo_canonical_home_url_fix($canonical_url) { // Add a trailing slash if it’s missing $canonical_url = trailingslashit($canonical_url); return preg_replace(“/(\.[a-z]{3,4})\/$/i”, “$1”, $link); } add_filter( ‘wpseo_canonical’, ‘wpseo_canonical_home_url_fix’ );-
This reply was modified 8 years, 4 months ago by
Maciej Bis.
thanks i have remplace the whole code
and now i have https://cl.ly/1j322i0E2X1K
it’s seems to be good for post and categories but not for home page https://cl.ly/0U1a463E2l0j
best regards
-
This reply was modified 8 years, 4 months ago by
The topic ‘issue canonical’ is closed to new replies.