分类重写分页404

时间:2014-03-24 作者:Robbert

我已将我的分类url重写为我的帖子类型“filter”的名称。

因为我已经这样做了,我的分页在url上不再起作用:

http://www.website.com/filter/term-name/

所以如果我访问

http://www.website.com/filter/term-name/page/2

我有404

我认为这是因为Wordpress现在将filter视为帖子类型和分类法。

如果我改变\'slug\' => \'filter\', 例如,至\'slug\' => \'test\' 它工作得很好。有人知道我怎么能对Wordpress说“过滤器”在这里就是分类法吗。

$args = array(
            \'labels\'                     => $labels,
            \'hierarchical\'               => true,
            \'public\'                     => true,
            \'show_ui\'                    => true,
            \'show_admin_column\'          => true,
            \'show_in_nav_menus\'          => true,
            \'show_tagcloud\'              => true,
            \'rewrite\'                    => array( \'slug\' => \'filter\', \'with_front\' => false ),
        );
        register_taxonomy( \'service\', \'filter\', $args );
有什么建议解释为什么这不起作用吗?

3 个回复
SO网友:Kumar

在寻找解决方案时,我得到了以下帖子:Taxonomies with same slug as CPT

因此,基本上您需要添加自定义重写规则,我还没有测试解决方案,但我想它会适合您。

/* Register CPT
*/
function wpse_138987_post_type_filter() {
 register_post_type(\'filter\', array(
    \'labels\' => array(
        \'name\' => \'Filter\',
        \'all_items\' => \'All Posts\'
    ),
    \'public\' => true
 ));
}
add_action(\'init\', \'wpse_138987_post_type_filter\');

// register taxonomy
function wpse_138987_taxonomy_service() {
 register_taxonomy(\'service\', array(\'filter\'), array(
    \'labels\' => array(
        \'name\' => \'Service\'
    ),
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
    \'rewrite\' => array(\'slug\' => \'filter\')
));
}
add_action(\'init\', \'wpse_138987_taxonomy_service\');


/*
 * Replace Taxonomy slug with Post Type slug in url
*/
function taxonomy_slug_rewrite($wp_rewrite) {
  $rules = array();
  // get all custom taxonomies
  $taxonomies = get_taxonomies(array(\'_builtin\' => false), \'objects\');
  // get all custom post types
  $post_types = get_post_types(array(\'public\' => true, \'_builtin\' => false), \'objects\');

  foreach ($post_types as $post_type) {
    foreach ($taxonomies as $taxonomy) {

        // go through all post types which this taxonomy is assigned to
        foreach ($taxonomy->object_type as $object_type) {

            // check if taxonomy is registered for this custom type
            if ($object_type == $post_type->rewrite[\'slug\']) {

                // get category objects
                $terms = get_categories(array(\'type\' => $object_type, \'taxonomy\' => $taxonomy->name, \'hide_empty\' => 0));

                // make rules
                foreach ($terms as $term) {
                    $rules[$object_type . \'/\' . $term->slug . \'/?$\'] = \'index.php?\' . $term->taxonomy . \'=\' . $term->slug;
                }
            }
        }
    }
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(\'generate_rewrite_rules\', \'taxonomy_slug_rewrite\');
上述解决方案仅在您使用带有Post name的Permalinks时有效。

确保在添加代码后保存permalink,以刷新重写规则。

此外,您还需要为自定义帖子类型存档和分类添加文件:

 archive-filter.php
 single-filter.php
 taxonomy-service.php
转到设置/永久链接,并使用结构/%postname%/保存更改。

SO网友:James

我知道这个问题已经很老了,但仍然有很多人在寻找这个答案,就像我今天早些时候一样。下面的代码假设您的自定义帖子类型和分类法共享相同的slug。在本例中,post类型是portfolio,分类法是services

function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
  $rules = array();
  $post_types = get_post_types( array( \'name\' => \'portfolio\', \'public\' => true, \'_builtin\' => false ), \'objects\' );
  $taxonomies = get_taxonomies( array( \'name\' => \'services\', \'public\' => true, \'_builtin\' => false ), \'objects\' );
  foreach ( $post_types as $post_type ) {
    $post_type_name = $post_type->name; // \'developer\'
    $post_type_slug = $post_type->rewrite[\'slug\']; // \'developers\'
    foreach ( $taxonomies as $taxonomy ) {
      if ( $taxonomy->object_type[0] == $post_type_name ) {
        $terms = get_categories( array( \'type\' => $post_type_name, \'taxonomy\' => $taxonomy->name, \'hide_empty\' => 0 ) );
        foreach ( $terms as $term ) {
          $rules[$post_type_slug . \'/\' . $term->slug . \'/?$\'] = \'index.php?\' . $term->taxonomy . \'=\' . $term->slug;
          $rules[$post_type_slug . \'/\' . $term->slug . \'/page/?([0-9]{1,})/?$\'] = \'index.php?\' . $term->taxonomy . \'=\' . $term->slug . \'&paged=\' . $wp_rewrite->preg_index( 1 );
        }
      }
    }
  }
  $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action(\'generate_rewrite_rules\', \'generate_taxonomy_rewrite_rules\');

SO网友:Vivekanand Saraswati

我也有同样的问题,但最后我解决了,在函数中添加了以下代码。主题文件夹的php。

function generate_taxonomy_rewrite_rules( $wp_rewrite ) {

$rules = array();

$post_types = get_post_types( array(\'public\' => true, \'_builtin\' => false ), \'objects\' );
$taxonomies = get_taxonomies( array(\'public\' => true, \'_builtin\' => false ), \'objects\' );

foreach ( $post_types as $post_type ) {
    $post_type_name = $post_type->name;
    $post_type_slug = $post_type->rewrite[\'slug\'];

    foreach ( $taxonomies as $taxonomy ) {//echo \'DDDDD\'; print_r($taxonomy);
        if ( $taxonomy->object_type[0] == $post_type_name ) {
            $terms = get_categories( array( \'type\' => $post_type_name, \'taxonomy\' => $taxonomy->name, \'hide_empty\' => 0 ) );
            //print_r($terms); 
            foreach ( $terms as $term ) {
                $rules[ $taxonomy->name.\'/\'. $term->slug . \'/?$\'] = \'index.php?\' . $term->taxonomy . \'=\' . $term->slug;
                $rules[ $taxonomy->name.\'/\'. $term->slug . \'/page/?([0-9]{1,})/?$\'] = \'index.php?\'.\'type=\'.$post_type_name."&" . $term->taxonomy . \'=\' . $term->slug . \'&paged=\' . $wp_rewrite->preg_index( 1 );

            }
        }
    } 
}

$wp_rewrite->rules = $rules + $wp_rewrite->rules;

}

add_action(\'generate_rewrite_rules\', \'generate_taxonomy_rewrite_rules\');

REMEMBER MY $taxonomy->name and post type is similar. hope it will work.

结束

相关推荐

Pagination Doesn't Work

I created myfile.php with the following content:global $paged; if ( get_query_var( \'paged\' ) ) { $paged = get_query_var( \'paged\' ); } else if ( get_query_var( \'page\' ) ) { $paged = get_query_var( \'page\' ); } else {