繁体   English   中英

自定义帖子类型分类删除类别 slug

[英]Custom Post Type Taxonomy remove category slug

我创建了一个自定义帖子类型“商店”,其中包含商店类别的自定义分类

对于我的商店类别存档页面,我希望 URL 结构为:

example.com/shop/tech/ (而不是 example.com/shop/category/tech/)

我希望商店帖子成为example.com/shop/shop-post-title-example

我已经尝试了下面的代码并保存了永久链接,但是当我访问商店帖子时,它显示 404 错误。

是否可以毫无错误地删除类别库?

// Custom Post Type : Shop  
function my_custom_post_shop() {
  $labels = array(
    'name'               => _x( 'Shop', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'Product' ),
    'add_new_item'       => __( 'Add New' ),
    'edit_item'          => __( 'Edit' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No projects found' ),
    'not_found_in_trash' => __( 'No projects found in the Trash' ), 
    'menu_name'          => 'Shop'
  );
  $args = array(
        'label'               => __( 'Shop' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'comments', 'revisions', 'custom-fields', 'thumbnail', ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'rewrite' => array( 'slug' => 'shop' ),
        'has_archive' => 'shop',
  );
  register_post_type( 'shop', $args ); 
}
add_action( 'init', 'my_custom_post_shop' );

// Shop Categories
function my_taxonomies_shop() {
  $labels = array(
    'name'              => _x( 'Shop Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Shop Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Categories' ),
    'all_items'         => __( 'All Categories' ),
    'parent_item'       => __( 'Parent Category' ),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item'         => __( 'Edit Category' ), 
    'update_item'       => __( 'Update Category' ),
    'add_new_item'      => __( 'Add New Category' ),
    'new_item_name'     => __( 'New Category' ),
    'menu_name'         => __( 'Shop Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'show_admin_column' => true,
    'rewrite' => array( 'slug' => 'shop'),
  );
  register_taxonomy( 'shop_category', 'shop', $args );
}
add_action( 'init', 'my_taxonomies_shop', 0 );

据我所知,为了显示自定义帖子类型和/或其类别页面,您需要先创建模板文件(对应的 PHP 文件)。 模板文件会提取您的帖子并显示它们。

例如,要显示您的类别“shop_category”,您可以创建一个名为“taxonomy-shop_category.php”的文件。
如果您想专门为“tech”商店类别创建模板,那么您可以创建一个名为“taxonomy-shop_category_tech.php”的文件。
模板有一个层次结构: https://wphierarchy.com/关于模板的更多信息: https://developer.Z1870A829D9BC69ABF500ECA6F00241-FEZ.org/themes/template-tax
关于自定义帖子类型分类法的好文章: https://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-post-type--cms-20340

模板文件应放在您的 WordPress 主题文件夹(或子主题文件夹)中
。重要的。 不要忘记更新您的永久链接。 Go 到 /wp-admin/options-permalink.php 并单击“保存更改”。

下面是一个简单模板文件的示例:

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
 
<?php get_header(); 
$term = get_queried_object();

$args = array(
    'post_type' => 'shop',
    'shop-category' => $term->slug
);
$query = new WP_Query( $args );

?>
 <header class="archive-header">
    <h1 class="archive-title">
        <?php echo $term->name; ?>
    </h1>
</header>
        <div id="content">
                 <?php
                 if ($query->have_posts()) {
                         
                    echo'<h2>Your shop category name ' . $term->name . '</h2>';
                     
                    echo '<ul>';
                     
                        while ( $query->have_posts() ) : $query->the_post(); ?>
                 
                        <li id="post-<?php the_ID(); ?>">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </li>
                         
                        <?php endwhile;
                         
                        echo '</ul>';
                         
                } 
                     
                wp_reset_postdata();
                ?>             
             
        </div><!-- #content -->

<?php
get_footer();

您可以使用以下方法删除自定义分类 URL 中的类别库:

$args = array(

    'rewrite' => array( 'slug' => '/', 'with_front' => false),

    /* ...(other args)... */
);
register_taxonomy( 'shop_category', 'shop', $args );

并重新保存永久链接设置。

但是,如果您将永久链接设置为Post name ,这将使您的(基本帖子类型)帖子 go 404 似乎您不能同时将自定义分类法和帖子都驻留在根目录中。 因此,您将 go 设置为永久链接设置并设置Custom structure ,例如/blog/%postname%/

副作用是您的 CPT 也会有这个“正面”,例如blog/yourcustomtype 您可以在产品类型注册中使用'with_front' => false来消除此问题:

register_post_type( 'yourcustomtype',  array(
           'rewrite' => array(
                'slug' => 'yourcustomurl',
                'with_front' => false
            ),
            /* ... */
));

这里找到。

暂无
暂无

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

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