问题描述
前提条件:
我创建了 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',) // 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;
}
}
并将其定位在 front-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>';
}
?>