显示自定义分类下拉列表帖子

时间:2021-01-18 作者:Moiz Aslam

我想在下拉列表中显示我的自定义分类法的帖子,我的意思是我在自定义帖子类型中为分类法创建一个过滤器。我有两种不同的类型,其中包含一些子类别&;在这个类别中,我添加了一些帖子,第一个分类法是;“类型”;第二个是;类别“;,因此,当我单击轴名下拉列表并选择用于显示帖子的子类别时,不会显示任何内容。。。

函数diwp\\U arts\\U custom\\U post\\U type(){

$labels = array(
                 \'name\' => \'Arts\',
                 \'singular_name\' => \'Art\',
                 \'add_new\'    => \'Add New Post\',
                 \'add_new_item\' => \'Enter Art Details\',
                 \'all_items\' => \'All Arts\',
                 \'featured_image\' => \'Add Feature Image\',
                 \'set_featured_image\' => \'Set Feature Image\',
                 \'remove_featured_image\' => \'Remove Feature Image\'

               );


$args = array(    
                \'public\'      => true,
                \'label\'       => \'Arts\',
                \'labels\'      => $labels,
                \'description\' => \'Arts is a collection of all Art and their info\',
                \'menu_icon\'      => \'dashicons-art\',    
                \'supports\'   => array( \'title\', \'editor\', \'thumbnail\'),
                \'capability_type\' => \'page\',
                 
             );


register_post_type(\'arts\', $args);
}

add\\u action(\'init\',\'diwp\\u arts\\u custom\\u post\\u type\');

函数wpdocs\\u create\\u book\\u tax(){注册\\u分类法(\'Types\',\'arts\',数组(\'label\',\'Types\',\'textdomain\'),\'rewrite\',\'array(\'slug\',\'arts\'),\'hierarchy\',\'true,);

register_taxonomy( \'Categories\', \'arts\', array(
    \'label\'        => __( \'Categories\', \'textdomain\' ),
    \'rewrite\'      => array( \'slug\' => \'arts\' ),
    \'hierarchical\' => true,
) );
}

添加\\u操作(\'init\',\'wpdocs\\u create\\u book\\u tax\',0);

函数get\\u terms\\u dropdown\\u grade\\u level($taxonomies,$args){$myterms=get\\u terms($taxonomies,$args);$output=”;//我可以更改此名称(对于我的remembreance)!$output。=”选择类型“;foreach($terms为$term){$root\\u url=get\\u bloginfo(\'url”)$term\\u分类法=$term->;分类学$term\\u slug=$term->;段塞$term\\u name=$term->;名称$链接=$term\\u slug$输出="E"E$term\\u名称"E";;}$输出="E&引用;;返回$输出;}

function get_terms_dropdown_type($taxonomies, $args){
        $myterms = get_terms($taxonomies, $args);
        $output ="<select name=\'Categories\'>";
        $output .="<option value=\'\'>Select Categories</option>";              
    foreach($myterms as $term){
                $root_url = get_bloginfo(\'url\');
                $term_taxonomy=$term->taxonomy;
                $term_slug=$term->slug;
                $term_name =$term->name;
                $link = $term_slug;
                $output .="<option value=\'".$link."\'>".$term_name."</option>";
        }
        $output .="</select>";
return $output;
}
我将此代码写入侧栏。php文件,因为我想在所有页面上显示下拉列表:

“method=”get“>”name“,”hide\\u empty“=>false);$select=get\\u terms\\u dropdown\\u grade\\u level($分类法,$args);$select=preg\\u replace(“\\]*)>”,“”,$select);echo$select;?>”name“,”hide\\u empty“=>false);$select=get\\u terms\\u dropdown\\u type($分类法,$args);

    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange=\'return this.form.submit()\'>", $select);
    echo $select;
    ?>
    <input type="submit" name="submit" value="Go" />
</div>

1 个回复
SO网友:LWS-Mo

我看不到您希望在代码中的什么地方有一个分类下拉列表,也看不到您希望如何处理这个下拉列表。这还取决于要用作选项值的值。

但通常你可以使用wp_dropdown_categories 创建自定义分类下拉列表。

wp_dropdown_categories( array(
    \'show_option_all\' =>  \'Show all arts\',
    \'taxonomy\'        =>  \'arts\', // Name of the taxonomy or taxonomies to retrieve
    \'name\'            =>  \'arts\', // Value for the \'name\' attribute of the select element
    \'orderby\'         =>  \'name\', // Order drop-down options
    \'value_field\'     =>  \'slug\', // Term field that should be used to populate the \'value\' attribute of the option elements
                                  // Accepts: \'term_id\', \'name\', \'slug\', \'term_group\', \'term_taxonomy_id\', \'taxonomy\', \'description\', \'parent\', \'count\'
    \'selected\'        =>  $selected, // Value of the option that should be selected. You need to add your own custom $selected
    \'hierarchical\'    =>  true // Whether to traverse the taxonomy hierarchy
) );

相关推荐

Debug in WP backend

问题是,我每次登录WP中的/admin页面时都会收到这些警告。根据我使用的浏览器,这些浏览器存在以下问题:警告:call\\u user\\u func\\u array()要求参数1为有效回调,在/var/www/public\\u html/wp includes/class wp hook中找不到函数“add\\u role\\u caps\\u to\\u job\\u agent”,或者函数名无效。php在线298警告:无法修改标题信息-标题已由/var/www//public\\u html/