簡體   English   中英

Wordpress Yoast從XML站點地圖中排除帖子

[英]Wordpress Yoast exclude posts from XML sitemap

我正在使用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 );

任何建議將不勝感激?

注意:我知道可以通過手動輸入帖子ID通過Yoast插件的后端來完成,但我需要通過過濾器來完成。 上面的代碼將在以后進一步更改,以自動從wordpress中的類別獲取帖子的帖子ID(single.php)。

我知道這個問題已經得到了解答但是我需要一個更加動態的方法來解釋@ clodclod91所解釋的問題,因為我不止一次收到這個問題,我寫了一些關於如何以“更流暢的方式”做到最好的代碼而不是硬編碼值。

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

我還寫了一篇文章,附有更多解釋。 在這里閱讀。

/**
 * 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' );

可以使用update_option函數。

$array = array(
  "excluded-posts" => 6298
);

update_option('wpseo_xml', $array);

也應該在add_filter鈎子里面工作。 update_option是一個在表選項中插入或更新的wp函數。 關於XML Sitemap的Yoast SEO的wp_options表內的關鍵是wpseo_xml。 在值中有一個包含其他信息的數組:

a:18: {
s: 22: "disable_author_sitemap";
b: 1;
s: 22: "disable_author_noposts";
b: 1;
s: 16: "enablexmlsitemap";
b: 1;
s: 16: "entries-per-page";
i: 1000;
s: 14: "excluded-posts";
s: 7: "1,2,3,4";
s: 38: "user_role-administrator-not_in_sitemap";
b: 0;
s: 31: "user_role-editor-not_in_sitemap";
b: 0;
s: 31: "user_role-author-not_in_sitemap";
b: 0;
s: 36: "user_role-contributor-not_in_sitemap";
b: 0;
s: 35: "user_role-subscriber-not_in_sitemap";
b: 0;
s: 36: "user_role-authors_tes-not_in_sitemap";
b: 0;
s: 40: "user_role-authors_academy-not_in_sitemap";
b: 0;
s: 30: "post_types-post-not_in_sitemap";
b: 0;
s: 30: "post_types-page-not_in_sitemap";
b: 0;
s: 36: "post_types-attachment-not_in_sitemap";
b: 1;
s: 34: "taxonomies-category-not_in_sitemap";
b: 0;
s: 34: "taxonomies-post_tag-not_in_sitemap";
b: 0;
s: 37: "taxonomies-post_format-not_in_sitemap";
b: 0;

}

您可以從Yoast管理界面輕松完成:

https://kb.yoast.com/kb/sitemap-shows-excluded-posts-pages/

干杯,弗朗切斯科

我在我的問題中發布的上述代碼工作正常。

我剛按下Yoast的儀表板“排除帖子”部分中的“保存更改”按鈕,它就可以了。 不確定如何,但它的工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM