我已经过滤了要添加到站点地图中的URL。我正在使用一个站点地图插件,它有hooks 进一步修改。
我的代码:
// add to theme\'s functions.php
add_filter(\'bwp_gxs_external_pages\', \'bwp_gxs_external_pages\');
function bwp_gxs_external_pages($pages)
{
return array(
array(\'location\' => home_url(\'www.example.com/used-cars/location/new-york/model/bmw\'), \'lastmod\' => \'27/03/2017\', \'frequency\' => \'auto\', \'priority\' => \'1.0\'),
array(\'location\' => home_url(\'www.example.com/used-cars/location/los-angeles/model/aston-martin\'), \'lastmod\' => \'27/03/2017\', \'frequency\' => \'auto\', \'priority\' => \'0.8\')
array(\'location\' => home_url(\'www.example.com/used-cars/model/mercedes-benz\'), \'lastmod\' => \'27/03/2017\', \'frequency\' => \'auto\', \'priority\' => \'0.8\')
);
}
正如你在我的代码中看到的,我有这样的URL
www.example.com/used-cars/location/new-york/model/bmw
&;
www.example.com/used-cars/model/mercedes-benz
所以我的问题是,有数千个这样的URL,我想把它们都推到这个网站地图上。
所以我的问题是,难道没有一种方法可以循环使用它们吗?而不是像这样一个接一个地插入代码
array(\'location\' => home_url(\'www.example.com/used-cars/model/aston-martin\'), \'lastmod\' => \'27/03/2017\', \'frequency\' => \'auto\', \'priority\' => \'0.8\')
最合适的回答,由SO网友:brianjohnhanna 整理而成
这是一个尝试。我不确定URL中的分类法是什么,甚至不确定分类法是什么,但基本上,您只需通过数组循环添加到页面数组的所有内容。您需要在一个数组中定义所有模型,或者使用以下方法从数据库中获取它们get_option()
;
add_filter( \'bwp_gxs_external_pages\', \'bwp_gxs_external_pages\', 10, 1 );
function bwp_gxs_external_pages($pages)
{
$models = array( \'aston-martin\', \'bmw\', \'mercedes-benz\' /*...*/ );
$locations = get_terms( array(
\'taxonomy\' => \'location\',
\'hide_empty\' => true,
) );
// Loop through the search terms
foreach ( $models as $model ) {
foreach ( $locations as $location ) {
$pages[] = array(
\'location\' => home_url( \'/used-cars/location/\' . $location->slug . \'/model/\' . $model ),
\'lastmod\' => \'27/03/2017\',
\'frequency\' => \'auto\',
\'priority\' => \'0.8\'
);
}
$pages[] = array(
\'location\' => home_url( \'/used-cars/model/\' . $model ),
\'lastmod\' => \'27/03/2017\',
\'frequency\' => \'auto\',
\'priority\' => \'0.8\'
);
}
return $pages;
}
希望这能让你开始。