php for full url yoast


php for full url yoast

我是php新手。我正在使用Wordpress和yast。

但是,yoast插件没有正确地编写规范。我希望编写规范,使其使用语法

<link rel="canonical" href="https://www.travelideology.com/about/">

然而,它正在写

<link rel="canonical" href="https:/about/">

如果有人能让我知道我的代码有什么问题,我会很高兴。

代码如下:

function my_wpseo_canonical( $canonical ) {
    global $post;
    $attachment = get_post( $post->ID );
    $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    $cleansed = $protocol . "://" . $_SERVER['HTTP_HOST'];
    $cleansed2 = get_permalink($attachment);
    $canonical = $cleansed . $cleansed2;
    return $canonical; 
}
add_filter( 'wpseo_canonical', 'my_wpseo_canonical' );

try

function my_wpseo_canonical( $canonical ) {
global $post;
$attachment = get_post( $post->ID );
    $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$cleansed = $protocol . "://" .site_url(). $_SERVER['HTTP_HOST'];
$cleansed2 = get_permalink($attachment);
$canonical = $cleansed . $cleansed2;
return $canonical; 
    }