如果自定义微件为空,如何隐藏微件标题

时间:2015-07-28 作者:Elena

我创建了带有类别过滤器选择的自定义小部件。小部件显示当前分类术语的相关帖子,并按所选类别进行筛选。

如果小部件为空(如果没有相关帖子),我想隐藏小部件标题。

我该怎么办?

这是我的代码:

public function widget( $args, $instance ) {
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
        $limit = $instance[\'limit\'];
        $cat = $instance[\'cat\'];
        $show_thumbnail = isset( $instance[\'show_thumbnail\'] ) ? $instance[\'show_thumbnail\'] : false;

            echo $args[ \'before_widget\' ];      


                if ( !empty( $title ) ) 
                echo $args[\'before_title\'] . $title . $args[\'after_title\']; 

                $cat_select = array( 
                \'numberposts\' => $limit,
                \'cat\' => $cat,  
            ); ?>

               /******* function to get the posts of current taxonomy term *******/
                <?php if ( function_exists( \'get_related_posts_widget\' ) ) { ?>

                    <?php $related_posts = get_related_posts_widget( \'authors\', $cat_select );
                        if ( $related_posts ) { ?>


                            <ul class="related-cat">
                            <!-- Widget loop -->
提前谢谢你

编辑

我使用你建议的代码here

我调用函数get\\u related\\u posts\\u widget from functions。php。我用所选的cat($cat)修改了查询。

public function widget( $args, $instance ) {
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
        $limit = $instance[\'limit\'];
        $cat = $instance[\'cat\'];
        $show_thumbnail = isset( $instance[\'show_thumbnail\'] ) ? $instance[\'show_thumbnail\'] : false;

            echo $args[ \'before_widget\' ];       

                $cat_select = array( 
                \'numberposts\' => $limit,
                \'cat\' => $cat,  
            );

                if ( function_exists( \'get_related_posts_widget\' ) ) { ?>
                    <?php $related_posts = get_related_posts( \'authors\', $cat_select ); 
                        if ( $related_posts ) {
                            if ( !empty( $title ) ) 
                            echo $args[\'before_title\'] . $title . $args[\'after_title\']; ?>

                                <ul class="related-cat">
                                    <?php foreach ( $related_posts as $post ) {
                                        setup_postdata( $post ); ?>
                                        <li>
                                            <?php if ( $show_thumbnail ) : ?>
                                            <figure class="post-thumbnail">
                                                <?php the_post_thumbnail(\'tie-large\'); ?>                                               
                                            </figure>
                                            <?php endif; ?>
                                            <h3><a href="<?php echo get_the_permalink($post->ID); ?>"> <?php echo get_the_title($post->ID);?> </a></h3>
                                        </li>
                                    <?php } //foreach ( $related_posts as $post ) ?>
                                </ul>


                        <?php } // end if ?>
                    <?php echo $args[ \'after_widget\' ]; ?>          

           <?php } // end if function   ?>

    <?php } // end Widget Function
就像$after\\u小部件不起作用一样。

求解

感谢@PieterGoosen!阅读下面的代码。精彩的

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

我总是在我的查询中移动我的过滤器,当我的查询没有返回结果时,这将阻止它们显示任何内容。您只需移动以下内容

echo $args[ \'before_widget\' ];      

if ( !empty( $title ) ) 
   echo $args[\'before_title\'] . $title . $args[\'after_title\']; 
就在这条线之后

if ( $related_posts ) { ?>
编辑我很快重写了一个小部件,以适应我的相关帖子功能。我已经评论了一些存在问题的地方。希望这有帮助

<?php
/**
 * Related_Posts_Widget widget class
 *
 * Displays posts from a selected category
 *
 * @since 1.0.0
*/
class Related_Posts_Widget extends WP_Widget 
{

    public function __construct() 
    {
        parent::__construct(
            \'widget_related_posts\', 
            _x( \'Related Posts Widget\', \'Related Posts Widget\' ), 
            [ \'description\' => __( \'Display a list of related posts.\' ) ] 
        );
        $this->alt_option_name = \'widget_related_posts\';

        add_action( \'save_post\', [$this, \'flush_widget_cache\'] );
        add_action( \'deleted_post\', [$this, \'flush_widget_cache\'] );
        add_action( \'switch_theme\', [$this, \'flush_widget_cache\'] );
    }

    public function widget( $args, $instance ) 
    {
        $cache = [];
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get( \'widget_rel_posts\', \'widget\' );
        }

        if ( ! is_array( $cache ) ) {
            $cache = [];
        }

        if ( ! isset( $args[\'widget_id\'] ) ) {
            $args[\'widget_id\'] = $this->id;
        }

        if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
            echo $cache[ $args[\'widget_id\'] ];
            return;
        }

        ob_start();

        $title          = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'Related Posts\' );
        /** This filter is documented in wp-includes/default-widgets.php */
        $title          = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
        $number         = ( ! empty( $instance[\'number\'] ) ) ? absint( $instance[\'number\'] ) : 5;
        if ( ! $number ) {
            $number = 5;
        }
        $cat_id         = $instance[\'cat_id\'];
        $thumbnail      = $instance[\'thumbnail\'] ? true : false; 

        /**
         * Filter the arguments for the Related Posts Widget.
         *
         * @since 1.0.0
         *
         */
        $cat_select = [ 
            \'numberposts\' => $number,
            \'cat\'         => $cat,  
        ];

        if ( function_exists( \'get_related_posts\' ) ) { // This line was wrong, get_related_posts_widget should be get_related_posts
            $related_posts = get_related_posts( \'authors\', $cat_select ); 
                if ( $related_posts ) {

                    echo $args[\'before_widget\'];
                    if ( $title ) { // This should not be !$title
                        echo $args[\'before_title\'] . $title . $args[\'after_title\'];
                    }               
                    ?>

                    <ul class="related-cat">
                        <?php foreach ( $related_posts as $post ) {
                            setup_postdata( $post ); ?>
                            <li>
                                <?php if ( $thumbnail ) : ?>
                                <figure class="post-thumbnail">
                                    <?php the_post_thumbnail( \'tie-large\' ); ?>                                               
                                </figure>
                                <?php endif; ?>
                                <h3><a href="<?php echo get_the_permalink( $post->ID ); ?>"> <?php echo get_the_title( $post->ID );?> </a></h3>
                            </li>
                        <?php } //foreach ( $related_posts as $post ) ?>
                    </ul>


                    <?php 
                } // end if     

            wp_reset_postdata(); // You forgot this, this is very very very important

            echo $args[\'after_widget\']; 
        }

        if ( ! $this->is_preview() ) {
            $cache[ $args[\'widget_id\'] ] = ob_get_flush();
            wp_cache_set( \'widget_rel_posts\', $cache, \'widget\' );
        } else {
            ob_end_flush();
        }
    }

    public function update( $new_instance, $old_instance ) 
    {
        $instance                   = $old_instance;
        $instance[\'title\']          = strip_tags( $new_instance[\'title\'] );
        $instance[\'number\']         = (int) $new_instance[\'number\'];
        $instance[\'cat_id\']         = (int) $new_instance[\'cat_id\'];
        $instance[\'thumbnail\']      = $new_instance[\'thumbnail\'];
        $this->flush_widget_cache();

        $alloptions = wp_cache_get( \'alloptions\', \'options\' );
        if ( isset($alloptions[\'widget_related_posts\']) )
            delete_option(\'widget_related_posts\');

        return $instance;
    }

    public function flush_widget_cache() 
    {
        wp_cache_delete(\'widget_rel_posts\', \'widget\');
    }

    public function form( $instance ) 
    {

        $title      = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
        $number     = isset( $instance[\'number\'] ) ? absint( $instance[\'number\'] ) : 5;
        $cat_id     = isset( $instance[\'cat_id\'] ) ? absint( $instance[\'cat_id\'] ) : 1;
        $thumbnail  = isset( $instance[\'thumbnail\'] ) ? $instance[\'thumbnail\'] : false; 
        ?>

        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo $title; ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( \'number\' ); ?>"><?php _e( \'Number of posts to show:\' ); ?></label>
            <input id="<?php echo $this->get_field_id( \'number\' ); ?>" name="<?php echo $this->get_field_name( \'number\' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id(\'cat_id\'); ?>"><?php _e( \'Category Name:\' )?></label>
            <select id="<?php echo $this->get_field_id(\'cat_id\'); ?>" name="<?php echo $this->get_field_name(\'cat_id\'); ?>">
                <?php 
                $this->categories = get_categories();
                foreach ( $this->categories as $cat ) {
                    $selected = ( $cat->term_id == esc_attr( $cat_id ) ) ? \' selected = "selected" \' : \'\';
                    $option = \'<option \'.$selected .\'value="\' . $cat->term_id;
                    $option = $option .\'">\';
                    $option = $option .$cat->name;
                    $option = $option .\'</option>\';
                    echo $option;
                }
                ?>
            </select>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id(\'thumbnail\'); ?>"><?php _e( \'Hide post thumbnail\' ); ?></label>
            <?php $checked = ( $thumbnail ) ? \' checked=\\"checked\\" \' : \'\'; ?>
            <input type="checkbox" id="<?php echo $this->get_field_id( \'thumbnail\' ); ?>" name="<?php echo $this->get_field_name( \'thumbnail\' ); ?>" value="true" <?php echo $checked; ?> />    
        </p>

    <?php
    }

}

add_action( \'widgets_init\', function () 
{
    register_widget( \'Related_Posts_Widget\' );
});

结束

相关推荐

customize footer widgets area

我有一个自定义的页脚小部件区域,它在一行中水平显示最多4个小部件。如果我添加了4个以上的小部件,那么布局就会中断,因为我试图在同一行中显示它。我想让它更灵活,例如有2行(div),我可以在第一行中添加let’s 2 widget,在第二行中添加4个widget。可能我需要的是复制这一个,并制作两个页脚区域。这可能吗?如果可能,我如何实现?下面是我的小部件的实际代码。php: /* Footer Widgets */ $footer_widgets_num = wp_get_sidebars_