如何创建具有自定义分类和自定义帖子类型的固定链接结构

时间:2018-02-19 作者:harry

我的自定义分类法和自定义帖子类型如下-

// Creating a house Custom Post Type
function house_custom_post_type() {
    $labels = array(
        \'name\'                => __( \'house\' ),
        \'singular_name\'       => __( \'house\'),
        \'menu_name\'           => __( \'house\'),
        \'parent_item_colon\'   => __( \'Parent house\'),
        \'all_items\'           => __( \'All house\'),
        \'view_item\'           => __( \'View house\'),
        \'add_new_item\'        => __( \'Add New house\'),
        \'add_new\'             => __( \'Add New house\'),
        \'edit_item\'           => __( \'Edit house\'),
        \'update_item\'         => __( \'Update house\'),
        \'search_items\'        => __( \'Search house\'),
        \'not_found\'           => __( \'Not Found\'),
        \'not_found_in_trash\'  => __( \'Not found in Trash\')
    );
    $args = array(
        \'label\'               => __( \'house\'),
        \'description\'         => __( \'Best house\'),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'revisions\', \'custom-fields\'),
        \'public\'              => true,
        \'hierarchical\'        => false,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'has_archive\'         => true,
        \'can_export\'          => true,
        \'exclude_from_search\' => false,
        \'yarpp_support\'       => true,
        \'taxonomies\'          => array( \'post_tag\' ),
        \'rewrite\'             => array( \'slug\' => \'%house_type%\', \'with_front\' => false ), 
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\'
    );
    register_post_type( \'house\', $args );
}
add_action( \'init\', \'house_custom_post_type\', 0 );

//create a custom taxonomy name it "house_type" for your posts
function house_create_custom_taxonomy() {
    $labels = array(
        \'name\'              => _x( \'house_type\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'house_type\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search house_type\' ),
        \'all_items\'         => __( \'All house_type\' ),
        \'parent_item\'       => __( \'Parent house_type\' ),
        \'parent_item_colon\' => __( \'Parent house_type: \' ),
        \'edit_item\'         => __( \'Edit house_type\' ), 
        \'update_item\'       => __( \'Update house_type\' ),
        \'add_new_item\'      => __( \'Add New house_type\' ),
        \'new_item_name\'     => __( \'New house_type Name\' ),
        \'menu_name\'         => __( \'house_type\' ),
    );

    register_taxonomy( \'house_type\', array( \'house\' ), array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'house\' ),
    ) );
}
add_action( \'init\', \'house_create_custom_taxonomy\', 0 );

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set( \'post_type\', array( \'post\', \'house\' ) );
    }
    return $query;
}
add_action( \'pre_get_posts\', \'add_my_post_types_to_query\' );

function mmp_rewrite_rules( $rules ) {
    $newRules = array();
    $newRules[\'basename/(.+)/?$\']                = \'index.php?house_type=$matches[1]\'; 
    $newRules[\'basename/(.+)/(.+)/(.+)/(.+)/?$\'] = \'index.php?house=$matches[4]\'; // my custom structure will always have the post name as the 5th uri segment
    return array_merge( $newRules, $rules );
}
add_filter( \'rewrite_rules_array\', \'mmp_rewrite_rules\' );

function filter_post_type_link( $link, $post ) {
    if ( $post->post_type != \'house\') {
        return $link;
    }

    if ( $cats = get_the_terms( $post->ID, \'house_type\' ) ) {
        $link = str_replace( \'%house_type%\', get_taxonomy_parents( array_pop( $cats )->term_id, \'house_type\', false, \'/\', true ), $link ); // see custom function defined below
    }
    return $link;
}
add_filter( \'post_type_link\', \'filter_post_type_link\', 10, 2 );

function get_taxonomy_parents( $id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array() ) {
    $chain = \'\';
    $parent = &get_term( $id, $taxonomy );

    if ( is_wp_error( $parent ) ) {
        return $parent;
    }

    if ( $nicename ) {
        $name = $parent->slug;
    } else {
        $name = $parent->name;
    }

    if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) ) {
        $visited[] = $parent->parent;
        $chain .= get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
    }

    if ( $link ) {

    } else {
        $chain .= $name . $separator;
    }
    return $chain;
}
我在本地主机中使用此自定义功能,所有功能都运行良好,但在此过程中我遇到了两个错误,我的permalink结构帖子类型是:

(1)http://localhost/project/house/bunglow/5bhk//my-houses/

但在5BHK附近创建了两个分离器5bhk//my-houses/ 我想删除一个分隔符,我只想要一个这样的分隔符:http://localhost/project/house/bunglow/5bhk/my-houses/ 在里面parent-tax/child-tax/ 总体安排

2) 并打开他们提供的链接

哎呀!找不到该页

此链接显示此错误显示未找到页面。我下面的函数有什么问题。请帮帮我,这很紧急。

1 个回复
SO网友:Beee

我怀疑额外的斜杠来自str\\u replace操作,因为通常在检索税务URL时不需要手动添加斜杠。

由于您现在得到的是一个不需要的斜杠,因此我建议您在有/没有斜杠的情况下进行测试。从删除函数定义中的一个开始。把它清空,看看会发生什么。

在我看来,这是调查的第一步。我想这篇文章也会对你有所帮助:https://codex.wordpress.org/Debugging_in_WordPress

结束

相关推荐

How to replace permalinks

我已将wordpress项目导出并导入live server。现在的问题是,我将permalink结构作为“localhost”格式。当我单击网站中的任何链接时,它会将我重定向到localhost。我怎样才能改变这一点?我的htaccess文件如下所示<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /myproject/ RewriteRule ^index\\.php$ - [L] RewriteCo