hz2新建Keyword页面

发布于:2025-05-10 ⋅ 阅读:(17) ⋅ 点赞:(0)

新建一个single-keywords.php即可,需要筛选项再建taxonomy-knowledge-category.php
参考:https://www.tkwlkj.com/customize-wordpress-category-pages.html

WordPress中使用了ACF创建了自定义产品分类products,现在想实现自定义产品分类下的文章使用特定的详情页,不使用single.php,如何实现?要实现自定义产品分类下的文章使用特定的详情页而不使用single.php,您可以按照以下步骤操作:

创建自定义产品分类模板文件:
在您的主题文件夹中创建一个新的模板文件,命名为taxonomy-products.php。这里的products是您的自定义产品分类的标识符。
在这个模板文件中,您可以编写您想要显示的产品分类的内容和样式。
在taxonomy-products.php中查询文章:
使用WordPress提供的WP_Query或get_posts()函数在taxonomy-products.php中查询特定分类下的文章。
您可以根据需要设置查询参数,例如指定分类和数量等。
创建自定义文章模板文件:
创建一个单独的文章模板文件,命名为single-products.php。这里的products是您的自定义产品文章类型的标识符。
在这个模板文件中,您可以编写您想要显示的单个产品文章的详细内容和样式。
链接到详情页:
确保在您的分类模板文件taxonomy-products.php中,每篇文章标题都链接到对应的详情页。
您可以使用WordPress提供的the_permalink()函数来获取文章的链接。
保存并上传文件:
保存您的模板文件并上传到您的WordPress主题文件夹中。
通过这些步骤,您就可以实现自定义产品分类下的文章使用特定的详情页。确保您的主题支持这些自定义模板文件,并且在设置时备份您的文件以防万一。

在ACF插件中如何查找自定义产品文章类型的标识符?
要找到自定义产品文章类型的标识符,您可以按照以下步骤在ACF(*自定义字段)插件中查找:

进入WordPress后台:
登录到您的WordPress网站的后台管理页面。
导航到ACF设置:
在WordPress后台菜单中找到“自定义字段”(通常位于左侧菜单中),然后单击进入。
找到您的自定义字段组:
在自定义字段页面中找到与您的自定义产品文章类型相关联的自定义字段组。这可能是您之前创建的用于该文章类型的字段组。
编辑字段组:
单击您的自定义产品文章类型相关联的字段组。
查看字段设置:
在字段组编辑页面中,您将看到与您的自定义产品文章类型相关的字段。
通常,您可以在字段设置中找到“位置”设置。在这里,您可以看到字段组与特定的文章类型相关联。
查看相关文章类型:
在“位置”设置中,您将看到字段组与哪些文章类型相关联。自定义产品文章类型的标识符将在这里列出。
通过这些步骤,您应该能够在ACF插件中找到自定义产品文章类型的标识符。

ACF文章类型的标识符
不同类型的详情页

<!in single-keywords.php-->
<?php include( "header.php"); ?><!--内含主导航-->

<!--上大标题-->

<section class="SubPage-Hero" style="background-image: url(<?php echo get_template_directory_uri(); ?>/assets/images/inner-banner.jpg);">
<div class="container aos-init aos-animate" data-aos="fade-down">
    <h1><?php the_title(); ?></h1>
    <ul>
        <li><a href="<?php echo home_url(); ?>">Home</a></li>
        <span><i class="fa fa-angle-right" aria-hidden="true"></i></span>

        <?php if (is_singular('keywords')) : ?>
            <?php
            // Get the terms associated with the product
            $terms = get_the_terms(get_the_ID(), 'keyword_category');
            if ($terms && !is_wp_error($terms)) :
                $term = array_shift($terms); // Get the first term
            ?>
                <li><a href="<?php echo get_term_link($term->term_id); ?>"><?php echo esc_html($term->name); ?></a></li>
                <span><i class="fa fa-angle-right" aria-hidden="true"></i></span>
            <?php endif; ?>
            
            <li><?php the_title(); ?></li>
        <?php elseif (is_post_type_archive('keyword')) : ?>
            <li>Products</li>
        <?php endif; ?>
    </ul>
</div>

        </section>
        
        
<!--edit by pp-->

<!--描述-->
     <section class="shop-details">
   <div class="container">
    <div class="row">
       
        <!--<div class="main-tit-bar">-->
        <!--                <h1 class="title"><?php the_title(); ?></h1>-->
        <!--                <div class="clear"></div>-->
        <!--            </div>-->
                    <article class="entry blog-article">
                        <?php
                        if (have_posts()):
                            while (have_posts()): the_post();
                                the_content();
                            endwhile;
                        endif;
                        ?>
                        
                    </article>

    </div>
        <!---->
        <div class="lavelname">
           <h2>Related Products</h2>
        </div>
    
      <!--started Related Products-->
    
    <div class="row">
                
        <?php
        
       // $current_category = get_queried_object();
       $current_category_name = get_post_meta($post->ID, '_product_size', true);
       $current_category = get_term_by('name',$current_category_name,'product_category');
       
        if ($current_category && !is_wp_error($current_category)) {
            $popular_tag = 'popular-products';  // Ensure we're querying for 'popular-products' tag

            $popular_args = array(
                'post_type' => 'products', // Make sure you're querying 'products'
                'posts_per_page' => 7,     // Limit to 5 products per query
                'orderby' => 'rand',       // Randomize the results on each page load
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_category', // Filter by product category
                        'field'    => 'term_id',
                        'terms'    => $current_category->term_id,
                    ),
                    array(
                        'taxonomy' => 'product_tag', // Filter by 'popular-products' tag
                        'field'    => 'slug',
                        'terms'    => $popular_tag,
                    ),
                ),
            );

            $popular_query = new WP_Query($popular_args);
          
            if ($popular_query->have_posts() && $popular_query->found_posts>7) :
           
                ?>
                
                    <?php while ($popular_query->have_posts()) : $popular_query->the_post(); ?>
                    <div class="col-lg-4 col-sm-6">
                        <div class="product_items">
                            <div class="overflows">
                                <div class="imagesec">
                                    <a href="<?php the_permalink(); ?>"><img  width="200" height="200" src="<?php the_post_thumbnail_url(); ?>" alt="<?php echo esc_attr(get_the_title() . ' - Product Image - ' . get_bloginfo('name')); ?>" title="<?php echo esc_attr(get_the_title()); ?>"></a>
                                    <div class="overly">
                                        <div class="addto">
                                           <a href="<?php the_permalink(); ?>">View Detail</a>
                                        </div>
                                    </div>
                                </div>
                                <div class="comtevt">
                                    <p><?php the_title(); ?></p>
                                </div>
                            </div>
                        </div>
                     </div>
                    <?php endwhile; ?>
                       
             </div>
            <?php 
            elseif($popular_query->have_posts() && $popular_query->found_posts<=7) : 
                
                    $popular_args_again = array(
                        'post_type' => 'products', // Make sure you're querying 'products'
                        'posts_per_page' => 7,     // Limit to 5 products per query
                        'orderby' => 'rand',       // Randomize the results on each page load
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'product_category', // Filter by product category
                                'field'    => 'term_id',
                                'terms'    => $current_category->term_id,
                            ),
                        ),
                    );
    
                $popular_query_again = new WP_Query($popular_args_again);
                if ($popular_query_again->have_posts() ) :
                        ?>
                        
                            <?php while ($popular_query_again->have_posts()) : $popular_query_again->the_post(); ?>
                                           <div class="col-lg-4 col-sm-6">
                    <div class="product_items">
                        <div class="overflows">
                                <div class="imagesec">
                                    <a href="<?php the_permalink(); ?>"><img  width="200" height="200" src="<?php the_post_thumbnail_url(); ?>" alt="<?php echo esc_attr(get_the_title() . ' - Product Image - ' . get_bloginfo('name')); ?>" title="<?php echo esc_attr(get_the_title()); ?>"></a>
                                    <div class="overly">
                                        <div class="addto">
                                            <a href="<?php the_permalink(); ?>">View Detail</a>
                                        </div>
                                    </div>
                                </div>
                                <div class="comtevt">
                                    <p><?php the_title(); ?></p>
                                </div>
                        </div>
                    </div>
                </div>
                            <?php endwhile; ?>
                       
             </div>
                    <?php 
                endif;
            else:
                echo '<p>No popular products found in this category.</p>';
            endif; 
            wp_reset_postdata();
        } else {
            echo '<p>No product category found.</p>';
        }
        ?>
                <div class="image_sc">
                    <?php if (has_post_thumbnail()) {
                        the_post_thumbnail('full', ['style' => 'width:100%;']);
                    } ?>
                </div>
    </div>
    <!--end Related Products-->
   <!---->
   
   
   
   
   
   </div>
</section>
        
        
        
        <!--开始第二节:热门商品-->
        

   <section class="shop-details">
   <div class="container">

        <!---->
        <div class="lavelname">
           <h2>Top Selling Products</h2>
        </div>
    
      <!--started popular Products-->
    
    <div class="row">
                
        <?php
        
       // $current_category = get_queried_object();
       $current_category_name = get_post_meta($post->ID, '_product_size', true);
       $current_category = get_term_by('name',$current_category_name,'product_category');
       
        if ($current_category && !is_wp_error($current_category)) {
            $popular_tag = 'popular-products';  // Ensure we're querying for 'popular-products' tag

            $popular_args = array(
                'post_type' => 'products', // Make sure you're querying 'products'
                'posts_per_page' => 6,     // Limit to 5 products per query
                'orderby' => 'rand',       // Randomize the results on each page load
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'post_type' => 'products', // Ensure this matches your custom post type
                        'posts_per_page' => 5, // Limit the number of products
                        'orderby' => 'rand',
                        'order' => 'DESC',
                    ),
                ),
            );

            $popular_query = new WP_Query($popular_args);
          
            if ($popular_query->have_posts()) :
           
                ?>
                
                    <?php while ($popular_query->have_posts()) : $popular_query->the_post(); ?>
                    <div class="col-lg-4 col-sm-6">
                        <div class="product_items">
                            <div class="overflows">
                                <div class="imagesec">
                                    <a href="<?php the_permalink(); ?>"><img  width="200" height="200" src="<?php the_post_thumbnail_url(); ?>" alt="<?php echo esc_attr(get_the_title() . ' - Product Image - ' . get_bloginfo('name')); ?>" title="<?php echo esc_attr(get_the_title()); ?>"></a>
                                    <div class="overly">
                                        <div class="addto">
                                            <a href="<?php the_permalink(); ?>">View Detail</a>
                                        </div>
                                    </div>
                                </div>
                                <div class="comtevt">
                                    <p><?php the_title(); ?></p>
                                </div>
                            </div>
                        </div>
                     </div>
                    <?php endwhile; ?>
                       
             </div>
            <?php 
            endif; 
            wp_reset_postdata();
        } else {
            echo '<p>No product category found.</p>';
        }
        ?>
              
    </div>
    <!--end popular Products-->
   <!---->
   
   
   
   
   
   </div>
</section>

  <!--结束第二节:热门商品-->


   <!--开始第三节:热门搜索-->
        

   <section class="shop-details">
   <div class="container">

        <!---->
        <div class="lavelname">
           <h2>related search</h2>
        </div>
    
      <!--started popular Products-->
    
    <div class="row">
                
        <?php
        
       // $current_category = get_queried_object();
       $current_category_name = get_post_meta($post->ID, '_product_size', true);
       $current_category = get_term_by('name',$current_category_name,'product_category');
       
        if ($current_category && !is_wp_error($current_category)) {
            $popular_tag = 'popular-products';  // Ensure we're querying for 'popular-products' tag

            $popular_args = array(
                'post_type' => 'keywords', // Make sure you're querying 'products'
                'posts_per_page' => 10,     // Limit to 5 products per query
                'orderby' => 'rand',       // Randomize the results on each page load
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'post_type' => 'keywords', // Ensure this matches your custom post type
                        'posts_per_page' => 10, // Limit the number of products
                        'orderby' => 'rand',
                        'order' => 'DESC',
                    ),
                ),
            );

            $popular_query = new WP_Query($popular_args);
          
            if ($popular_query->have_posts()) :
           
                ?>
                
                    <?php while ($popular_query->have_posts()) : $popular_query->the_post(); ?>
                    <div class="col-lg-4 col-sm-6">
                        <div class="product_items">
                            <div class="overflows">
                                <div class="imagesec">
                                    <a href="<?php the_permalink(); ?>"><img  width="200" height="200" src="<?php the_post_thumbnail_url(); ?>" alt="<?php echo esc_attr(get_the_title() . ' - Product Image - ' . get_bloginfo('name')); ?>" title="<?php echo esc_attr(get_the_title()); ?>"></a>
                                    <div class="overly">
                                        <div class="addto">
                                            <a href="<?php the_permalink(); ?>">View Detail</a>
                                        </div>
                                    </div>
                                </div>
                                <div class="comtevt">
                                    <p><?php the_title(); ?></p>
                                </div>
                            </div>
                        </div>
                     </div>
                    <?php endwhile; ?>
                       
             </div>
            <?php 
            endif; 
            wp_reset_postdata();
        } else {
            echo '<p>No product category found.</p>';
        }
        ?>
              
    </div>
    <!--end popular Products-->
   <!---->
   
   
   
   
   
   </div>
</section>

  <!--结束第san节:热门搜索-->

<?php include( "footer.php"); ?>


网站公告

今日签到

点亮在社区的每一天
去签到