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

不想你离开。 提交于 2019-12-05 00:46:43

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.

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.

weston deboer

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

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