如何在 WordPress 的 Widget 中列出多个页面?

How To List In Widget Several Pages At WordPress?

先决条件:

我创建了 2 个页面:

我创建了小部件

<?php class listArchive extends WP_Widget {

/**
 * Register widget with WordPress.
 */
function __construct() {
    parent::__construct(
        'listarchive', // Base ID
        esc_html__( 'listarchive', 'text_domain' ), // Name
        array( 'description' => esc_html__( 'Widget regarding archive posts', 'text_domain' ), ) // Args
    );
}

/**
 * Front-end display of widget.
 *
 * @see WP_Widget::widget()
 *
 * @param array $args     Widget arguments.
 * @param array $instance Saved values from database.
 */
public function widget( $args, $instance ) {
    echo $args['before_widget'];
    if ( ! empty( $instance['title'] ) ) {
        echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
    }
    echo esc_html__( 'Best archive', 'text_domain' );
    echo $args['after_widget'];
}

/**
 * Back-end widget form.
 *
 * @see WP_Widget::form()
 *
 * @param array $instance Previously saved values from database.
 */
public function form( $instance ) {
    $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Best Archive', 'text_domain' );
    ?>
    <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
    </p>
    <?php
}

/**
 * Sanitize widget form values as they are saved.
 *
 * @see WP_Widget::update()
 *
 * @param array $new_instance Values just sent to be saved.
 * @param array $old_instance Previously saved values from database.
 *
 * @return array Updated safe values to be saved.
 */
public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';

    return $instance;
}

}

并将它放在前面-page.php

<?php
    get_header();
?>
<div class="container">
    <div class="widgets">
        <div class="table_bg width_fifty"><?php get_sidebar('listarchive'); ?></div>
    </div>
</div>
<?php
    get_footer();
?>

任务:我需要在此小部件中列出红色存档页面和白色存档页面。

问题:我不知道如何设置小部件以列出红色存档页面和白色存档页面,请帮助。

<?php
/**
 * Query just the 2 pages
 **/

//EDIT FOR WIDGET TITLE
echo '<h3> Widget Title </h3>'; //Or whatever tag you want the title in

//Get the two posts from the DB that match these ID's, replace the IDs with your own.
foreach( get_posts(['include' => ['123','456']]) as $post ){

  //For each of these posts, spit the title. Change the p tag to whatever you need.
  echo '<p>' . $post->post_title . '</p>';

}

/**
 * Query posts by term
 * This will allow you to add posts to categories and query multiple categories for posts to show.
 **/
foreach( get_posts([
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => ['term1', 'term2', 'term3'],
            'operator' => 'IN'
        ]        
    ],
    'exclude' => ['1','2','3'] //In case there is anything you want to ommit
]) as $post ){

//For each of these posts, spit the title. Change the p tag to whatever you need.
echo '<p>' . $post->post_title . '</p>';

}
?>