我正在尝试让permalinks使用自定义分类法和自定义帖子类型。按照说明操作here 我能让它几乎正常工作,但有一个小故障。
注册分类的代码如下所示:
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'show_in_nav_menus\' => false,
\'show_ui\' => true,
\'show_tagcloud\' => true,
\'hierarchical\' => true,
\'rewrite\' => array(\'slug\' => \'products\', \'hierarchical\' => true, \'with_front\' => true),
// \'rewrite\' => true,
\'query_var\' => true
);
register_taxonomy( \'tc_product_type\', array(\'tc_products\'), $args );
注册帖子类型的代码:
$post_type_args = array(
\'labels\' => $labels,
\'singular_label\' => __(\'Product\'),
\'public\' => true,
\'show_ui\' => true,
\'publicly_queryable\'=> true,
\'query_var\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => true,
\'rewrite\' => array(\'slug\' => \'products/%taxonomy_name%/\'),
// \'rewrite\' => array(\'slug\' => \'products\', \'with_front\' => false),
\'supports\' => $supports,
\'menu_position\' => 0,
\'taxonomies\' => array(\'post_tag\')
);
register_post_type(\'tc_products\',$post_type_args);
permalinks本身创建得很好,查看分类页面也很好,但在网站上查看帖子时,我得到了404。我转储了WP\\u查询,它似乎使用permalink URI的最后一段作为tc\\u product\\u类型,而不是post slug(在post slug为“77777-2”的情况下):
WP_Query::__set_state(array(
\'query_vars\' =>
array (
\'tc_product_type\' => \'77777-2\',
\'error\' => \'\',
\'m\' => 0,
\'p\' => 0,
\'post_parent\' => \'\',
\'subpost\' => \'\',
\'subpost_id\' => \'\',
\'attachment\' => \'\',
\'attachment_id\' => 0,
\'name\' => \'\',
\'static\' => \'\',
\'pagename\' => \'\',
\'page_id\' => 0,
\'second\' => \'\',
\'minute\' => \'\',
\'hour\' => \'\',
\'day\' => 0,
\'monthnum\' => 0,
\'year\' => 0,
\'w\' => 0,
\'category_name\' => \'\',
\'tag\' => \'\',
\'cat\' => \'\',
\'tag_id\' => \'\',
\'author_name\' => \'\',
\'feed\' => \'\',
\'tb\' => \'\',
\'paged\' => 0,
\'comments_popup\' => \'\',
\'meta_key\' => \'\',
\'meta_value\' => \'\',
\'preview\' => \'\',
\'s\' => \'\',
\'sentence\' => \'\',
\'fields\' => \'\',
\'category__in\' =>
array (
),
\'category__not_in\' =>
array (
),
\'category__and\' =>
array (
),
\'post__in\' =>
array (
),
\'post__not_in\' =>
array (
),
\'tag__in\' =>
array (
),
\'tag__not_in\' =>
array (
),
\'tag__and\' =>
array (
),
\'tag_slug__in\' =>
array (
),
\'tag_slug__and\' =>
array (
),
\'ignore_sticky_posts\' => false,
\'suppress_filters\' => false,
\'cache_results\' => true,
\'update_post_term_cache\' => true,
\'update_post_meta_cache\' => true,
\'post_type\' => \'\',
\'posts_per_page\' => 10,
\'nopaging\' => false,
\'comments_per_page\' => \'50\',
\'no_found_rows\' => false,
\'taxonomy\' => \'tc_product_type\',
\'term\' => \'77777-2\',
\'order\' => \'DESC\',
),
\'tax_query\' =>
WP_Tax_Query::__set_state(array(
\'queries\' =>
array (
0 =>
array (
\'taxonomy\' => \'tc_product_type\',
\'terms\' =>
array (
0 => \'77777-2\',
),
\'include_children\' => true,
\'field\' => \'slug\',
\'operator\' => \'IN\',
),
),
\'relation\' => \'AND\',
)),
\'meta_query\' =>
WP_Meta_Query::__set_state(array(
\'queries\' =>
array (
),
\'relation\' => NULL,
)),
\'post_count\' => 0,
\'current_post\' => -1,
\'in_the_loop\' => false,
\'comment_count\' => 0,
\'current_comment\' => -1,
\'found_posts\' => \'0\',
\'max_num_pages\' => 0,
\'max_num_comment_pages\' => 0,
\'is_single\' => false,
\'is_preview\' => false,
\'is_page\' => false,
\'is_archive\' => false,
\'is_date\' => false,
\'is_year\' => false,
\'is_month\' => false,
\'is_day\' => false,
\'is_time\' => false,
\'is_author\' => false,
\'is_category\' => false,
\'is_tag\' => false,
\'is_tax\' => false,
\'is_search\' => false,
\'is_feed\' => false,
\'is_comment_feed\' => false,
\'is_trackback\' => false,
\'is_home\' => false,
\'is_404\' => true,
\'is_comments_popup\' => false,
\'is_paged\' => false,
\'is_admin\' => false,
\'is_attachment\' => false,
\'is_singular\' => false,
\'is_robots\' => false,
\'is_posts_page\' => false,
\'is_post_type_archive\' => false,
\'query_vars_hash\' => \'e63e67c24b4fac79b23b43e0704197c3\',
\'query_vars_changed\' => false,
\'thumbnails_cached\' => false,
\'query\' =>
array (
\'tc_product_type\' => \'cotton-candy/tasty-tubs-2/77777-2\',
),
\'request\' => \' SELECT SQL_CALC_FOUND_ROWS tc_posts.* FROM tc_posts WHERE 1=1 AND 0 = 1 AND tc_posts.post_type IN (\\\'post\\\', \\\'page\\\', \\\'attachment\\\', \\\'tc_testamonials\\\', \\\'tc_press\\\', \\\'tc_products\\\', \\\'tc_service\\\', \\\'tc_variant_group\\\', \\\'tc_variation_item\\\', \\\'tc_variation_rule\\\', \\\'tc_coupon\\\', \\\'tc_payment\\\', \\\'tc_order\\\', \\\'tc_contact\\\', \\\'tc_contact_address\\\', \\\'tc_project\\\', \\\'tc_activity\\\') AND (tc_posts.post_status = \\\'publish\\\' OR tc_posts.post_status = \\\'private\\\') GROUP BY tc_posts.ID ORDER BY tc_posts.post_date DESC LIMIT 0, 10\',
\'posts\' =>
array (
),
\'queried_object\' => NULL,
\'queried_object_id\' => 0,
))
我是不是错过了什么?如何告诉WP最后一段是页面段塞?
编辑:忘记了一些重要的事情。在上面链接的帖子中,他创建了重写规则,知道页面段塞将始终位于永久链接中的特定段。这在我的情况下行不通,因为每个分类下都会有帖子。。。这意味着我不知道弹头的确切位置(当然,除了它将是末端)。是否有覆盖该场景的重写规则?它如何知道最后一段是分类法还是后段?
编辑2:添加相关重写规则的转储:
array (
\'products/%taxonomy_name%/?$\' => \'index.php?%taxonomy_name%$matches[1]\',
\'products/%taxonomy_name%/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?%taxonomy_name%$matches[1]&feed=$matches[2]\',
\'products/%taxonomy_name%/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?%taxonomy_name%$matches[1]&feed=$matches[2]\',
\'products/%taxonomy_name%/page/([0-9]{1,})/?$\' => \'index.php?post_type=tc_products&paged=$matches[1]\',
/*...more rules, then...*/
\'products/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?tc_product_type=$matches[1]&feed=$matches[2]\',
\'products/(.+?)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?tc_product_type=$matches[1]&feed=$matches[2]\',
\'products/(.+?)/page/?([0-9]{1,})/?$\' => \'index.php?tc_product_type=$matches[1]&paged=$matches[2]\',
\'products/(.+?)/?$\' => \'index.php?tc_product_type=$matches[1]\',
/*...more rules, then...*/
\'products/%taxonomy_name%/.+?/attachment/([^/]+)/?$\' => \'index.php?attachment=$matches[1]\',
\'products/%taxonomy_name%/.+?/attachment/([^/]+)/trackback/?$\' => \'index.php?attachment=$matches[1]&tb=1\',
\'products/%taxonomy_name%/.+?/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?attachment=$matches[1]&feed=$matches[2]\',
\'products/%taxonomy_name%/.+?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?attachment=$matches[1]&feed=$matches[2]\',
\'products/%taxonomy_name%/.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?attachment=$matches[1]&cpage=$matches[2]\',
\'products/%taxonomy_name%/(.+?)/trackback/?$\' => \'index.php?%taxonomy_name%$matches[1]&tc_products=$matches[2]&tb=1\',
\'products/%taxonomy_name%/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?%taxonomy_name%$matches[1]&tc_products=$matches[2]&feed=$matches[3]\',
\'products/%taxonomy_name%/(.+?)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?%taxonomy_name%$matches[1]&tc_products=$matches[2]&feed=$matches[3]\',
\'products/%taxonomy_name%/(.+?)/page/?([0-9]{1,})/?$\' => \'index.php?%taxonomy_name%$matches[1]&tc_products=$matches[2]&paged=$matches[3]\',
\'products/%taxonomy_name%/(.+?)/comment-page-([0-9]{1,})/?$\' => \'index.php?%taxonomy_name%$matches[1]&tc_products=$matches[2]&cpage=$matches[3]\',
\'products/%taxonomy_name%/(.+?)(/[0-9]+)?/?$\' => \'index.php?%taxonomy_name%$matches[1]&tc_products=$matches[2]&page=$matches[3]\',
\'products/%taxonomy_name%/page/?([0-9]{1,})/?$\' => \'index.php?%taxonomy_name%$matches[1]&paged=$matches[2]\',
\'products/%taxonomy_name%/comment-page-([0-9]{1,})/?$\' => \'index.php?%taxonomy_name%$matches[1]&cpage=$matches[2]\',
/*...more rules...*/
)
它似乎符合以下规则:
\'products/(.+?)/?$\' => \'index.php?tc_product_type=$matches[1]\',
不知道如何使其与其他规则匹配,或者其他规则是否有效。