我正在创建一个小部件,用于显示我的帖子类型“推荐”中的数据。小部件的后端工作正常,显示了一个推荐列表,并允许我选择要显示在前端的内容。
然而,我的WP\\u查询似乎根本没有引入任何数据。
这是我的小部件前端的代码。
public function widget( $args, $instance ) {
// extract the widget arguments
extract( $args );
$testimonial = $instance[\'testimonials\'];
$options = $instance[\'options\'];
// If All option selected display all testimomials
if ( $options == \'All\' ) {
$args = array ( \'post-type\' => \'testimonials\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 20,
\'orderby\' => \'title\',
\'order\' => \'ASC\'
);
// else only display the selected testimonial
} else {
$args = array( \'post-type\' => \'testimonials\',
\'post-status\' => \'publish\',
\'post_per_page\' => 20,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'post__in\' => $testimonial );
}
$my_query = new WP_Query( $args );
?>
<div id="rotate">
<!-- WordPress Loop -->
<?php if ( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post() ?>
<p> <!-- echo the testimonial content, trim white space and strip out any unwanted html tags -->
"<?php echo trim( strip_tags( get_the_content() ) ); ?>"<br/>
<!-- print out the company name -->
<span><?php the_title(); ?></span>
</p>
<?php endwhile; endif;
// reset WP query, restores $wp_query and global post data to original main query
wp_reset_query(); ?>
</div>
<?php }
以及我用来注册帖子类型的代码
register_post_type( \'testimonials\',
array(
\'labels\' => array(
\'name\' => __( \'Testimonials\' ),
\'singular\' => __( \'Testimonial\' ),
\'add_new_item\' => __( \'Add New Testimonial\' ),
\'edit_item\' => __( \'Edit Testimonial\' )
),
\'public\' => true,
\'has_archive\' => true,
\'menu_position\' => 20,
\'menu_icon\' => \'dashicons-groups\',
\'supports\' => array( \'title\',\'editor\')
)
);
有人能看出我错在哪里吗?任何帮助都将不胜感激。