我正在开设一个在线商店项目。我创建了一个名为“性别”的wordpress自定义分类法。并在男性和女性中添加了两个术语,并在产品中添加了分类。当我添加每个产品时,我选择分类,即男性或女性的分类。 。还创建了一个自定义菜单,在菜单栏上显示分类。当我点击男性时,男性类别下的产品应该展示,当选择女性时,相应的产品应该显示。我是php和wordpress的新手,我不知道如何显示与分类条款相关的相应产品。请帮助....
enter code here
<?php
/*
* Plugin Name: Gender Category Tab
* Description: Creates a gender category.
* Version: 1.0.1
*/
if(!defined('ABSPATH')) exit; // Exit if accessed directly
//hook into the init action and call create_gender_hierarchical_taxonomy when it fires
add_action( 'init', 'create_gendercategory_taxonomy', 0 );
function create_gendercategory_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'gender', 'taxonomy general name' ),
'singular_name' => _x( 'gender', 'taxonomy singular name' ),
'search_items' => __( 'Search gender' ),
'popular_items' => __( 'Popular gender' ),
'all_items' => __( 'All gender' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit gender' ),
'update_item' => __( 'Update gender' ),
'add_new_item' => __( 'Add New gender' ),
'new_item_name' => __( 'New gender Name' ),
'add_or_remove_items' => __( 'Add or remove gender' ),
'choose_from_most_used' => __( 'Choose from the most used gender' ),
'menu_name' => __( 'gender' ),
);
register_taxonomy('gender','product',array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
} add_filter(&#39; wp_nav_menu_items&#39;,&#39; gender_clothing_menu_item&#39;,10,2);
function gender_clothing_menu_item( $items, $args ) {
$taxo_terms = get_terms( 'gender', array( 'hide_empty' => 0) );
$allProducts=site_url()."/shop";
$items.="<li class='menu-item menu-item-type-custom menu-item-object-custom menu-item-has- children dropdown'><a href='".$allProducts."'>Clothings</a>";
$items.="<ul class='sub-menu'>";
foreach($taxo_terms as $term)
{
$items.='<li class="menu-item menu-item-type-custom menu-item-object-custom"><a href="">' .$term->name. '</a></li>';
}
$items.="</ul>";
$items.="</li>";
return $items;
}
?>
答案 0 :(得分:0)
更改
foreach($taxo_terms as $term)
{ $term_link = get_term_link( $term );
$items.='<li class="menu-item menu-item-type-custom menu-item-object-custom"><a href="' . esc_url( $term_link ) . '">' .$term->name. '</a></li>';
}
并使用名称
在主题中创建文件taxonomy-{taxonomy}.php
并执行显示产品的代码