重写自定义帖子类型url的添加元框值

时间:2013-05-14 作者:Richard B

我有以下自定义帖子类型(CPT)和元框:

电影(CPT)类型(元框)

  • 类型(CPT)
  • 如果有一部电影被称为“铁杆电影”,那么我希望它是:/Genres/action/Die hard

    通过将电影CPT设置为“rewrite”=>false并改用以下代码,可以轻松解决此问题:

    add_action(\'init\', \'rb_add_rewrite_rules\');
    add_filter(\'post_type_link\', \'rb_create_permalinks\', 10, 3);
    
    function rb_add_rewrite_rules() {
        global $wp_rewrite;
        $wp_rewrite->add_rewrite_tag(\'%movies%\', \'([^/]+)\', \'movies=\');
        $wp_rewrite->add_rewrite_tag(\'%genre%\', \'([^/]+)\', \'genre=\');
    
        $wp_rewrite->add_permastruct(\'movies\', \'/genres/%genre%/%movies%\', false);
    }
    
    function rb_create_permalinks($permalink, $post, $leavename) {
        $no_data = \'no-data\';
        $post_id = $post->ID;
    
        if($post->post_type != \'movies\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))
        return $permalink;
    
        $event_id = get_post_meta($post_id, \'genre\', true);
        $var1 = basename(get_permalink($event_id));
        $var1 = sanitize_title($var1);
    
        if(!$var1)
            $var1 = $no_data;
    
        $permalink = str_replace(\'%genre%\', $var1, $permalink);
    
        return $permalink;
    }
    
    好吧,问题来了。铁杆permalink和归档类型。php(显示所有类型)看起来不错,但只显示单个类型。找不到php–相反,转到/genres/action将显示索引。php代码。

    我想问题是我的重写发生了冲突,也许不可能做到这一点:

    \'movies\' CPT permalink

    /custom base name/custom field meta box value)/\'movies\' CPT post name
    

    \'genres\' CPT permalink

    /custom post type name (same value as the custom base name above!!)/\'genres\' CPT post name
    
    如何使WordPress使用单一体裁。php for/流派/动作等?

  • 2 个回复
    最合适的回答,由SO网友:Matthew Boynes 整理而成

    听起来你离这里很近了,你唯一剩下的问题是你对流派和电影的永久链接发生了冲突。

    \'movies\' CPT permalink
    /genres/%custom field meta box value%/%movie%/
    
    \'genres\' CPT permalink
    /genres/%genre%/
    
    这里的问题是使用add_permastruct. 当它为电影制定规则时,它们看起来是这样的:

    genres/([^/]+)/([^/]+)(/[0-9]+)?/?$
      => index.php?genre=$matches[1]&movies=$matches[2]&page=$matches[3]
    
    按照WordPress处理请求的方式,您不能在同一URL中设置多个帖子类型——WordPress在第一个帖子类型之后停止。也就是说,they can be in the pretty URL, but not in the "ugly" one that generates the request and query. 任何重写规则movies= 不能包含genre=. 以下是对rb_add_rewrite_rules. 您可以提取任何可能不需要的规则,如附件URL、提要URL等。与往常一样,在进行重写更改时,请确保通过转到“设置”->“永久链接”并单击“保存更改”刷新永久链接。另外,确保\'rewrite\' => false 在两种自定义帖子类型上都设置。

    function rb_add_rewrite_rules() {
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/trackback/?$\', \'index.php?movies=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?movies=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?movies=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/page/?([0-9]{1,})/?$\', \'index.php?&movies=$matches[1]&paged=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)(/[0-9]+)?/?$\', \'index.php?movies=$matches[1]&page=$matches[3]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/trackback/?$\', \'index.php?genre=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?genre=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?genre=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/page/?([0-9]{1,})/?$\', \'index.php?genre=$matches[1]&paged=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)(/[0-9]+)?/?$\', \'index.php?genre=$matches[1]&page=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
    }
    

    SO网友:Angela

    我认为您在数据结构方面采取了错误的方法。您应该创建一个称为流派的自定义分类法,并将您的电影CPT绑定到流派自定义分类法,然后选择电影所属的流派。

    解决有关现有代码的问题。单个XXX。php模板用于显示名为XXX的CPT中的内容。因此,为了显示电影,您应该使用单个电影。php。并专门为特定的post_meta寻址内容,您可以执行以下操作:

    if(get_post_meta($post_id, \'genres\', false) == \'action\') {
       //Do Stuff Here
    }
    

    结束

    相关推荐

    重写自定义帖子类型url的添加元框值 - 小码农CODE - 行之有效找到问题解决它

    重写自定义帖子类型url的添加元框值

    时间:2013-05-14 作者:Richard B

    我有以下自定义帖子类型(CPT)和元框:

    电影(CPT)类型(元框)

  • 类型(CPT)
  • 如果有一部电影被称为“铁杆电影”,那么我希望它是:/Genres/action/Die hard

    通过将电影CPT设置为“rewrite”=>false并改用以下代码,可以轻松解决此问题:

    add_action(\'init\', \'rb_add_rewrite_rules\');
    add_filter(\'post_type_link\', \'rb_create_permalinks\', 10, 3);
    
    function rb_add_rewrite_rules() {
        global $wp_rewrite;
        $wp_rewrite->add_rewrite_tag(\'%movies%\', \'([^/]+)\', \'movies=\');
        $wp_rewrite->add_rewrite_tag(\'%genre%\', \'([^/]+)\', \'genre=\');
    
        $wp_rewrite->add_permastruct(\'movies\', \'/genres/%genre%/%movies%\', false);
    }
    
    function rb_create_permalinks($permalink, $post, $leavename) {
        $no_data = \'no-data\';
        $post_id = $post->ID;
    
        if($post->post_type != \'movies\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))
        return $permalink;
    
        $event_id = get_post_meta($post_id, \'genre\', true);
        $var1 = basename(get_permalink($event_id));
        $var1 = sanitize_title($var1);
    
        if(!$var1)
            $var1 = $no_data;
    
        $permalink = str_replace(\'%genre%\', $var1, $permalink);
    
        return $permalink;
    }
    
    好吧,问题来了。铁杆permalink和归档类型。php(显示所有类型)看起来不错,但只显示单个类型。找不到php–相反,转到/genres/action将显示索引。php代码。

    我想问题是我的重写发生了冲突,也许不可能做到这一点:

    \'movies\' CPT permalink

    /custom base name/custom field meta box value)/\'movies\' CPT post name
    

    \'genres\' CPT permalink

    /custom post type name (same value as the custom base name above!!)/\'genres\' CPT post name
    
    如何使WordPress使用单一体裁。php for/流派/动作等?

  • 2 个回复
    最合适的回答,由SO网友:Matthew Boynes 整理而成

    听起来你离这里很近了,你唯一剩下的问题是你对流派和电影的永久链接发生了冲突。

    \'movies\' CPT permalink
    /genres/%custom field meta box value%/%movie%/
    
    \'genres\' CPT permalink
    /genres/%genre%/
    
    这里的问题是使用add_permastruct. 当它为电影制定规则时,它们看起来是这样的:

    genres/([^/]+)/([^/]+)(/[0-9]+)?/?$
      => index.php?genre=$matches[1]&movies=$matches[2]&page=$matches[3]
    
    按照WordPress处理请求的方式,您不能在同一URL中设置多个帖子类型——WordPress在第一个帖子类型之后停止。也就是说,they can be in the pretty URL, but not in the "ugly" one that generates the request and query. 任何重写规则movies= 不能包含genre=. 以下是对rb_add_rewrite_rules. 您可以提取任何可能不需要的规则,如附件URL、提要URL等。与往常一样,在进行重写更改时,请确保通过转到“设置”->“永久链接”并单击“保存更改”刷新永久链接。另外,确保\'rewrite\' => false 在两种自定义帖子类型上都设置。

    function rb_add_rewrite_rules() {
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/trackback/?$\', \'index.php?movies=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?movies=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?movies=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/page/?([0-9]{1,})/?$\', \'index.php?&movies=$matches[1]&paged=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)(/[0-9]+)?/?$\', \'index.php?movies=$matches[1]&page=$matches[3]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/trackback/?$\', \'index.php?genre=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?genre=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?genre=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)/page/?([0-9]{1,})/?$\', \'index.php?genre=$matches[1]&paged=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/([^/]+)(/[0-9]+)?/?$\', \'index.php?genre=$matches[1]&page=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/?$\', \'index.php?attachment=$matches[1]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/trackback/?$\', \'index.php?attachment=$matches[1]&tb=1\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\', \'index.php?attachment=$matches[1]&feed=$matches[2]\', \'top\' );
        add_rewrite_rule( \'genres/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\', \'index.php?attachment=$matches[1]&cpage=$matches[2]\', \'top\' );
    }
    

    SO网友:Angela

    我认为您在数据结构方面采取了错误的方法。您应该创建一个称为流派的自定义分类法,并将您的电影CPT绑定到流派自定义分类法,然后选择电影所属的流派。

    解决有关现有代码的问题。单个XXX。php模板用于显示名为XXX的CPT中的内容。因此,为了显示电影,您应该使用单个电影。php。并专门为特定的post_meta寻址内容,您可以执行以下操作:

    if(get_post_meta($post_id, \'genres\', false) == \'action\') {
       //Do Stuff Here
    }
    

    相关推荐

    如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

    一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: