自定义帖子类型分类-生成固定链接,但未找到帖子

时间:2016-03-22 作者:Solflux

我有一个自定义的贴子类型,记录诊所提供的治疗方法。这个位于网站的化妆品部分(site.com/cosmetics/).

筛选器已成功转换\\%type\\ 对于permalinks。这就是我想要的结构:site.com/cosmetics/treatments

site.com/cosmetics/treatments/[type]/

site.com/cosmetics/treatments/[type]/[treatment]

我已经试着理解这里的许多答案了。我的代码中有什么想法或明显的缺陷吗?我已经每次都在冲洗permalinks了。

site.com/cosmetics/treatments/[type] 工作正常。治疗页面本身从管理面板链接到以下内容:site.com/[type]/[treatment]这是404错误。

UPDATE

我重新排列了一些代码,所以我在这里更改了代码以反映这一点。问题仍然没有改变。

插件代码:

    class NSCTreatments {

        /**
         * Constructor for taxonomy and post type initialisation
         */
        function __construct(){
            add_action( \'init\', array( $this, \'register_custom_taxonomies\') ); // this MUST go first
            add_action( \'init\', array( $this, \'register_custom_post_type\' ) );

            add_filter(\'post_link\', \'treatment_permalink\', 10, 3);
            add_filter(\'post_type_link\', \'treatment_permalink\', 10, 3);

            function treatment_permalink($permalink, $post_id, $leavename) {
                if (strpos($permalink, \'%type%\') === FALSE) return $permalink;

                // Get post
                $post = get_post($post_id);
                if (!$post) return $permalink;

                // Get taxonomy terms
                $terms = wp_get_object_terms($post->ID, \'treatment-type\');
                if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
                else $taxonomy_slug = \'no-type\';

                return str_replace(\'%type%\', $taxonomy_slug, $permalink);
            }

        }

        function  register_custom_taxonomies (){

        $labels = array(
            \'name\'              => _x( \'Treatment Types\', \'taxonomy general name\' ),
            \'singular_name\'     => _x( \'Treatment Type\', \'taxonomy singular name\' ),
            \'search_items\'      => __( \'Search Treatment Types\' ),
            \'all_items\'         => __( \'All Treatment Types\' ),
            \'parent_item\'       => __( \'Parent Treatment Types\' ),
            \'parent_item_colon\' => __( \'Parent Treatment Types:\' ),
            \'edit_item\'         => __( \'Edit Treatment Type\' ),
            \'update_item\'       => __( \'Update Treatment Type\' ),
            \'add_new_item\'      => __( \'Add New Treatment Type\' ),
            \'new_item_name\'     => __( \'New Treatment Type Name\' ),
            \'menu_name\'         => __( \'Treatment Type\' ),
        );

        $args = array(
            \'hierarchical\'      => false,
            \'labels\'            => $labels,
            \'show_ui\'           => true,
            \'show_admin_column\' => false,
            \'query_var\'         => true,
            \'rewrite\'           => array( \'slug\' => \'cosmetics/treatments\', \'with_front\' => false ),
        );

        register_taxonomy( \'treatment-type\', array( \'treatment\' ), $args );
    }

        /**
         * Registers a Custom Post Type called Treatment
         */
        function register_custom_post_type() {
            register_post_type( \'treatment\', array(
                \'labels\' => array(
                    \'name\'               => _x( \'Treatments\', \'post type general name\', \'nsc-treatments\' ),
                    \'singular_name\'      => _x( \'Treatment\', \'post type singular name\', \'nsc-treatments\' ),
                    \'menu_name\'          => _x( \'Treatments\', \'admin menu\', \'nsc-treatments\' ),
                    \'name_admin_bar\'     => _x( \'Treatment\', \'add new on admin bar\', \'nsc-treatments\' ),
                    \'add_new\'            => _x( \'Add New\', \'treatment\', \'nsc-treatments\' ),
                    \'add_new_item\'       => __( \'Add New Treatment\', \'nsc-treatments\' ),
                    \'new_item\'           => __( \'New Treatment\', \'nsc-treatments\' ),
                    \'edit_item\'          => __( \'Edit Treatment\', \'nsc-treatments\' ),
                    \'view_item\'          => __( \'View Treatment\', \'nsc-treatments\' ),
                    \'all_items\'          => __( \'All Treatments\', \'nsc-treatments\' ),
                    \'search_items\'       => __( \'Search Treatments\', \'nsc-treatments\' ),
                    \'parent_item_colon\'  => __( \'Parent Treatment:\', \'nsc-treatments\' ),
                    \'not_found\'          => __( \'No treatment found.\', \'nsc-treatments\' ),
                    \'not_found_in_trash\' => __( \'No treatment found in Trash.\', \'nsc-treatments\' ),
                ),

                // Frontend
                \'has_archive\'        => \'cosmetics/treatments\',
                \'public\'             => true,
                \'publicly_queryable\' => true,
                \'rewrite\' => array(\'slug\' => \'%type%\', \'with_front\' => false),

                // Admin
                \'capability_type\' => \'post\',
                \'menu_icon\'     => \'dashicons-screenoptions\',
                \'menu_position\' => 20,
                \'query_var\'     => true,
                \'show_in_menu\'  => true,
                \'show_ui\'       => true,
                \'supports\'      => array(
                    \'title\',
                    \'author\',
                ),
            ) );
        }
    }

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您之前的编辑更接近。将post type rewrite slug更改为:

cosmetics/treatments/%treatment-type%
然后更改的所有实例%type%%treatment-type%treatment_permalink 作用

刷新重写后,一切都应该正常工作。

为供将来参考Monkeyman rewrite analyzer plugin 对于调试重写规则非常有帮助。正如我提到的,您还可以转储$wp_query 查看WordPress在每个请求中试图查询的内容。您可以将类似的内容直接放入404模板中,以便快速调试您认为不应该是404的请求:

<pre>
<?php print_r( $wp_query ); ?>
</pre>
您将看到所有查询变量及其值的列表,并将生成的SQL查询WordPress发送到数据库。

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?