我有一个名为“博客”的自定义帖子类型。我希望在小部件中显示此自定义帖子类型中选定的帖子数量。但我得到的只是“找不到列表”,如果找不到帖子,那就是else语句。有帖子。怎么了?
My custom post type:
function blog_post_type() {
$labels = array(
\'name\' => \'Blog\',
\'singular_name\' => \'Blog post\',
\'add_new\' => \'Add new blog post\',
\'add_new_item\' => \'Add new blog post\',
\'edit_item\' => \'Edit blog post\',
\'new_item\' => \'New blog post\',
\'all_items\' => \'All blog posts\',
\'view_item\' => \'View blog post\',
\'search_items\' => \'Search blog posts\',
\'not_found\' => \'No blog posts found\',
\'not_found_in_trash\' => \'No blog posts found in trash\',
\'menu_name\' => \'Blog posts\',
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'supports\' => array(
\'title\',
\'editor\',
\'author\',
\'thumbnail\',
\'excerpt\',
\'post-formats\' ),
\'has_archive\' => true,
\'taxonomies\' => array( \'post-tag\' ),
\'show_in_menu\' => true,
);
register_post_type( \'blog posts\', $args );
}
add_action( \'init\', \'blog_post_type\' );
My widget: class Blog_Widget extends WP_Widget {
function __construct() {
parent::__construct(
\'blog_widget\', // base id
\'Blog Widget\', // Name
array(
\'description\'=> __(\'Displays the latest post excerpt\')
)
);
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'numberOfListings\'] = strip_tags($new_instance[\'numberOfListings\']);
return $instance;
}
function form($instance) {
if($instance) {
$title = esc_attr($instance[\'title\']);
$numberOfListings = esc_attr($instance[\'numberOfListings\']);
}else {
$title = \'\';
$numberOfListings = \'\';
}
?>
<p>
<label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title\', \'blog_widget\'); ?> </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(\'numberOfListings\'); ?>"><?php _e(\'Number of Listings:\', \'blog_widget\'); ?> </label>
<select id="<?php echo $this->get_field_id(\'numberOfListings\'); ?>" name="<?php echo $this->get_field_name(\'numberOfListings\'); ?>">
<?php for($x=1; $x<=10; $x++): ?>
<option <?php echo $x == $numberOfListings ? \'selected="selected"\' : \'\'; ?> value="<?php echo $x; ?>"><?php echo $x; ?> </option>
<?php endfor; ?>
</select>
</p>
<?php
}
function widget($args, $instance) {
extract( $args ) ;
$title = apply_filters(\'widget_title\', $instance[\'title\']);
$numberOfListings = $instance[\'numberOfListings\'];
echo $before_widget;
if($title) {
echo $before_title . $title . $after_title;
}
$this->getBlogExcerpt($numberOfListings);
echo $after_widget;
}
function getBlogExcerpt($numberOfListings) { //html
global $post;
add_image_size( \'realty_widget_size\', 85, 45, false );
$listings = new WP_Query();
$listings->query(\'post_type=blog&posts_per_page=\' . $numberOfListings );
if($listings->found_posts > 0) {
echo \'<ul class="realty_widget">\';
while ($listings->have_posts()) {
$listings->the_post();
$image = (has_post_thumbnail($post->ID)) ? get_the_post_thumbnail($post->ID, \'realty_widget_size\') : \'<div class="noThumb"></div>\';
$listItem = \'<li>\' . $image;
$listItem .= \'<a href="\' . get_permalink() . \'">\';
$listItem .= get_the_title() . \'</a>\';
$listItem .= \'<span>Added \' . get_the_date() . \'</span></li>\';
echo $listItem;
}
echo \'</ul>\';
wp_reset_postdata();
}else{
echo \'<p style="padding:25px;">No listing found</p>\';
}
}
}
它默认为什么
else
\'找不到列表“”?有可用的帖子。谢谢大家!