hz修改后台新增keyword功能

functions.php

php 复制代码
function create_product_post_type2() {
    register_post_type('keywords',
        array(
            'labels' => array(
                'name' => __('Keywords'),
                'singular_name' => __('Keyword')
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), // No need to add 'tags' here
            'rewrite' => array('slug' => 'keywords'),
        )
    );

    // Register product categories taxonomy
    register_taxonomy('keyword_category', 'keywords', array(
        'label' => __('Keyword Categories'),
        'rewrite' => array('slug' => 'keyword-category'),
        'hierarchical' => true,
    ));

    // Register product tags taxonomy
    register_taxonomy('keyword_tag', 'keywords', array( // Make sure this is registered correctly
        'label' => __('Tags'),
        'rewrite' => array('slug' => 'keyword-tag'),
        'hierarchical' => true,
    ));
}
add_action('init', 'create_product_post_type2');

// Add Meta Box for Product Details and Highlights
function keyword_details_meta_box() {
    add_meta_box(
        'product_details_box',       // ID of the meta box
        'Products <--> Keyword', // Title of the meta box
        'keyword_details_meta_box_callback', // Callback function to display the meta box content
        'keywords',                  // Post type where the meta box will appear (custom post type 'products')
        'normal',                    // Context: 'normal', 'side', 'advanced'
        'high'                       // Priority: 'high', 'core', 'default', 'low'
    );
}
add_action('add_meta_boxes', 'keyword_details_meta_box');

// Meta box content callback function //keyword的自定义页面cr by pp.
function keyword_details_meta_box_callback($post) {
 
    // Retrieve existing values from the database
    $product_size = get_post_meta($post->ID, '_product_size', true);
    $product_carton = get_post_meta($post->ID, '_product_carton', true);
    $product_weight = get_post_meta($post->ID, '_product_weight', true);
    $product_description = get_post_meta($post->ID, '_product_description', true);
    $product_highlights = get_post_meta($post->ID, '_product_highlights', true); // New highlights field

    // Display form fields
    ?>
    <label for="product_size"><b>Secondary product catalog corresponding to this keywords:</b></label><br>
    <input type="text" name="product_size" id="product_size" value="<?php echo esc_attr($product_size); ?>" size="50" /><br><br>


 
    <div class="basecategory d-none d-lg-block d-md-block">
    <div class="widget-title">
        <h3>Related Popular Products</h3>
    </div>
    <div class="products-item">
        <?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) :
           
                ?>
                <div class="product-gallery-images">
                    <?php while ($popular_query->have_posts()) : $popular_query->the_post(); ?>
                        <div class="gallery-image">
                            <a href="<?php the_permalink(); ?>" class="d-flex align-items-center">
                               <div class="proimg_lest">
                                    <!--<img src="<?php the_post_thumbnail_url(); ?>" alt="">-->
                                    <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()); ?>">
                                </div>
                                <div class="content_parts">
                                    <p><?php the_title(); ?></p>
                                </div>
                            </a>
                        </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() ) :
                        ?>
                        <div class="product-gallery-images">
                            <?php while ($popular_query_again->have_posts()) : $popular_query_again->the_post(); ?>
                                <div class="gallery-image">
                                    <a href="<?php the_permalink(); ?>" class="d-flex align-items-center">
                                       <div class="proimg_lest">
                                            <!--<img src="<?php the_post_thumbnail_url(); ?>" alt="">-->
                                            <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()); ?>">
                                        </div>
                                        <div class="content_parts">
                                            <p><?php the_title(); ?></p>
                                        </div>
                                    </a>
                                </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>
</div>
    
    
    
    
    
    
    
    <?php
    
    
    
}
相关推荐
w23617346012 分钟前
Tomcat:从零理解Java Web应用的“心脏”
java·前端·tomcat
姝然_95275 分钟前
cursor vue3 rules
前端
littleplayer5 分钟前
iOS 中的 @MainActor 详解
前端·swiftui·swift
yuren_xia7 分钟前
示例:Spring JDBC编程式事务
java·后端·spring
嘻嘻嘻嘻嘻嘻ys10 分钟前
《智能编码新纪元:GitHub Copilot+Cursor实战开发效能跃迁指南》
前端
zhangxiao12 分钟前
预览组件 支持图片跟PDF
前端
陈大大陈22 分钟前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存
纪元A梦24 分钟前
华为OD机试真题——通过软盘拷贝文件(2025A卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
络734 分钟前
IDEA导入并启动若依项目步骤(SpringBoot+Vue3)
java·spring boot·mysql·vue·intellij-idea
几颗流星35 分钟前
SpringBoot项目集成达梦数据库
java·后端