Как добавить два виджета из двух пользовательских типов записей в Wordpress - PullRequest
0 голосов
/ 14 апреля 2020

Привет, мне нужно зарегистрировать два виджета типа два поста. первый тип - это фильмы, а второй - новости. Я добавил виджет в функцию, но показывает только один из них.

У меня есть два класса, и когда я тестирую только одну из тем, работает ли она хорошо? но когда я добавляю его вместе, я вижу только один из них

, это мой класс кода Movies_Recent_Posts extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
        parent::__construct('wpsites-recent-posts', esc_html__('Recent Movies','artor'), $widget_ops);
        $this->alt_option_name = 'Movies_Recent_Posts';

        add_action( 'save_post', array($this, 'flush_widget_cache') );
        add_action( 'deleted_post', array($this, 'flush_widget_cache') );
        add_action( 'switch_theme', array($this, 'flush_widget_cache') );
    }

    public function widget($args, $instance) {
        $cache = array();
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
        }

        if ( ! is_array( $cache ) ) {
            $cache = array();
        }

        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'] : esc_html__( 'Recent Movies','artor' );

        /** 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;
        $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;


        $r = new WP_Query( apply_filters( 'widget_posts_args', array(
            'posts_per_page'      => $number,
            'no_found_rows'       => true,
            'post_status'         => 'publish',
            'post_type'           => array('movies',
            'ignore_sticky_posts' => true
        ) ) ) );

        if ($r->have_posts()) :
?>
        <?php echo $args['before_widget']; ?>
        <?php if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
        <ul>
        <?php while ( $r->have_posts() ) : $r->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
            <?php if ( $show_date ) : ?>
                <small class="wmovie-date"><?php echo get_the_date(); ?></small>
                <hr>
            <?php endif; ?>
            </li>
        <?php endwhile; ?>
        </ul>
        <?php echo $args['after_widget']; ?>
<?php

        wp_reset_postdata();

        endif;

        if ( ! $this->is_preview() ) {
            $cache[ $args['widget_id'] ] = ob_get_flush();
            wp_cache_set( 'movies_widget_recent_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['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        $this->flush_widget_cache();

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

        return $instance;
    }

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

    public function form( $instance ) {
        $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></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 echo esc_html__( 'Number of movies to show:','artor' ); ?></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><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
        <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
    }
}
class News_Recent_Posts extends WP_Widget {

    public function __construct() {
        $widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
        parent::__construct('wpsites-recent-posts', esc_html__('Recent News','artor'), $widget_news);
        $this->alt_option_name = 'News_Recent_Posts';

        add_action( 'save_post', array($this, 'flush_widget_cache') );
        add_action( 'deleted_post', array($this, 'flush_widget_cache') );
        add_action( 'switch_theme', array($this, 'flush_widget_cache') );
    }

    public function widget($args, $instance_news) {
        $cache_news = array();
        if ( ! $this->is_preview() ) {
            $cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
        }

        if ( ! is_array( $cache_news ) ) {
            $cache_news = array();
        }

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

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

        ob_start();

        $title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );

        /** This filter is documented in wp-includes/default-widgets.php */
        $title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );

        $number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
        if ( ! $number_news )
            $number_news = 5;
        $show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;


        $r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
            'posts_per_page'      => $number_news,
            'no_found_rows'       => true,
            'post_status'         => 'publish',
            'post_type'           => array('news',
            'ignore_sticky_posts' => true
        ) ) ) );

        if ($r_news->have_posts()) :
?>
        <?php echo $args['before_widget']; ?>
        <?php if ( $title_news ) {
            echo $args['before_title'] . $title_news . $args['after_title'];
        } ?>
        <ul>
        <?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
            <?php if ( $show_date_news ) : ?>
                <span class="post-date"><?php echo get_the_date(); ?></span>
            <?php endif; ?>
            </li>
        <?php endwhile; ?>
        </ul>
        <?php echo $args['after_widget']; ?>
<?php

        wp_reset_postdata();

        endif;

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

    public function update( $new_instance, $old_instance_news ) {
        $instance_news = $old_instance_news;
        $instance_news['title'] = strip_tags($new_instance['title']);
        $instance_news['number'] = (int) $new_instance['number'];
        $instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        $this->flush_widget_cache();

        $alloptions_news = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions_news['News_Recent_Posts']) )
            delete_option('News_Recent_Posts');

        return $instance_news;
    }

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

    public function form( $instance_news ) {
        $title_news     = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
        $number_news    = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
        $show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></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_news; ?>" /></p>

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

        <p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
        <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
    }
}
function load_widgets() {
    register_widget('Movies_Recent_Posts');
    register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');

1 Ответ

0 голосов
/ 14 апреля 2020

Извините, я исправил. спасибо всем

class Movies_Recent_Posts extends WP_Widget {

    public function __construct() {
        $widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
        parent::__construct('widjet-recent-movies', esc_html__('Recent Movies','artor'), $widget_ops);
        $this->alt_option_name = 'Movies_Recent_Posts';

        add_action( 'save_post', array($this, 'flush_widget_cache') );
        add_action( 'deleted_post', array($this, 'flush_widget_cache') );
        add_action( 'switch_theme', array($this, 'flush_widget_cache') );
    }

    public function widget($args, $instance) {
        $cache = array();
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
        }

        if ( ! is_array( $cache ) ) {
            $cache = array();
        }

        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'] : esc_html__( 'Recent Movies','artor' );

        /** 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;
        $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;


        $r = new WP_Query( apply_filters( 'widget_posts_args', array(
            'posts_per_page'      => $number,
            'no_found_rows'       => true,
            'post_status'         => 'publish',
            'post_type'           => array('movies',
            'ignore_sticky_posts' => true
        ) ) ) );

        if ($r->have_posts()) :
?>
        <?php echo $args['before_widget']; ?>
        <?php if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
        <ul>
        <?php while ( $r->have_posts() ) : $r->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
            <?php if ( $show_date ) : ?>
                <small class="wmovie-date"><?php echo get_the_date(); ?></small>
                <hr>
            <?php endif; ?>
            </li>
        <?php endwhile; ?>
        </ul>
        <?php echo $args['after_widget']; ?>
<?php

        wp_reset_postdata();

        endif;

        if ( ! $this->is_preview() ) {
            $cache[ $args['widget_id'] ] = ob_get_flush();
            wp_cache_set( 'movies_widget_recent_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['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        $this->flush_widget_cache();

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

        return $instance;
    }

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

    public function form( $instance ) {
        $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></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 echo esc_html__( 'Number of movies to show:','artor' ); ?></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><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
        <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
    }
}
class News_Recent_Posts extends WP_Widget {

    public function __construct() {
        $widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
        parent::__construct('widjet-recent-news', esc_html__('Recent News','artor'), $widget_news);
        $this->alt_option_name = 'News_Recent_Posts';

        add_action( 'save_post', array($this, 'flush_widget_cache') );
        add_action( 'deleted_post', array($this, 'flush_widget_cache') );
        add_action( 'switch_theme', array($this, 'flush_widget_cache') );
    }

    public function widget($args, $instance_news) {
        $cache_news = array();
        if ( ! $this->is_preview() ) {
            $cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
        }

        if ( ! is_array( $cache_news ) ) {
            $cache_news = array();
        }

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

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

        ob_start();

        $title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );

        /** This filter is documented in wp-includes/default-widgets.php */
        $title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );

        $number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
        if ( ! $number_news )
            $number_news = 5;
        $show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;


        $r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
            'posts_per_page'      => $number_news,
            'no_found_rows'       => true,
            'post_status'         => 'publish',
            'post_type'           => array('news',
            'ignore_sticky_posts' => true
        ) ) ) );

        if ($r_news->have_posts()) :
?>
        <?php echo $args['before_widget']; ?>
        <?php if ( $title_news ) {
            echo $args['before_title'] . $title_news . $args['after_title'];
        } ?>
        <ul>
        <?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
            <?php if ( $show_date_news ) : ?>
                <span class="post-date"><?php echo get_the_date(); ?></span>
            <?php endif; ?>
            </li>
        <?php endwhile; ?>
        </ul>
        <?php echo $args['after_widget']; ?>
<?php

        wp_reset_postdata();

        endif;

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

    public function update( $new_instance, $old_instance_news ) {
        $instance_news = $old_instance_news;
        $instance_news['title'] = strip_tags($new_instance['title']);
        $instance_news['number'] = (int) $new_instance['number'];
        $instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        $this->flush_widget_cache();

        $alloptions_news = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions_news['News_Recent_Posts']) )
            delete_option('News_Recent_Posts');

        return $instance_news;
    }

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

    public function form( $instance_news ) {
        $title_news     = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
        $number_news    = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
        $show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></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_news; ?>" /></p>

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

        <p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
        <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
    }
}
function load_widgets() {
    register_widget('Movies_Recent_Posts');
    register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...