Have a WordPress page with same title as custom post type “front” slug

会有一股神秘感。 提交于 2019-12-06 17:28:50

问题


I have a custom post type djs and a custom taxonomy city with two terms: boston and nyc. So a DJ can either be tagged as Boston or NYC.

DJ profiles (a single djs post) are located at /nyc-wedding-dj/joe-shmoe/, for example, or for a Boston DJ, /boston-wedding-dj/jane-doe/. In other words, the rewrite slug for that custom post type is set as %city%-wedding-dj.

I have two regular pages called /boston-wedding-djs/ and /nyc-wedding-djs/ (note the plural DJs with an s) where I use a custom page template to loop through the DJs from the corresponding city and display a list of links to DJ Profiles from that page's city.

What I would like to do is to have the rewrite slug be %city%-wedding-djs so that the custom post type single pages "appear" (at least to google) as child pages of those pages mentioned above.

There's a conflict however, that when I change the rewrite slug for the custom post type djs to %city%-wedding-djs, I get a 404 Not Found error for the pages /boston-wedding-djs/ and /nyc-wedding-djs/.

How can I have a rewrite slug with the same name as a page?


回答1:


This reminded me of a great blog post by Matthew Boynes on a somewhat related subject.

It's recommended to use a unique base slug, to avoid the clashes you're describing.

If you must do this and are looking for a workaround within WordPress, then it's possible to adjust how it parses the incoming request, with the help of the request- or parse_request filter.

Example:

This checks if the %city%-wedding-djs page exists, so you don't need to update the code snippet if you later add more cities:

! is_admin() && add_filter( 'request', function( $query_vars )  
{
    if ( isset( $query_vars['city'] ) && ! isset( $query_vars['djs'] ) )  
    {
        $slug = $query_vars['city'] . '-wedding-djs';

        // Override if the %city%-wedding-djs page exists
        if ( get_page_by_path( $slug ) )
            $query_vars['pagename'] = $slug;
    }
    return $query_vars;
} );

or if you only got boston and nyc as cities and you don't need to check if the corresponding pages exists:

! is_admin() && add_filter( 'request', function( $query_vars )  
{
    if ( isset( $query_vars['city'] ) && ! isset( $query_vars['djs'] ) )  
    {
        $city = $query_vars['city'];
        if ( in_array( $city, array( 'boston', 'nyc' ) ) )
            $query_vars['pagename'] = $city . '-wedding-djs';
    }
    return $query_vars;
} );

This means that the request:

example.tld/nyc-wedding-djs/

is now treated as a page, and

example.tld/nyc-wedding-djs/joe-smhoe/

as a djs post.

Here we assume that the page rewrite rules have lower priority than for the djs post type.




回答2:


If you are using htaccess you can manually set up the redirects for those pages and they should then resolve correctly. Add these lines :

RewriteCond %{REQUEST_URI} ^/boston-wedding-djs/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/boston-wedding-djs$ [NC]
RewriteRule . index\.php?pagename=boston-wedding-djs [NC,L]
RewriteCond %{REQUEST_URI} ^/nyc-wedding-djs/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/nyc-wedding-djs$ [NC]
RewriteRule . index\.php?pagename=nyc-wedding-djs [NC,L]

after RewriteRule ^index\.php$ - [L]

You may also be able to do this with WordPress add_rewrite_rule however it already seems like they are confused so it seems that is less likely to work.




回答3:


You can't have a post and a page with the same slug.

You need to remove your page and create a template page of archive-djs.php and maybe a single-djs.php and that will display that page dynamically for you.

See this page for the template hierarchy



来源:https://stackoverflow.com/questions/31621722/have-a-wordpress-page-with-same-title-as-custom-post-type-front-slug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!