代码之家  ›  专栏  ›  技术社区  ›  Sandeep Rathore

如何在仪表板中调用类别,如果我的帖子仅附加到它们?

  •  0
  • Sandeep Rathore  · 技术社区  · 8 年前

    我正在制作用户仪表板,并为类别创建仪表板小部件。现在我想显示类别,如果当前用户的帖子附加到它们。

    目前我可以看到所有类别。 这是我的函数

    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' );
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Sofiane Achouba    8 年前

    您需要查询每个类别中的所有用户帖子,如果不为空,则包括该类别。试试这个修改过的代码

    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' );