帮助编写一个定制的php脚本来设置页面或帖子的最高优先级

时间:2016-04-10 作者:Emanuele

我正在努力改进本教程script 包括switch 在第一个if指定自定义的条件priority 仅限于某些帖子或页面。

所以我想告诉Yoastif some pages have certain id the priority have to change, 但我的脚本将0.5值分配给所有页面。

有什么帮助吗?

add_filter( \'wpseo_xml_sitemap_post_priority\', \'my_custom_post_xml_priority\', 10, 3 );

function my_custom_post_xml_priority( $return, $type, $post) {

if($type == \'page\') {
switch ($post) {
case \'8\':
case \'395\':
case \'342\':
$return = 0.9;
case \'5\':
$return = 1.00;
case \'620\':
case \'703\':
case \'603\':
case \'688\':
case \'695\':
case \'614\':
case \'684\':
case \'639\':
case \'628\':
case \'539\':
case \'542\':
case \'521\':
case \'509\':
case \'517\':
case \'528\':
case \'533\':
case \'536\':
case \'548\':
case \'545\':
case \'525\':
case \'551\':
$return = 0.7;
break;
default:
$return = 0.5;
break;
}
}

elseif ($type == \'post\') {
$return = 0.8;
}

return $return;
}

1 个回复
最合适的回答,由SO网友:Mirko 整理而成

解决方案应该是:

add_filter( \'wpseo_xml_sitemap_post_priority\', \'my_custom_post_xml_priority\', 10, 3 );

function my_custom_post_xml_priority( $return, $type, $post) {

    if($type == \'page\') {
        switch ($post->ID) {
        case \'8\':
        case \'395\':
        case \'342\':
            $return = 0.9;
            break;
        case \'5\':
            $return = 1.00;
            break;
        case \'620\':
        case \'703\':
        case \'603\':
        case \'688\':
        case \'695\':
        case \'614\':
        case \'684\':
        case \'639\':
        case \'628\':
        case \'539\':
        case \'542\':
        case \'521\':
        case \'509\':
        case \'517\':
        case \'528\':
        case \'533\':
        case \'536\':
        case \'548\':
        case \'545\':
        case \'525\':
        case \'551\':
            $return = 0.7;
            break;
        default:
            $return = 0.5;
            break;
        }
    } elseif ($type == \'post\') {
        $return = 0.8;
    }

    return $return;
}
我认为$post 是一个WP_Post object, 然后,您必须比较交换机上的属性ID。