【问题标题】:WordPress searching by custom taxonomyWordPress 按自定义分类搜索
【发布时间】:2023-03-22 11:53:02
【问题描述】:

我正在使用以下代码,它允许我在我的自定义分类中创建一个术语下拉列表以用于搜索。

此代码适用于创建下拉列表并使用它进行搜索。问题是,我无法在结果页面的选择框中保留所选术语“已选择”。

我在此处 (https://wordpress.org/support/topic/plugin-events-manager-searching-by-custom-taxonomy#post-3792604) 复制了此问题的答案中的代码,它应该将所选类添加到搜索词中——但它没有做任何事情。

你能通过我的代码告诉我我做错了什么吗?问题似乎与 $search_values['grades'] == $term->slug 行有关。我的自定义分类是“等级”。

function get_terms_dropdown($taxonomies, $args)
{
    global $search_values;

    $myterms = get_terms($taxonomies, $args);
    $output = "";
    foreach ($myterms as $term) {
        $root_url = get_bloginfo('url');
        $term_taxonomy = $term->taxonomy;
        $term_slug = $term->slug;
        $term_name = $term->name;
        $value = $term->term_id;
        if ($search_values['grades'] == $term->slug) {
            $selected = 'selected="selected" ';
        } else {
            $selected = '';
        }
        $output .= "<option value='" . $value . "' " . $selected . ">" . $term_name . "</option>";
    }

    return $output;
}

【问题讨论】:

    标签: php wordpress custom-taxonomy


    【解决方案1】:

    尝试使用刚刚选择的生成如下选项的选项,而不是 selected = 'selected'。应该解决它:http://www.w3schools.com/tags/att_option_selected.asp

    function get_terms_dropdown($taxonomies, $args)
    {
        global $search_values;
    
        $myterms = get_terms($taxonomies, $args);
        $output = "";
        foreach ($myterms as $term) {
            $root_url = get_bloginfo('url');
            $term_taxonomy = $term->taxonomy;
            $term_slug = $term->slug;
            $term_name = $term->name;
            $value = $term->term_id;
            if ($search_values['grades'] == $term->slug) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $output .= "<option value='" . $value . "' " . $selected . ">" . $term_name . "</option>";
        }
    
        return $output;
    }
    

    【讨论】:

    • 谢谢。不幸的是,这并没有做到。如果我只是将 $search_values['grades'] 更改为术语(如 $search_values['first-grade']),它会正确地将“selected”添加到该术语。所以我认为问题是我需要弄清楚如何使 $search_values[....] 等于当前选择的术语。
    猜你喜欢
    • 2013-09-02
    • 1970-01-01
    • 2011-03-01
    • 2018-01-08
    • 1970-01-01
    • 2014-04-30
    • 2016-12-18
    • 2017-04-05
    • 2015-10-03
    相关资源
    最近更新 更多