我想出来了。我重新排列了HTML并使用了变量,这样代码就不会中断。我还删除了
esc_attr_e( \'Select:\', \'textdomain\' );
因为它导致结果出现在DIV之外,所以插入了shortcode。
function searchbar_function() {//Creates shortcode [searchbar]
$MainCategories = get_categories( array(
\'orderby\' => \'name\',
\'parent\' => 0
) );//Grabs just the parent categories
$CodeResult.=\'<form action="action_page.php">\';
foreach ( $MainCategories as $MainCategory ) {
$MainCatID=esc_html($MainCategory->term_id);//Gets Main Category ID
if($MainCatID!==\'1\' && $MainCatID!==\'153\'){//Exclude Uncategorized or Featured from Main Category List
$MainCatURL=esc_url( get_category_link( $MainCategory->term_id ) );//Gets Main Category URL
$MainCatName=esc_html( $MainCategory->name );//Gets Main Category Name
$MainCatSlug=esc_html( $MainCategory->slug );//Gets Main Category Slug
$MainCatCount=esc_html( $MainCategory->category_count );
$MainCat=\'<label>\'.$MainCatName.\'</label>\';
$MainCatCode=\'<option value="\'.$MainCatSlug.\'">ALL \'.$MainCatName.\'</option>\';
$CodeResult.=$MainCat;
$SelectCode=\'<select name="\'.$MainCatSlug.\'-dropdown"><option value="">Select:</option>\';
$CodeResult.=$SelectCode;
$CodeResult.=$MainCatCode;//Makes Main Category first choice
$categories = get_categories( array( \'child_of\' => $MainCatID ) );//Number is the Main Category ID
foreach ( $categories as $category ) {
$SubCatValueName= esc_attr($category->category_nicename);
$SubCatValue= esc_attr( \'/category/archives/\' . $category->category_nicename );
$SubCatName=esc_html( $category->cat_name );
$SubCatCount=esc_html( $category->category_count );
$SubCatOption =\'<option value="\'.$SubCatValueName.\'">\'.$SubCatName.\'</option>\';
$CodeResult.=$SubCatOption;
}
$CodeResult.=\'</select>\';
}
}
$SubmitEnd=\'<input type="submit" value="Search">
</form>\';
$CodeResult.=$SubmitEnd;
return $CodeResult;
}
add_shortcode( \'searchbar\', \'searchbar_function\' );