【发布时间】:2021-08-05 19:17:53
【问题描述】:
我想在内置代码的页面中为 php 中的类别创建一个下拉列表。我可以成功地为它创建一个简码,但由于简码对 html 不友好,我决定不走这条路。因此,没有任何脚本或 jquery,我正在尝试将 php 代码添加到 div 中
<select class="store-search-input form-control" name="dokan_seller_search" value="<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
?>" >
</select>
这发生了:
任何帮助将不胜感激
感谢 Alex 的帮助更新: 我还没有足够的积分直接添加图片:
<input type="select" class="store-search-input form-control" name="dokan_seller_search" value="<?php
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hierarchical' => true,
'hide_empty' => false
);
$all_categories = get_categories( $args );
esc_html('<select class="store-search-input form-control" name="dokan_seller_search">');
foreach($all_categories as $parent){
if ($parent->category_parent == 0) {
echo '<option value="'.$parent->name.'"><a href="'. get_term_link($parent->slug, 'product_cat') .'">'. $parent->name .'</option>';
}
$args2 = array(
'taxonomy' => 'product_cat',
'child_of' => $parent->term_id,
'parent' => $parent->term_id,
'hide_empty' => 'false'
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name;
}
}
}
esc_html('</select>');
?>">
我期待的结果是一个
【问题讨论】:
-
嘿,关于如何改进您的问题以增加在这里获得帮助的机会的一些建议:1. 将屏幕截图直接添加到帖子中(不是每个人都可能想要点击链接); 2. 可能会添加对您期望发生的事情与实际发生的事情的描述; 3. 为了帮助任何人通过查看代码来调试您的代码,如果您可以提供由它生成的 RAW HTML(不是渲染的屏幕截图),那将非常有帮助。
-
您实际上创建了一个没有任何选项的
-
@IvanR 感谢您的建议!会改进的:)
-
@Mayur 好的,谢谢您的留言!
标签: wordpress woocommerce product categories woocommerce-theming