admin管理员组

文章数量:1279085

I use the built in wp tag functionality to group my posts thematically and then I use the WP_query to have the posts displayed according to custom design. I do not need the tag pages created by default in wordpress and I would either like to have them NOT created at all (ideally) or be able to add a noindex meta to the post, as these should not be indexed separately. Is there some way to accomplish this?

EDIT: one additional possibility I have looked at is to exclude all matching urls from the sitemap eg exclude all urls matchine /tag/* . I use the Yoast plugin, but I don't see any filter which would allow me to do this type of exclusion.

I use the built in wp tag functionality to group my posts thematically and then I use the WP_query to have the posts displayed according to custom design. I do not need the tag pages created by default in wordpress and I would either like to have them NOT created at all (ideally) or be able to add a noindex meta to the post, as these should not be indexed separately. Is there some way to accomplish this?

EDIT: one additional possibility I have looked at is to exclude all matching urls from the sitemap eg exclude all urls matchine /tag/* . I use the Yoast plugin, but I don't see any filter which would allow me to do this type of exclusion.

Share Improve this question edited Nov 16, 2021 at 17:14 shelbypereira asked Nov 16, 2021 at 13:18 shelbypereirashelbypereira 1375 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To redirect from the automatically-generated tag archive pages, you can check to see if is_tag() is true in the template_redirect action hook, and redirect with wp_redirect() if it is:

add_action( 'template_redirect', function() {
    if ( is_tag() ) {
        // Currently redirects to the site's home page.
        wp_redirect( '/' );
        // Use the 301 Permanent redirect if desired.
        // wp_redirect( '/', 301 );
        exit;
    }
} );

To exclude the tags from the sitemap generated by WordPress (Plan B in your question), you can use the wp_sitemaps_taxonomies filter.

add_filter( 'wp_sitemaps_taxonomies', function( $taxonomies ) {
    if ( ! empty( $taxonomies['post_tag'] ) ) {
        unset( $taxonomies['post_tag'] );
    }
    return $taxonomies;
} );

本文标签: How can I prevent wordpress from creating tag pages