gpt4 book ai didi

php - Wordpress Yoast 从 XML 站点地图中排除帖子

转载 作者:行者123 更新时间:2023-12-04 15:52:58 25 4
gpt4 key购买 nike

我正在使用 Yoast SEO 插件,我正在尝试使用“wpseo_sitemap_entry”过滤器从帖子 XML 站点地图中手动排除帖子,但到目前为止还没有运气。

这是我当前的代码:

function sitemap_exclude_post( $url, $type, $post) {
if($post->ID == 6298 ) {
return false;
}

return $url;
}
add_filter( 'wpseo_sitemap_entry', 'sitemap_exclude_post', 1, 3 );

任何建议将不胜感激?

注意:我知道这可以通过 Yoast 插件的后端手动输入帖子 ID 来完成,但我需要通过过滤器来完成。上述代码稍后将进一步更改,以自动从 wordpress 中的类别获取帖子的帖子 ID(single.php)。

最佳答案

我知道这个问题已经得到解答,但我需要一种更动态的方法来解释 @clodclod91 解释的内容,因为我不止一次收到这个问题,所以我写了一些关于如何以“更流畅的方式”做到最好的代码而不是硬编码任何值。

我希望它可以帮助一些人。

我还写了一篇解释更多的帖子。阅读 here .

/**
* Set all excluded items for sitemap in a transient
*
* @return mixed
*/
function beee_get_sitemap_excludes_transient() {

// get transient
$output = get_transient( 'beee_exclude_sitemap' );

// if transient returns false, create it
if ( false == $output ) {

$exclude_args = array(
'post_type' => [ 'page' ], // change this to the post types you want excluded
'posts_per_page' => -1, // all items of course
'meta_query' => array(
array(
'key' => 'beee_exclude_sitemap',
'value' => '1',
'compare' => '=',
),
),
'orderby' => 'ID', // recommend to set to ID so 'the order is easier to compare later on', otherwise it can create issues
'order' => 'ASC', // same, just sort ASC to avoid issues
);
$excluded_items = get_posts( $exclude_args );
if ( count( $excluded_items ) > 0 ) {
// if there are items and create an empty array
$exclude = [];
foreach( $excluded_items as $item ) {
// add post id to array
$exclude[] = $item->ID;
}
// create a string from an array since Yoast stores a string, not an array
$output = implode( ',', $exclude );

// set transient with a 24-hour expiration
set_transient( 'beee_exclude_sitemap', $output, 24 * HOUR_IN_SECONDS );
}
}

return $output;
}

/**
* Set Yoast settings from transient
*/
function beee_exclude_pages_sitemap( $post_id ) {

// get our excluded items from transient
$our_excluded_posts = beee_get_sitemap_excludes_transient();

// check if 'exclude' field has been set (in the post)
if ( 1 == get_field( 'beee_exclude_sitemap', $post_id ) ) {
// if yes, check if it exists in transient
if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) ) {
// yes, it's already included so we don't have to do anything
} else {
// no, it's not included so it needs to be added
// so we delete the transient (existing values)
delete_transient( 'beee_exclude_sitemap' );
}
} else {
// it has not been set in the post
if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) ) {
// if does exists in our stored transient, which it shouldn't be in there
// so we delete the transient (existing values)
delete_transient( 'beee_exclude_sitemap' );
}
}

// get our excluded ids from transient
// since we just cleared it, the transient doesn't exist and is retrieved from scratch
$our_excluded_posts = beee_get_sitemap_excludes_transient();

// get post ids from Yoast settings
$wpseo_xml = get_option( 'wpseo_xml' );

// compare Yoast's value with our items
if ( $wpseo_xml[ 'excluded-posts' ] == $our_excluded_posts ) {
// they are the same so we don't need to do anything
} else {
// they are not the same so we need to update the database with our new ids
update_option( 'wpseo_xml', $wpseo_xml );
}
}
add_action( 'save_post', 'beee_exclude_pages_sitemap' );

关于php - Wordpress Yoast 从 XML 站点地图中排除帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com