繁体   English   中英

自定义帖子类型和分类使用相同的slug与重写

[英]Custom Post type and Taxonomy using same slug with rewrite

这是我第一次尝试使用WordPress重写规则,所以请耐心等待。 问题是我添加到我的投资组合的所有项目都有多个类别。 我想在显示投资组合帖子时从网址中删除该类别。

site.com/portfolio - >工作

site.com/portfolio/category/ - >工作

site.com/portfolio/category/post-added-to-portfolio/ - >工作,但我不希望它

site.com/portfolio/post-added-to-portfolio/ - >不起作用,但它应该

/* Post Type: Portfolio */
$labels = array(
    "name" => __( "Portfolio", "" ),
    "singular_name" => __( "Portfolio", "" ),
);
$args = array(
    "label" => __( "Portfolio", "" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => "portfolio",
    "show_in_menu" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => array( "slug" => "portfolio", "with_front" => true ),
    "query_var" => true,
    "supports" => array( "title", "editor" ),
    "taxonomies" => array( "services" ),
);
register_post_type( "portfolio", $args );   

/* Taxonomy: Services */
$labels = array(
    "name" => __( "Services", "" ),
    "singular_name" => __( "Service", "" ),
);
$args = array(
    "label" => __( "Services", "" ),
    "labels" => $labels,
    "public" => true,
    "hierarchical" => true,
    "label" => "Services",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'portfolio', 'with_front' => true, ),
    "show_admin_column" => false,
    "show_in_rest" => false,
    "rest_base" => "",
    "show_in_quick_edit" => false,
);
register_taxonomy( "services", array( "portfolio" ), $args );

// handle redirects for taxonomy
add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
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;
    $post_type_slug = $post_type->rewrite['slug'];
    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;
}

经过一个星期的努力,我终于解决了这个问题。 我完全按照原样保留了帖子类型。 我用以下代替了generate_rewrite_rules函数。

add_filter('request', 'setTermRequest', 1, 1 );
function setTermRequest($query){
    $tax_name = 'services';
    if( $query['attachment'] ) :
        $include_children = true;
        $name = $query['attachment'];
    else:
        $include_children = false;
        $name = $query['name'];
    endif;
    $term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists
    if (isset($name) && $term && !is_wp_error($term)): // check it here
        if( $include_children ) {
            unset($query['attachment']);
            $parent = $term->parent;
            while( $parent ) {
                $parent_term = get_term( $parent, $tax_name);
                $name = $parent_term->slug . '/' . $name;
                $parent = $parent_term->parent;
            }
        } else { unset($query['name']); }
        switch( $tax_name ):
            case 'category':{
                $query['category_name'] = $name; // for categories
                break;
            }
            case 'post_tag':{
                $query['tag'] = $name; // for post tags
                break;
            }
            default:{
                $query[$tax_name] = $name; // for another taxonomies
                break;
            }
        endswitch;
    endif;
    return $query;
}
add_filter( 'term_link', 'writeTermPerm', 10, 3 );
function writeTermPerm( $url, $term, $taxonomy ){
    $taxonomy_name = 'services';
    $taxonomy_slug = 'services';
    if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;
    $url = str_replace('/'.$taxonomy_slug, '/portfolio', $url);
    return $url;
}

在修复了url结构之后,我将以下代码添加到我的taxonomy-services.php文档中,以便在一个文件中控制整个系统。

locate_template( 'archive-portfolio.php', true );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM