admin管理员组

文章数量:1390914

I use WordPress.

I need to show page instead of category when they have same slug.

  • I have category called "Acne" with the slug "Acne"

    page and the category have the same slug.

When I write www.example/acne , the category page is opening,

I need to make WordPress to show the page instead of category when they have the same slug.

Is there any way to do that.

I use WordPress.

I need to show page instead of category when they have same slug.

  • I have category called "Acne" with the slug "Acne"

    page and the category have the same slug.

When I write www.example/acne , the category page is opening,

I need to make WordPress to show the page instead of category when they have the same slug.

Is there any way to do that.

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Feb 4, 2015 at 5:17 JaiJai 113 bronze badges 2
  • Asking for plugin recommendation is off-topic. edit and rewrite your question for actual cause. – Mayeenul Islam Commented Feb 4, 2015 at 5:19
  • I Am Trying To Make A Silo Structure Site With Wordpress. – Jai Commented Feb 4, 2015 at 5:36
Add a comment  | 

1 Answer 1

Reset to default 2

You can hook onto parse_request and trick WordPress into thinking it matched a page permalink if one exists with the same slug for a category term:

/**
 * Override query for pages that match a category slug.
 * 
 * @param   WP  $wp
 */ 
function wpse_177014_category_to_page ( $wp ) {
    if ( ! empty( $wp->query_vars['category_name'] ) && get_page_by_path( $slug = $wp->query_vars['category_name'] ) ) {
        if ( ! empty( $wp->query_vars['paged'] ) )
            $page = $wp->query_vars['paged'];
        else
            $page = '';

        $wp->matched_query = "pagename=$slug&page=$page";
        $wp->query_vars    = array(
            'pagename' => $slug,
            'page'     => $page,    
        );      
    }
}

add_action( 'parse_request', 'wpse_177014_category_to_page' );

本文标签: urlsHow to make pages slug have priority over any category