帮助实现分层自定义分类和固定链接...几乎就在那里

时间:2012-05-03 作者:flashape

我正在尝试让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]\',
不知道如何使其与其他规则匹配,或者其他规则是否有效。

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

好的,我通过挂接到“请求”过滤器,然后检查最后一段是否为term来实现它。如果不是,请更改$请求变量:

function alter_the_query( $request ) {
    $dummy_query = new WP_Query();  // the query isn\'t run if we don\'t pass any query vars
    $dummy_query->parse_query( $request );

    if ( !$dummy_query->is_admin && isset($request[\'tc_product_type\']) ){       
        $lastSegment = basename($request[\'tc_product_type\']);
        if(get_term_by(\'slug\',$lastSegment,  \'tc_product_type\') === FALSE){
            $request[\'post_type\'] = \'tc_products\';
            $request[\'tc_products\'] = $lastSegment;
            $request[\'name\'] = $lastSegment;
        }
    }

    return $request;
}
add_filter( \'request\', \'alter_the_query\' );

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post