嗨, 这是wordpress显示标签的代码:
<label><?php _e('Tags', PLSH_THEME_DOMAIN); ?>:</label>
<?php
foreach($tags as $tag)
{
echo '<a href="' . plsh_assamble_url($shop_page_url, array('product_tag=' . $tag->slug), array('product_tag')) . '"';
if(plsh_get($_GET, 'product_tag') == $tag->slug) echo 'class="active"';
echo '>' . $tag->name . '</a>';
}
?>
我想使它显示为下拉菜单, 但我无法弄清楚:(有人可以帮我吗
#1
<label><?php _e('Tags'); ?></label>
<form action="<?php bloginfo('url'); ?>/" method="get">
<div>
<?php
$args = array(
'taxonomy' => 'product_tag', // Taxonomy to return. Valid values are 'category', 'post_tag' or any registered taxonomy.
'show_option_none' => 'Select tag', 'show_count' => 1, 'orderby' => 'name', 'value_field' => 'slug', 'echo' => 0
);
$select = wp_dropdown_categories( $args );
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div>
</form>
你可以尝试上面的代码吗?
#2
将此代码粘贴到functions.php结束回声到你想要的任何位置。你还可以使用以下代码制作简码:add_shortcode(‘要显示的名称’, ‘displayLocationDropdown’)’。另请参阅评论。
<?php
function displayLocationDropdown() {
$html = '';
$html .= '<form class="location-select" method="post">';
$html .= '<select id="location-selector" name="location" class="location">';
$tag = wp_tag_cloud( array(
'format' => 'array', 'taxonomy' => 'product_tag'
) );
$page_path = $_SERVER["REQUEST_URI"];
$page_url = site_url() . $page_path;
$url_parts = parse_url($page_url);
$constructed_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['path'])?$url_parts['path']:'');
$html .= '<option value="#">--Select Location--</option>';
foreach($tag as $tagkey => $tagvalue)
{
$cleanedup = strip_tags($tagvalue);
$tag_info = get_term_by('name', $cleanedup, 'product_tag', 'ARRAY_A');
$tag_slug = $tag_info['slug'];
if(isset($_GET['product_tag'])) { /// I am not sure if it is necessery
$value_url = $constructed_url . '?product_tag=' . $tag_slug;
} else {
$value_url = $page_url . '?product_tag=' . $tag_slug;
}
/// (this prevent duplicate in URL in my case) $value_url = site_url() . '?product_tag=' . $tag_slug;
$html .= '<option value="' . $value_url . '">' . $cleanedup . '</option>';
}
$html .= '<option value="' . $constructed_url . '">View All Locations</option>';
$html .= '</select>';
$html .= '</form>';
echo $html;
}
add_action('woocommerce_before_shop_loop', 'displayLocationDropdown', 40);
add_action('wp_head', 'addDisplayLocationScript');
function addDisplayLocationScript() {
$script = '';
$script .= '<script type="text/javascript">';
$script .= 'jQuery(document).ready(function() {';
$script .= ' jQuery("#location-selector").change(function() {';
$script .= ' location = jQuery("#location-selector option:selected").val();';
$script .= ' });';
$script .= '});';
$script .= '</script>';
echo $script;
}
评论前必须登录!
注册