WordPress:执行四个条件

时间:2016-08-14 05:30:08

标签: wordpress search conditional-statements

请问,我怎样才能完成这四个条件,了解与搜索结果有关的这些命令

        <?php 
    while(have_posts())
    {
    the_post();
      if ($post->post_type == "book") {
         get_template_part( 'search', 'book' );
      }
      elseif ($post->post_type == "video")
      { get_template_part( 'search', 'video' ); }
      else { 
         get_template_part( 'search', 'normal' );

      } else { 
?>
          <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
<?php
    }
}
    ?>

感激

2 个答案:

答案 0 :(得分:0)

我想您忘记结束while循环,请使用以下代码:

   <?php 
    while(have_posts())
    {
    the_post();
      if ($post->post_type == "book") {
         get_template_part( 'search', 'book' );
      }
      elseif ($post->post_type == "video")
      { get_template_part( 'search', 'video' ); }
      else { 
         get_template_part( 'search', 'normal' );

      } else { 
?>
          <p>Sorry, but nothing matched your search criteria. Please try    again with some different keywords.</p>
    <?php
    }}
    ?>

答案 1 :(得分:0)

谢谢你的回复 问题是为什么状态被放置在以下条件之前

 while(have_posts())
{
the_post();

正确的代码:

<?php if ( have_posts() ) : ?>
<?php 
while(have_posts())
{
the_post();
  if ($post->post_type == "book") { get_template_part( 'search', 'book' ); }
  elseif ($post->post_type == "video") { get_template_part( 'search', 'video' ); }
  else { get_template_part( 'search', 'normal' ); }
} 
else : ?>
      <p>Sorry, but nothing matched your search criteria. Please try    again with some different keywords.</p>
<?php endif; ?>