在没有循环的情况下从特定类别中获取帖子的标题


get the title of post from specific category without loop

我正在尝试这个代码:

<ul>
<?php 
$casino_news = new WP_Query('category_name=casino_games'); 
while($casino_news ->have_posts()) :
 $casino_news ->the_post(); 
?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>

它非常适合我。但如果我试图搜索该帖子不存在,它将不会在该小部件上显示任何内容。

在这种情况下我能防止什么?

试试这个,我希望它能起作用,如果你遇到任何问题,请告诉我。

<ul>
<?php 
$casino_news = new WP_Query('category_name=casino_games'); 
while($casino_news->have_posts()) :
 if($casino_news->get_the_post() == '' ){
     $casino_news->the_title(); 
 }else{
     $casino_news->the_post(); 
 } 
?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>