本文介绍了如何在自定义帖子类型的重写规则中放置元值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完全重写了这个问题,因为这个问题有点长,我担心人们会在没有完全阅读的情况下跳过它.

I've completely re-written this question as it got a bit long, and I was worried people skipped it without reading it completely.

我有一个自定义帖子类型(过程),它具有一个自定义元键/值以及一个我想用作子标签的页面ID.

I have a custom post type (procedure) that features a custom meta key/value with a page ID that I want to use as the slug.

我正在使用此功能(如下)在管理区域中创建永久链接,但是在查看这些永久链接时,页面出现404错误.如何创建重写规则以使用相同格式?

I'm using this function (below) to create the permalinks in the admin area, but when viewing these, the pages are 404 errors. How can I create rewrite rules to use this same format?

function bv_procedure_parent_slug( $url, $post ) {

    if( get_post_type( $post ) == 'procedure' && get_post_meta( $post->ID, 'procedure_parent', true ) ) {
        $procedure_parent = get_post( get_post_meta( $post->ID, 'procedure_parent', true))->post_name;
        if( $procedure_parent ) {
            $url = str_replace( 'procedure', $procedure_parent, $url );
        }
    }
    return $url;
}
add_filter( 'post_type_link', 'bv_procedure_parent_slug', 1, 3 );

这里的目标是我将在这里发布很多帖子,其中将包含procedure_parent => 31的元键/值(其中31是页面ID,而帖子名称是'face').当查看单个帖子时,我希望URL为/face/facelift/,而不是/procedure/facelift/.

The goal here is that I'll have lots of posts here, that will contain a meta key/value of procedure_parent => 31 (where 31 is a page ID, and the post name is 'face'). When viewing the single post, rather than the URL being /procedure/facelift/ I would like it to be /face/facelift/.

为此,我相信我需要能够在创建重写规则时访问$ post,以便可以使用get_post_meta().

For this, I believe I need to be able to get access to $post when creating the rewrite rule so I can get use get_post_meta().

但是如何?

推荐答案

似乎您已经使问题变得有点复杂了.如果您执行以下操作,是否可行?

It seems like you've over complicated your problem a little. Would it work if you did the following?

  • 安装自定义帖子类型永久链接插件和
  • 将默认永久链接更改为http://example.com/custompostname/%category%/%postname%/.
  • 然后不用创建带有类别的某些页面,而是使用WordPress的内置存档功能并为您的自定义帖子创建自定义存档模板.
  • Install the Custom Post Type Permalinks plugin and
  • Change the default permalink to http://example.com/custompostname/%category%/%postname%/.
  • Then instead of creating some pages with categories with them, use WordPress' inbuilt archive functionality and make a custom archive template for your custom post.

否则,我认为add_permastruct()是您所缺少的功能.我在 https://wordpress.stackexchange.com/a/253325/58884function my_custom_rewrite() { add_permastruct('procedure', '/%parent%/', false); } add_action('init', 'my_custom_rewrite');

在帖子类型声明中,使用

而不是'rewrite' => array( 'slug' => '%parent%', 'with_front' => false ).该声明的'slug' => '%parent%'部分是自定义帖子类型名称的可选替换($post_type > register_post_type()代码条目).因此,如果您使用'rewrite' => array( 'slug' => 'wibblewobble', 'with_front' => true),则您的网址将从http://example.com/procedure/%postname%/更改为http://example.com/wibblewobble/%postname%/.对于'with_front' => false,我认为它没有任何作用.当您输入'slug' => '%parent%'时,我认为WordPress会将其作为字符串,而不动态地对其进行任何操作.

rather than 'rewrite' => array( 'slug' => '%parent%', 'with_front' => false ) in the post type declaration. The 'slug' => '%parent%' part of that declaration is an optional substitution for the name of the custom post type ($post_type in the register_post_type() codex entry). So if you had 'rewrite' => array( 'slug' => 'wibblewobble', 'with_front' => true) your url would change from http://example.com/procedure/%postname%/ to http://example.com/wibblewobble/%postname%/. With 'with_front' => false I don't think it does anything. When you've entered 'slug' => '%parent%' I think WordPress is taking that as a string and not doing anything with it dynamically.

还请确保使用flush_rewrite_rules();.您可以在测试时在init挂钩函数中使用它,然后将其移动到主题/插件激活挂钩函数.

Also be sure to use flush_rewrite_rules();. You can use it in the init hooked function while testing and then move it to theme/plugin activation hooked function once it works.

祝你好运!

这篇关于如何在自定义帖子类型的重写规则中放置元值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:05