hz修改后台新增keyword功能

时间:2025-04-26 07:56:24
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 }