Loại bỏ phân trang trong rel=”canonical” Yoast Seo

18 Aug 2020
0 Comments

Lưu ý: Các bạn có thể thêm các loại trang lưu trữ khác thay vì is_home() || is_category() || is_author().

                
                    
<?php
/**
 * WP SEO integration
 *
 * @author      UX Themes
 * @package     Flatsome/Integrations
 * @since       3.7.0
 */

namespace FlatsomeChild\Change\Integrations;

/**
 * Class WP_Seo
 *
 * @package FlatsomeChild\Change\Integrations
 */
class WP_Seo {

    /**
     * Static instance
     *
     * @var WP_Seo $instance
     */
    private static $instance = null;

    /**
     * WP_Seo constructor.
     */
    public function __construct() {
        add_action( 'wp', [ $this, 'integrate' ] );
    }
    /**
     * Setting based integration.
     */
    public function integrate() {
        // Primary term.
        add_filter('wpseo_canonical', [ $this, 'yoast_seo_canonical_presenter' ] );
    }

    /**
     * Removes last crumb in the crumbs array.
     *
     * @param array $crumbs The crumbs array.
     *
     * @return mixed
     */
    public function yoast_seo_canonical_presenter( $canonical) {
        if ( is_home() || is_category() || is_author()) {
            $pattern = '/page\\/[0-9]+\\//i';
            $canonical = preg_replace($pattern, '', $canonical);
        }
        
        return $canonical;
    }

    /**
     * Initializes the object and returns its instance.
     *
     * @return WP_Seo The object instance
     */
    public static function get_instance() {
        if ( ! isset( self::$instance ) ) {
            self::$instance = new self();
        }

        return self::$instance;
    }
}

WP_Seo::get_instance();
                
            

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.