2016-12-26 39 views
0

我正在制作用户仪表板,并为类别创建仪表板小部件。现在我想显示类别,如果当前用户的帖子附加到他们。如果发布的矿井仅在其附属的仪表板上如何调用仪表板中的类别?

当前我能够看到所有类别。 这里是我的功能

function user_categories() { 

wp_add_dashboard_widget(
      'wp_widget_category',   // Widget slug. 
      'Categories',   // Title. 
      'my_dashboard_category' // Display function. 
    ); 
function my_dashboard_category() { 
    if (is_user_logged_in()): 
     $current_user = wp_get_current_user(); 

    if (($current_user instanceof WP_User)) { 
     ?> 

     <?php 
     $args = array(
      'type'      => 'recipes', 
      'child_of'     => 0, 
      'parent'     => '', 
      'orderby'     => 'name', 
      'order'     => 'ASC', 
      'hide_empty'    => 1, 
      'author'     => $current_user->ID, 
      'hierarchical'    => 1, 
      'exclude'     => '', 
      'include'     => '', 
      'number'     => '', 
      'taxonomy'     => 'recipe_categories', 
      'pad_counts'    => false); 

     $categories = get_categories($args); 
     foreach ($categories as $category) { 
      $url = get_term_link($category);?> 
     <div class="submitted_recipe"> 
      <a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?> 
       <h2><?php echo $category->name; ?></h2> 
     </a> 
      </div> 
     <?php 
     } 
     ?> 

    <?php 
    } 
    endif; 
    } 
} 
add_action('wp_dashboard_setup', 'user_categories'); 

回答

0

您需要查询所有用户发布在每个categorie,如果它不是空的,则包括该类别。尝试这个修改后的代码

function user_categories() { 

wp_add_dashboard_widget(
      'wp_widget_category',   // Widget slug. 
      'Categories',   // Title. 
      'my_dashboard_category' // Display function. 
    ); 


function my_dashboard_category() { 
    if (is_user_logged_in()): 
     $current_user = wp_get_current_user(); 

    if (($current_user instanceof WP_User)) { 
     ?> 

     <?php 
     $args = array(
      'child_of'     => 0, 
      'parent'     => '', 
      'orderby'     => 'name', 
      'order'     => 'ASC', 
      'hide_empty'    => 1, 
      'hierarchical'    => 1, 
      'exclude'     => '', 
      'include'     => '', 
      'number'     => '', 
      'taxonomy'     => 'recipe_categories', 
      'pad_counts'    => false); 

     $categories = get_categories($args); 
     foreach ($categories as $category) { 
      $posts_query = new WP_Query(array(
       'post_type' => 'recipes', 
       'author' => $current_user->ID, 
       'tax_query' => array(
        array('taxonomy' => 'recipe_categories', 
        'field' => 'slug', 
        'terms' => $category->slug) 
       ), 
       'fields' => 'ids' 
      )); 
      $ids = $posts_query->posts; 
      if (!empty ($ids)) { 
      $url = get_term_link($category);?> 
     <div class="submitted_recipe"> 
      <a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?> 
       <h2><?php echo $category->name; ?></h2> 
     </a> 
      </div> 
     <?php 
      } 
     } 
     ?> 

    <?php 
    } 
    endif; 
    } 
} 
add_action('wp_dashboard_setup', 'user_categories'); 
+0

我试过你的代码,并为同一类添加2个帖子,但它仍然显示所有三个类别。我链接我的帖子只有一个类别 –

+0

对不起,我犯了一个错误,tax_query是一个数组数组,我编辑了代码 –

+0

谢谢了。你救了我的命 :) –