去除CPT和CT中的碱液段塞,在永久链接中使用CT

时间:2016-01-28 作者:f8m

我已经走了很长一段路,但还是没法让它发挥作用。

我想从我的自定义帖子类型中删除base slugreview 和我的自定义分类法brand.

最终结果应为如下URL:https://example.org/apple/iphone7.

现在我知道了:https://example.org/review/apple/iphone7.

我读了很多书,我知道其后果和可能的冲突,WordPress并不是为处理此类重写而设计的。但必须有一种方法来实现我所要做的。

代码位于http://www.markwarddesign.com/2014/02/remove-custom-post-type-slug-permalink/ 可以移除基本段塞,但我无法将下面的功能与我的功能结合起来。只要我添加markwarddesign中的函数。com结果为404。

我尝试了发布在Custom Taxonomy specific to a Custom Post type 但它仍然含有碱液。

请查看我的设置。

function brand_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, \'%brand%\') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, \'brand\');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = \'other\';

    return str_replace(\'%brand%\', $taxonomy_slug, $permalink);
}
add_filter(\'post_link\', \'brand_permalink\', 1, 3);
add_filter(\'post_type_link\', \'brand_permalink\', 1, 3);

/**
 * Code below is from https://wordpress.stackexchange.com/questions/57493/custom-taxonomy-specific-to-a-custom-post-type
 * A custom taxonomy is created and linked to CPT \'review\'.
 * The goal is to create permalinks containing the taxonomy + CPT post name, e.g. /some-brand/xyz-review/
 */
function custom_brand_taxonomy() {
    register_taxonomy(
        \'brand\',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        \'review\',        //post type name
        array(  
            \'hierarchical\' => true,  
            \'label\' => \'Brand\',  //Display name
            \'query_var\' => true,
            \'rewrite\' => array(
                \'slug\' => \'\', // This controls the base slug that will display before each term
                \'with_front\' => false // Don\'t display the category base before 
            )
        )  
    );  
}  
add_action( \'init\', \'custom_brand_taxonomy\');

/**
 * Creating a function to create our CPT
 *
 */
function xyz_custom_post_types() {

    // Set options for Custom Post Type REVIEW
    $review_args = array(
        \'label\'               => __( \'review\', \'mythemexyz\' ),
        \'description\'         => __( \'Descrption review bla bla\', \'mythemexyz\' ),
        \'labels\'              => array(
                                \'name\'                => _x( \'reviewe\', \'Post Type General Name\', \'mythemexyz\' ),
                                \'singular_name\'       => _x( \'review\', \'Post Type Singular Name\', \'mythemexyz\' ),
                                \'menu_name\'           => __( \'reviewe\', \'mythemexyz\' ),
                                \'parent_item_colon\'   => __( \'Parent review\', \'mythemexyz\' ),
                                \'all_items\'           => __( \'Alle reviewe\', \'mythemexyz\' ),
                                \'view_item\'           => __( \'review ansehen\', \'mythemexyz\' ),
                                \'add_new_item\'        => __( \'review erstellen\', \'mythemexyz\' ),
                                \'add_new\'             => __( \'Erstellen\', \'mythemexyz\' ),
                                \'edit_item\'           => __( \'review bearbeiten\', \'mythemexyz\' ),
                                \'update_item\'         => __( \'review aktualisieren\', \'mythemexyz\' ),
                                \'search_items\'        => __( \'review suchen\', \'mythemexyz\' ),
                                \'not_found\'           => __( \'Nicht gefunden\', \'mythemexyz\' ),
                                \'not_found_in_trash\'  => __( \'Nicht in Papierkorb gefunden\', \'mythemexyz\' ),
                                ),
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'revisions\', \'custom-fields\', \'page-attributes\' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 99,
        \'can_export\'          => true,
        \'rewrite\'             => array( \'slug\' => \'review/%brand%\', \'with_front\' => false ),
        \'has_archive\'         => \'review\',
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );

    // Registering your Custom Post Type
    register_post_type( \'review\', $review_args );

}

/* Hook into the \'init\' action so that the function
 * Containing our post type registration is not 
 * unnecessarily executed. 
 */
add_action( \'init\', \'xyz_custom_post_types\', 0 );
非常感谢您的帮助。

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

我按照上面的方式复制了您的代码,将其粘贴到了2616主题中,并将post类型重写段塞从review/%brand%%brand%. 这使得术语归档和评论帖子都具有您想要的URL结构并成功显示。

现在坏消息是,为分类法和post类型生成的重写规则践踏了post和页面重写规则。请求发帖会导致WordPress试图找到一个与你的帖子匹配的品牌术语。对父页/子页的请求导致WordPress尝试查询与子页匹配的审阅帖子。

好消息是我们可以解决这个问题。当WordPress解析请求时,将填充所有可能匹配的查询变量,并通过request filter 在那里我们可以修改和转换它们。

要查看此筛选器,请尝试:

function test_request( $request ){
    echo \'<pre>\';
    print_r($request);
    echo \'</pre>\';
}
add_filter( \'request\', \'test_request\' );
然后访问不同类型的页面,查看填充了哪些查询变量。

我拼凑了一个快速的例子,修复了基本的帖子和子页面显示。这还不包括所有可能损坏的东西。我还没有测试分页的帖子,附件,其他东西??如果您有其他帖子类型,或者您的帖子permalink结构发生变化,这也可能会发生变化。但这应该给你一个让事情运转起来的起点。

function wpd_fix_requests( $request ){

    // if it\'s a brand term request
    // see if a brand term exists by this name with get_term_by
    // if not, reset the query to a post or page with name
    if( array_key_exists( \'brand\' , $request )
        && ! array_key_exists( \'post_type\' , $request )
        && ! get_term_by( \'slug\', $request[\'brand\'], \'brand\' ) ){
            $request[\'name\'] = $request[\'brand\'];
            $request[\'post_type\'] = array( \'post\', \'page\' );
            unset( $request[\'brand\'] );
    }

    // if this is a review request
    // add page to post type in case it\'s a child page
    if( array_key_exists( \'review\', $request ) ){
        $request[\'post_type\'] = array( \'review\', \'page\' );
    }

    // return request vars
    return $request;
}
add_filter( \'request\', \'wpd_fix_requests\' );

SO网友:f8m

对于任何感兴趣的人来说,这是我问题的完整解决方案。到目前为止,效果相当好。

我使用%postname% 将我的自定义分类法作为我的Brand 和自定义帖子类型作为我的Product.

我的URL如下所示example.org/some-brand/some-product 而我可以在上显示概述example.org/some-brand/.

我正在使用高级自定义字段Pro插件使我的所有页面都可编辑。功能wp323_get_template_file 实现@Milo建议的内容,因为对页面或具有自定义页面模板的页面的所有请求都被重定向到了single。php。现在页面按页面显示。php,如果有自定义页面模板,则会显示正确的模板。

每一个Product 必须链接到Brand, 否则URL将包含/other/ 而不是品牌。

希望这对某人有所帮助,我花了几个晚上才完成。感谢@Milo。

 <?php

 function wpd_fix_requests( $request ){
     // Written by @Milo: https://wordpress.stackexchange.com/questions/215987/remove-base-slug-in-cpt-ct-use-ct-in-permalink
     // if it\'s a brand term request
     // see if a brand term exists by this name with get_term_by
     // if not, reset the query to a post or page with name
     if( array_key_exists( \'brand\' , $request )
         && ! array_key_exists( \'post_type\' , $request )
         && ! get_term_by( \'slug\', $request[\'brand\'], \'brand\' ) ){
             $request[\'name\'] = $request[\'brand\'];
             $request[\'post_type\'] = array( \'post\', \'page\' );
             unset( $request[\'brand\'] );
     }

     // if this is a review request
     // add page to post type in case it\'s a child page
     if( array_key_exists( \'review\', $request ) ){
         $request[\'post_type\'] = array( \'review\', \'page\' );
     }

     // return request vars
     return $request;
 }
 add_filter( \'request\', \'wpd_fix_requests\' );

 function brand_permalink($permalink, $post_id, $leavename) {
     if (strpos($permalink, \'%brand%\') === FALSE) return $permalink;
         // Get post
         $post = get_post($post_id);
         if (!$post) return $permalink;

         // Get taxonomy terms
         $terms = wp_get_object_terms($post->ID, \'brand\');
         if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
             $taxonomy_slug = $terms[0]->slug;
         else $taxonomy_slug = \'other\';

     return str_replace(\'%brand%\', $taxonomy_slug, $permalink);
 }
 add_filter(\'post_link\', \'brand_permalink\', 1, 3);
 add_filter(\'post_type_link\', \'brand_permalink\', 1, 3);

 /**
  * Code below is from https://wordpress.stackexchange.com/questions/57493/custom-taxonomy-specific-to-a-custom-post-type
  * A custom taxonomy is created and linked to CPT \'review\'.
  * The goal is to create permalinks containing the taxonomy + CPT post name, e.g. /some-brand/xyz-review/
  */
 function custom_brand_taxonomy() {
     register_taxonomy(
         \'brand\',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
         \'review\',        //post type name
         array(  
             \'hierarchical\' => true,  
             \'label\' => \'Brand\',  //Display name
             \'query_var\' => true,
             \'rewrite\' => array(
                 \'slug\' => \'/\', // This controls the base slug that will display before each term
                 \'with_front\' => false // Don\'t display the category base before 
             )
         )  
     );  
 }  
 add_action( \'init\', \'custom_brand_taxonomy\');

 /**
  * Creating a function to create our CPT
  *
  */
 function xyz_custom_post_types() {

     // Set options for Custom Post Type REVIEW
     $review_args = array(
         \'label\'               => __( \'review\', \'mythemexyz\' ),
         \'description\'         => __( \'Descrption review bla bla\', \'mythemexyz\' ),
         \'labels\'              => array(
                                 \'name\'                => _x( \'reviewe\', \'Post Type General Name\', \'mythemexyz\' ),
                                 \'singular_name\'       => _x( \'review\', \'Post Type Singular Name\', \'mythemexyz\' ),
                                 \'menu_name\'           => __( \'reviewe\', \'mythemexyz\' ),
                                 \'parent_item_colon\'   => __( \'Parent review\', \'mythemexyz\' ),
                                 \'all_items\'           => __( \'Alle reviewe\', \'mythemexyz\' ),
                                 \'view_item\'           => __( \'review ansehen\', \'mythemexyz\' ),
                                 \'add_new_item\'        => __( \'review erstellen\', \'mythemexyz\' ),
                                 \'add_new\'             => __( \'Erstellen\', \'mythemexyz\' ),
                                 \'edit_item\'           => __( \'review bearbeiten\', \'mythemexyz\' ),
                                 \'update_item\'         => __( \'review aktualisieren\', \'mythemexyz\' ),
                                 \'search_items\'        => __( \'review suchen\', \'mythemexyz\' ),
                                 \'not_found\'           => __( \'Nicht gefunden\', \'mythemexyz\' ),
                                 \'not_found_in_trash\'  => __( \'Nicht in Papierkorb gefunden\', \'mythemexyz\' ),
                                 ),
         // Features this CPT supports in Post Editor
         \'supports\'            => array( \'title\', \'editor\', \'revisions\', \'custom-fields\', \'page-attributes\' ),
         /* A hierarchical CPT is like Pages and can have
         * Parent and child items. A non-hierarchical CPT
         * is like Posts.
         */  
         \'hierarchical\'        => false,
         \'public\'              => true,
         \'show_ui\'             => true,
         \'show_in_menu\'        => true,
         \'show_in_nav_menus\'   => true,
         \'show_in_admin_bar\'   => true,
         \'menu_position\'       => 99,
         \'can_export\'          => true,
         \'rewrite\'             => array( \'slug\' => \'%brand%\', \'with_front\' => false ),
         \'has_archive\'         => \'review\',
         \'exclude_from_search\' => false,
         \'publicly_queryable\'  => true,
         \'capability_type\'     => \'page\',
     );

     // Registering your Custom Post Type
     register_post_type( \'review\', $review_args );

 }

 /* Hook into the \'init\' action so that the function
  * Containing our post type registration is not 
  * unnecessarily executed. 
  */
 add_action( \'init\', \'xyz_custom_post_types\', 0 );

 /**
  * Use single_template filter to properply redirect to page.php and custom page templates
  */
 function wp323_get_template_file($single_template) {
      global $post;

      $page_custom_template = get_post_meta( $post->ID, \'_wp_page_template\', true );

      if ($post->post_type == \'page\') {
        if($page_custom_template != \'default\') {
            $single_template = dirname( __FILE__ ) . \'/\' . $page_custom_template;
        }
        else {
            $single_template = dirname( __FILE__ ) . \'/page.php\';
        }

      }
      return $single_template;
 }
 add_filter( \'single_template\', \'wp323_get_template_file\' );

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?