• Resolved seiyar2603

    (@seiyar2603)


    Hi in category page i don’t have canonical

    how can i add canonical in category page ?

    best regards

Viewing 15 replies - 1 through 15 (of 28 total)
  • Plugin Author Maciej Bis

    (@mbis)

    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,
    Maciej

    Thread Starter seiyar2603

    (@seiyar2603)

    Hi Maciej thanks for your message i have put in functions.php in my theme this function but nothing changes

    best regards

    Plugin Author Maciej Bis

    (@mbis)

    Hi @seiyar2603,

    can you provide me with your website address?

    Best regards,
    Maciej

    Thread Starter seiyar2603

    (@seiyar2603)

    Thread Starter seiyar2603

    (@seiyar2603)

    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

    Plugin Author Maciej Bis

    (@mbis)

    Hi @seiyar2603,

    no, please send it via email to contact /at/ maciejbis.net

    Regards,
    Maciej

    Thread Starter seiyar2603

    (@seiyar2603)

    https://cl.ly/310f1q1Q0I0x

    (you can see my url on screenshot)

    best regards

    Plugin Author Maciej Bis

    (@mbis)

    As far as I can see, the canonical tag is present:
    http://prntscr.com/ibgsia

    Thread Starter seiyar2603

    (@seiyar2603)

    yes but if i go to post or category i have a slash at the end of url in canonical

    https://cl.ly/1d1C3D3T2O0m

    best regards

    Plugin Author Maciej Bis

    (@mbis)

    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)) : "";

    Thread Starter seiyar2603

    (@seiyar2603)

    Hi i have replace but no change

    best regards

    Plugin Author Maciej Bis

    (@mbis)

    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.
    Thread Starter seiyar2603

    (@seiyar2603)

    Version 2.0.5.6

    thanks i have put this and now it’s good on post page thanks but not category

    https://cl.ly/0N391p452e0A

    <?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);

    Plugin Author Maciej Bis

    (@mbis)

    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.
    Thread Starter seiyar2603

    (@seiyar2603)

    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

Viewing 15 replies - 1 through 15 (of 28 total)

The topic ‘issue canonical’ is closed to new replies.