自定义帖子类型和类别链接在一起

时间:2016-04-29 作者:shapic94

我对为此创建新页面有异议:

类别链接为:

/category/category_name

但我想:

/custom_post_type/category_name

自定义邮政类型代码

$labels     =   array(
        \'name\'                  => \'Vege\',
        \'singular_name\'         => \'Vege\',
        \'add_new\'               => \'Add Item\',
        \'all_items\'             => \'All Items\',
        \'add_new_item\'          => \'Add Item\',
        \'edit_item\'             => \'Edit Item\',
        \'new_item\'              => \'New Item\',
        \'view_item\'             => \'View Item\',
        \'search_item\'           => \'Search Recepti\',
        \'not_found\'             => \'No Items found\',
        \'not_found_in_trash\'    => \'No items found in trash\',
        \'parent_item_colon\'     => \'Parent Item\'
    );

    $args       =   array(
        \'labels\'                => $labels,
        \'public\'                => true,
        \'has_archive\'           => true,
        \'publicly_queryable\'    => true,
        \'query_var\'             => true,
        \'rewrite\'               => true,
        \'capability_type\'       => \'post\',
        \'hierarchical\'          => false,
        \'supports\'              => array(
            \'title\',
            \'editor\',
            \'excerpt\',
            \'thumbnail\',
            \'revisions\'
        ),
        \'taxonomies\'            => array(
            \'category\',
            \'post_tag\'
        ),
        \'menu_position\'         => 100,
        \'menu_icon\'             => \'dashicons-wordpress\',
        \'exclude_from_search\'   => false
    );
    register_post_type( \'vege\', $args );
永磁线

http://prntscr.com/ayfqaf

http://prntscr.com/ayfq03

有可能吗?

1 个回复
SO网友:Howard E

希望这就是你想要的?

确保之后更新永久链接(只需单击更新)。

这将完成您的网站。com/vege/%vege类别%/%帖子%

在您的示例中,您没有注册分类法类型,另外还需要添加函数以将类别slug放入永久链接。

function my_custom_post_vege() {
    $labelsvege = array(
         \'name\'                 => \'Vege\',
        \'singular_name\'         => \'Vege\',
        \'add_new\'               => \'Add Item\',
        \'all_items\'             => \'All Items\',
        \'add_new_item\'          => \'Add Item\',
        \'edit_item\'             => \'Edit Item\',
        \'new_item\'              => \'New Item\',
        \'view_item\'             => \'View Item\',
        \'search_item\'           => \'Search Recepti\',
        \'not_found\'             => \'No Items found\',
        \'not_found_in_trash\'    => \'No items found in trash\',
        \'parent_item_colon\'     => \'Parent Item\'
    );
    $argsvege = array(
        \'labels\'        => $labelsvege,
        \'description\'   => \'What type of Vege this is\',
        \'public\'        => true,
        \'menu_position\' => 100,
        \'menu_icon\'     => \'dashicons-wordpress\',
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\' ),
        \'has_archive\'   => true,
        \'hierarchical\'  => true,
        \'rewrite\'       => array(\'slug\' => \'vege/%vege-category%\',\'with_front\' => false),
        \'query_var\'     => true,
        \'show_in_nav_menus\'  => TRUE,
        \'show_in_menu\'  => TRUE,
        \'label\'         => \'Veges\',
        \'publicly_queryable\'  => TRUE
    );
    register_post_type( \'vege\', $argsvege );
}

add_action( \'init\', \'my_custom_post_vege\' );

function my_taxonomies_vegetype() {
    $labelsTaxvegetype = array(
        \'name\'              => _x( \'Vege Category\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Vege Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search vege Category\' ),
        \'all_items\'         => __( \'All vege Category\' ),
        \'parent_item\'       => __( \'Parent Item\' ),
        \'parent_item_colon\' => __( \'vege Type:\' ),
        \'edit_item\'         => __( \'Edit vege Type\' ),
        \'update_item\'       => __( \'Update vege Type\' ),
        \'add_new_item\'      => __( \'Add New vege Type\' ),
        \'new_item_name\'     => __( \'New vege Type Name\' ),
        \'menu_name\'         => __( \'vege Type\' ),
    );
    $argsTaxvegetype = array(
        \'labels\' => $labelsTaxvegetype,
        \'hierarchical\'  => true,
        \'public\'        => true,
        \'query_var\'     => \'category\',
        \'rewrite\'       =>  array(\'slug\' => \'vege\' ),
        \'_builtin\'      => false,
    );
    register_taxonomy( \'vege-category\', \'vege\', $argsTaxvegetype );
}

add_action( \'init\', \'my_taxonomies_vegetype\', 0 );

add_filter(\'post_link\', \'category_permalink\', 1, 3);
add_filter(\'post_type_link\', \'category_permalink\', 1, 3);

function category_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, \'%vege-category%\') === FALSE) return $permalink;
        $post = get_post($post_id);
        if (!$post) return $permalink;
        $terms = wp_get_object_terms($post->ID, \'vege-category\');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = \'no-category\';

    return str_replace(\'%vege-category%\', $taxonomy_slug, $permalink);
}