创建自定义wp_Dropdown_Categories

时间:2011-03-08 作者:Archie Webmaker

我已经为此工作了很多天了。我想将类别分配给作者。我在google上找到了一些提示和插件,但不适用于Wordpress 3.1。我只是想出了我自己的主意。

作为管理员,我将为作者创建一个类别,然后在其各自的概要文件元字段中定义或放置类别slug名称。

我正在使用自定义的帖子类型名称“networks and taxonomy=blogs”

现在,我试图在wp下拉类别中只包含概要文件元字段值(我上面说过)作为默认值,并将其隐藏在我的自定义发布表单中。

当我回显时,cat ID和名称是正确的,但它不包括在下拉列表中。有人能帮我吗?

<?php
global $current_user;

get_currentuserinfo();

$authorcategory = get_the_author_meta(\'authorcategory\', $current_user->ID);
$myterm = get_term_by( \'slug\', $authorcategory, \'blogs\');

if ( is_term( $authorcategory, \'blogs\' && $authorcategory == $myterm ) ) {
    $my_cat_id = get_cat_id($authorcategory);
    $my_cat_name = get_cat_name($my_cat_id);
    $args = array(
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        \'show_last_update\' => 0,
        \'style\' => \'list\',
        \'show_count\' => 0,
        \'hide_empty\' => 0,
        \'include\' => $my_cat_name,
        \'hierarchical\' => true,
        \'title_li\' => __( \'Categories\' ),
        \'show_option_none\' => __(\'No categories\'),
        \'number\' => NULL,
        \'taxonomy\' => \'blogs\'
    );
    wp_dropdown_categories($args); 
} 
?>

2 个回复
SO网友:t31os

你好像搞混了wp_list_categories 与的参数wp_dropdown_categories, 因此,我已经从下面的代码中删除了它们,我还假设include参数是指当前选定的项。

参考:

Suggested code:

global $current_user;

get_currentuserinfo();

$authorcategory = get_user_meta( $current_user->ID, \'authorcategory\', true );
$user_term = get_term_by( \'slug\', $authorcategory, \'blogs\');

// Uncomment the two forward slashes before print for debug
// print \'<pre>\';print_r( $authorcategory );print \'</pre>\';

if( $user_term ) {
    $args = array(
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        \'show_last_update\' => 0,
        \'show_count\' => 0,
        \'hide_empty\' => 0,
        \'selected\' => 0,
        \'child_of\' => $user_term->ID,
        \'hierarchical\' => true,
        \'title_li\' => __( \'Categories\' ),
        \'show_option_none\' => __(\'No categories\'),
        \'taxonomy\' => \'blogs\'
    );
    wp_dropdown_categories($args); 
} 
你不需要这两条线中的任何一条。。

$my_cat_id = get_cat_id($authorcategory);
$my_cat_name = get_cat_name($my_cat_id);
在中,您已经有了该术语的完整对象$myterm 打电话之后get_term_by.

希望这有帮助。。

SO网友:Archie Webmaker

This work for me

<p>
 <?php 
  global $current_user;
  get_currentuserinfo();
  $authorcategory = get_user_meta( $current_user->ID, \'authorcategory\', true );
  $user_term = get_term_by( \'slug\', $authorcategory, \'blogs\');
  $mycat = $user_term->slug;
  $cats_array = get_categories(array(\'child_of=$mycat\',\'taxonomy\' => \'blogs\'));
 ?>
</p>
<p>
 <strong>Is <?php echo $mycat; ?> your Blog Name? Make it sure before posting </strong>
 <select style=&quot;display:none;&quot; id=&quot;cat&quot; name=&quot;cat&quot; class=&quot;input&quot;>
  <?php foreach ( $cats_array as $category ): ?>
  <?php if ($category->cat_name == $mycat ): ?>
  <option value=&quot;<?php echo $category->cat_ID; ?>&quot;><?php echo $category->cat_name; ?></option>
  <?php endif; ?>   
  <?php endforeach; ?> 
 </select>
</p>
结束

相关推荐