如何在不使用插件和 woocommerce 的情况下将子类别添加到产品目录的自定义帖子类型

问题描述 投票:0回答:1

我陷入了问题。我创建了一个自定义帖子类型,即“产品”。在这篇自定义帖子中,我添加了一个分类类别名称作为“产品类别”。在产品类别中,我添加了六个类别,即太阳镜、隐形眼镜等。之后,我只需添加诸如雷朋、coopervision 等品牌名称的帖子,并将它们分配给这些产品类别。单击主要类别(例如太阳镜=>)后,它会将我们重定向到显示所有品牌的单个页面。现在,点击这些单一品牌后,我想将其重定向到另一个单一主页,其中它将显示该特定品牌的产品。但此刻,如果我点击一个品牌,它会重定向到其单个页面。 我想要的是点击品牌产品后应该出现单页。

我得到的:产品/太阳镜/雷朋

我想要什么:产品/太阳镜/雷朋/雷朋-产品页面/单产品。

如果您不明白我的意思,我附加一个链接,我想要与产品导航项中相同的结果...产品=>下拉=>类别>类别主页=>子类别主页=>产品名称/项目页面。

这里检查产品导航菜单项

P.S:如果可能的话,我是初学者,请向我发送所有这些的完整代码和配置。

我已经尝试创建另一个名为产品项目的类别,但无法获得结果。

我已经在两个 php 文件中获取了它。

这是我的functions.php

/****new custom functions**********/

// Create Custom Post type called Products
function register_products_post_type() {
    $args = array(
        'labels'    => array(
            'name'               => __( 'Products', 'mt-products' ),
            'singular_name'      => __( 'Products', 'mt-products' ),
            'menu_name'          => __( 'Products', 'mt-products' ),
            'name_admin_bar'     => __( 'Products', 'mt-products' ),
            'add_new'            => __( 'Add New', 'mt-products' ),
            'add_new_item'       => __( 'Add New Products', 'mt-products' ),
            'new_item'           => __( 'New Products', 'mt-products' ),
            'edit_item'          => __( 'Edit Products', 'mt-products' ),
            'view_item'          => __( 'View Products', 'mt-products' ),
            'all_items'          => __( 'All Products', 'mt-products' ),
            'search_items'       => __( 'Search Products', 'mt-products' ),
            'parent_item_colon'  => __( 'Parent Products:', 'mt-products' ),
            'not_found'          => __( 'No Products found.', 'mt-products' ),
            'not_found_in_trash' => __( 'No Products found in Trash.', 'mt-products' )
        ),
        'query_var'              => 'products',
        'rewrite'                => array(
            'slug'               => 'products/%product_category%',
            'with_front'         => false
        ),
        'public'                 => true,  // If you don't want it to make public, make it false
        'publicly_queryable'     => true,  // you should be able to query it
        'show_ui'                => true,  // you should be able to edit it in wp-admin
        'has_archive'            => 'products',    //true,
        'menu_position'          => 51,
        'supports'               => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'taxonomies'             => array( 'product_category'),
    );
    flush_rewrite_rules();

    register_post_type('mt_products', $args);
}
add_action( 'init', 'register_products_post_type' );


/* Register custom taxonomy */

function taxonomies() {
    $taxonomies = array();

    $taxonomies['product_category'] = array(
        'hierarchical'  => true,
        'query_var'     => 'product-category',
        'rewrite'       => array(
            'slug'      => 'product/category'
        ),
        'labels'            => array(
            'name'          => 'Product Category',
            'singular_name' => 'Product Category',
            'edit_item'     => 'Edit Product Category',
            'update_item'   => 'Update Product Category',
            'add_new_item'  => 'Add Product Category',
            'new_item_name' => 'Add New Product Category',
            'all_items'     => 'All Product Category',
            'search_items'  => 'Search Product Category',
            'popular_items' => 'Popular Product Category',
            'separate_items_with_commas' => 'Separate Product Categories with Commas',
            'add_or_remove_items' => 'Add or Remove Product Categories',
            'choose_from_most_used' => 'Choose from most used categories',
        ),
        'show_admin_column' => true
    );

    flush_rewrite_rules();

    foreach( $taxonomies as $name => $args ) {
        register_taxonomy( $name, array( 'mt_products' ), $args );
    }
}
add_action( 'init', 'taxonomies' );

// filter url

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

    if ($cats = get_the_terms($post->ID, 'product_category'))
        $link = str_replace('%product_category%', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

这是我的第一个主要产品类别代码

<?php get_header();
$catSlug = $wp_query->get_queried_object()->slug;
?> 
<style>
            h1,h2,h3,h4,h5,h6,p{
                margin: 0;
                padding: 0;
            }
            .category-inner-wrapper{
                display: flex;
                flex-wrap: wrap;
                justify-content: center;
                flex-basis: 100%;
            }
            .product-block{
                flex-basis: 33%;
                min-height: 341px;
                text-align: center;
                flex-basis: 30%;
                min-height: 341px;
                text-align: center;
                margin-right: 26px;
            }
            .product-block:hover{
                border: 1px solid black;
                cursor: pointer;
            }
            .product-block-footer{
                background: #f2f2f2;
                padding: 15px 5px;
            }
            .product-block-footer h2{
              font-size: 20px;
            }
            .product-block-footer a{
                text-transform: uppercase;
                font-size: 12px;
                color: #000;
                border: 1px solid #415561;
                border-radius: 2px;
                padding: 10px 8px;
                display: inline-block;
                outline: none;
            }

        </style>
    </head>
    <body>
        <section class="categories">
            <div class="category-outer-wrapper">
                <div class="container">
                    <div class="category-inner-wrapper">
                    <?php

// The Loop
$args = array(
  'post_type' => 'mt_products',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'tax_query' => array(
        array(
            'taxonomy' => '',
            'field'    => 'slug',
            'terms'    => get_queried_object()->slug,
        ),
    ),
);
global $post;

$myposts = get_posts($args);

if ( $myposts ) {
    foreach ( $myposts as $post ) : 
        setup_postdata( $post ); ?>
                        <div class="product-block">
                            <div class="img-part">
                                <img src="<?php the_post_thumbnail_url('full');?>">
                            </div>
                            <div class="product-block-footer">
                                <h2><?php the_title(); ?></h2>
                                <a href="<?php the_permalink(); ?>">Enquire Now</a>
                            </div>
                        </div>
    <?php
    endforeach;
    wp_reset_postdata();
}
?>                            
                    </div>
                </div>
            </div>
        </section>
<?php get_footer(); ?>

这里是单页

<?php get_header();
$cat= $wp_query->get_queried_object();
$post = get_post();
?> 
<style>
            h1,h2,h3,h4,h5,h6,p{
                margin: 0;
                padding: 0;
            }
            section.brands{
                padding: 80px 0;
            }
            .brands-inner-wrapper{
                display: flex;
                flex-basis: 100%;
                flex-wrap: wrap;
                justify-content: space-between;
            }
            .brands-inner-wrapper .brand-content h3,h4,h5{
                font-size: 13px;
            }
            .brands-inner-wrapper .brand-content a{
                margin-top: 12px;
                display: inline-block;
                background: #2fcb41;
                border-radius: 0px;
                border: none;
                text-transform: uppercase;
                transition: all 0.4s ease-out;
                font-size: 14px;
                color: #fff;
                padding: 10px 16px;
            }
            .brands-inner-wrapper .brand-img{
                border: 1px solid #DEDEDE;
                flex-basis: 40%;
            }

            .brands-inner-wrapper  .brand-content{
                flex-basis: 50%;
            }

            .brands-inner-wrapper .brand-content p{
                width: 75%;
            }
            .brands-inner-wrapper .brand-content h2{
                color: #00a8ab;
                text-transform: uppercase;
            }
            .brands-inner-wrapper .brand-content h3{
                padding:25px 0 ;
            }
            .brands-inner-wrapper .brand-content h4{
                padding: 30px 0;
                text-transform: uppercase;
            }
        </style>
    </head>
    <body>
      <section class="brands">
            <div class="brands-outer-wrapper">
                <div class="container">
                    <div class="brands-inner-wrapper">

                         <div class="brand-img">
                            <img src=<?php the_post_thumbnail_url('full');?>>
                        </div>

                        <div class="brand-content">

                            <h2><?php echo $post->post_title;?></h2>
                            <h3><?php echo $post->post_title;?></h3>
                            <p><?php echo $post->post_content;?>.</p>

                            <h4>
<?php
$category_detail=wp_get_post_terms($post->ID,"product_category");
foreach($category_detail as $cd){
echo $cd->name." ";
}
?></h4>

        <h5>Sample</h5>
                            <a href="">Enquire Now</a>
                        </div>
                        <?php

// /// The Loop
// $args = array(
//   'post_type' => 'mt_products',
//   'post_status' => 'publish',
//   'posts_per_page' => -1
// );
// global $post;

// $myposts = get_posts($args);

// if ( $myposts ) {
//     foreach ( $myposts as $post ) : 
//         setup_postdata( $post ); 
//     endforeach;
//     wp_reset_postdata();
// }
?>                            
                    </div>
                </div>
            </div>
        </section>
<?php get_footer(); ?>

php wordpress categories custom-post-type product-catalog
1个回答
-1
投票

首先查看模板层次结构

然后考虑使用自定义链接包装您的目标。

为了使代码更简洁,请将您的样式放入样式表中。

© www.soinside.com 2019 - 2024. All rights reserved.