子标题相同标题的WordPress页面

子标题相同标题的WordPress页面

本文介绍了拥有与自定义帖子类型“前"子标题相同标题的WordPress页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义帖子类型djs和一个具有两个术语的自定义分类法city:bostonnyc.因此,可以将DJ标记为Boston或NYC.

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配置文件(单个djs帖子)位于/nyc-wedding-dj/joe-shmoe/,或者对于Boston DJ,/boston-wedding-dj/jane-doe/.换句话说,该自定义帖子类型的重写标记设置为%city%-wedding-dj.

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.

我有两个常规页面,分别称为/boston-wedding-djs//nyc-wedding-djs/(请注意带有 s 的复数 DJ ),在这里我使用自定义页面模板来循环浏览来自相应城市的DJ,并显示指向该页面所在城市的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.

我想做的是将重写代码设为%city%-wedding-djs,以便自定义帖子类型的单个页面出现"(至少对google而言)作为上述那些页面的子页面.

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.

但是有一个冲突,当我将自定义帖子类型djs的重写标记更改为%city%-wedding-djs时,页面/boston-wedding-djs//nyc-wedding-djs/出现404 Not Found错误.

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?

推荐答案

这使我想起了一个很棒的博客有关某个主题的信息.

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.

如果必须这样做,并且正在WordPress中寻找解决方法,则可以在-或过滤器.

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.

示例:

这会检查%city%-wedding-djs页是否存在,因此,如果以后添加更多城市,则无需更新代码段:

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;
} );

,或者仅将bostonnyc作为城市,并且不需要检查是否存在相应的页面:

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;
} );

这意味着请求:

example.tld/nyc-wedding-djs/

现在被视为page,并且

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

作为djs帖子.

在这里,我们假定page重写规则的优先级低于djs帖子类型的优先级.

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

这篇关于拥有与自定义帖子类型“前"子标题相同标题的WordPress页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:43