我很想知道你对我的最终解决方案的意见。如果能做得更好的话,请随时拉我上来。解决方案如下:
<select id="select-category">
<option class="first-option" value="-1">Select Product Category</option>
<?php
$taxonomy = \'category\';
$terms = get_terms($taxonomy, array(\'orderby\' => \'name\'));
foreach($terms as $term) {
echo \'<option value="\'.$term->term_id.\'">\'.$term->name.\'</option>\';
} ?>
</select>
<select id="select-product" disabled="disabled"></select>
<input id="myButton" type="submit" value="Take me to this product" />
在我的函数中紧随其后。php文件:
function implement_ajax() {
$term_id = $_POST[\'id\'];
echo \'<option class="first-option" value="-1">Select Product</option>\';
$args = array(
\'post_type\' => \'products\',
\'taxonomy\' => \'category\',
\'terms\' => $term_id
);
global $wp_query;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post()
echo \'<option value="\' . get_permalink() . \'">\' . get_the_title() . \'</option>\';
endwhile;
};
add_action(\'wp_ajax_my_special_ajax_call\', \'implement_ajax\');
add_action(\'wp_ajax_nopriv_my_special_ajax_call\', \'implement_ajax\');
然后,我默示了一些jQuery,以将所有内容组合在一起。我很想知道这是否是最好、最有效的方法。