无法从自定义分类类别页面的固定链接中删除正面

时间:2019-07-05 作者:DeltaG

我创建了一个名为“portfolio”的自定义帖子类型和一个名为“portfolio categorie”的自定义分类法。虽然我在页面中看到了新的类别,但我在从url结构中删除“front”时遇到了一个问题。我的首选url是domain.com/portfolio/category-name/domain.com/portfolio/categorie/catergory-name 也是一种选择。

在我的永久链接设置中,我将此作为一种结构(用于博客):/blog/%postname%/

当我访问自定义分类类别页面时,我会看到一个404错误页面。我确实重写了permalinks。

如果我将with\\U front更改为\'with_front\' => false WordPress返回带有url的页面/blog/portfolio/portfolio-name/

我做错了什么?

迄今为止我的代码:

function custom_post_type() {

    $labels = array(
        \'name\'                  => _x( \'Portfolio\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'         => _x( \'Project\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'             => __( \'Portfolio\', \'text_domain\' ),
        \'name_admin_bar\'        => __( \'Portfolio\', \'text_domain\' ),
        \'archives\'              => __( \'Item Archives\', \'text_domain\' ),
        \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'             => __( \'Alle Projecten\', \'text_domain\' ),
        \'add_new_item\'          => __( \'Voeg nieuw Project toe\', \'text_domain\' ),
        \'add_new\'               => __( \'Voeg toe\', \'text_domain\' ),
        \'new_item\'              => __( \'Nieuw Project\', \'text_domain\' ),
        \'edit_item\'             => __( \'Bewerk Project\', \'text_domain\' ),
        \'update_item\'           => __( \'Pas Project aan\', \'text_domain\' ),
        \'view_item\'             => __( \'Bekijk Project\', \'text_domain\' ),
        \'search_items\'          => __( \'Doorzoek Project\', \'text_domain\' ),
        \'not_found\'             => __( \'Niet gevonden\', \'text_domain\' ),
        \'not_found_in_trash\'    => __( \'Niet gevonden in de prullenmand\', \'text_domain\' ),
        \'featured_image\'        => __( \'Project Hoofdafbeelding\', \'text_domain\' ),
        \'set_featured_image\'    => __( \'Stel Hoofdafbeelding in\', \'text_domain\' ),
        \'remove_featured_image\' => __( \'Verwijder hoofdafbeelding\', \'text_domain\' ),
        \'use_featured_image\'    => __( \'Gebruik als hoofdafbeelding\', \'text_domain\' ),
        \'insert_into_item\'      => __( \'Voeg toe aan project\', \'text_domain\' ),
        \'uploaded_to_this_item\' => __( \'Geupload naar dit project\', \'text_domain\' ),
        \'items_list\'            => __( \'Project lijst\', \'text_domain\' ),
        \'items_list_navigation\' => __( \'Project navigatie\', \'text_domain\' ),
        \'filter_items_list\'     => __( \'Filter project lijst\', \'text_domain\' ),
    );
    $args = array(
        \'label\'                 => __( \'Portfolio\', \'text_domain\' ),
        \'description\'           => __( \'Recente projecten\', \'text_domain\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'thumbnail\', ),
        \'hierarchical\'          => true,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'menu_icon\'             => \'dashicons-images-alt\',
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => true,
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\'       => \'page\',
        \'rewrite\'               => array( \'with_front\' => false ),
    );
    register_post_type( \'portfolio\', $args );
}
add_action( \'init\', \'custom_post_type\', 0 );


function portfolio_custom_taxonomy() {

  $labels = array(
    \'name\' => _x( \'Categorieën\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Categorie\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Zoek categorieën\' ),
    \'all_items\' => __( \'Alle categorieën\' ),
    \'parent_item\' => __( \'Bovenliggende categorie\' ),
    \'parent_item_colon\' => __( \'Bovenliggende categorie:\' ),
    \'edit_item\' => __( \'Bewerk categorie\' ), 
    \'update_item\' => __( \'Update categorie\' ),
    \'add_new_item\' => __( \'Nieuwe categorie toevoegen\' ),
    \'new_item_name\' => __( \'Nieuwe categorie\' ),
    \'menu_name\' => __( \'Categorieën\' ),
  );    

  register_taxonomy(\'portfolio-categorie\', \'portfolio\', array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'portfolio\', \'with_front\' => false ),
  ));
}

add_action( \'init\', \'portfolio_custom_taxonomy\', 0 );
任何帮助都将不胜感激!

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

获取URL,如example.tld/portfolio/PORTFOLIO-CATEGORY-NAME/PORTFOLIO-NAME/ 执行以下操作。

改变rewrite 自定义帖子类型为

\'rewrite\' => array(
    \'slug\'       => \'portfolio/%portfolio-categorie%\',
    \'with_front\' => false
),
更改rewrite 对于自定义分类法

\'rewrite\' => array(
    \'with_front\' => false
),
使用post_type_link 要更换的过滤器%portfolio-categorie% 使用实际的分类术语:

add_filter( \'post_type_link\', \'my_portfolio_permalink\', 10, 4 );

function my_portfolio_permalink( $post_link, $post, $leavename, $sample ) {
    if ( false !== strpos( $post_link, \'%portfolio-categorie%\' ) ) {

        $portfolio_cat_term = get_the_terms( $post->ID, \'portfolio-categorie\' );

        if ( !empty( $portfolio_cat_term ) ) {
            $post_link = str_replace( \'%portfolio-categorie%\', array_pop( $portfolio_cat_term )->
        slug, $post_link );
        } else {
            $post_link = str_replace( \'%portfolio-categorie%\', \'uncategorized\', $post_link );
        }
    }
    return $post_link;
}

相关推荐

如何将自定义选项添加到wp_Dropdown_Categories?

我需要将自定义选项添加到wp_dropdown_categories. 现在,整个万维网世界还没有找到解决方案。。。因此,我在这里要求一个解决方案……因为我真的无法想象WordPress的开发人员没有考虑到这将永远不需要,对吗?