is_page() 使用模板文件显示不同的 WordPress 帖子内容


is_page() to display different wordpress post content using a template file

我正在尝试使用 *is_page()* 显示以在单个模板文件中显示 2 个不同的内容.. 我的代码看起来像

<?php
    if ( is_page('news') ) {
        // Insert HTML/PHP for News - code shown below
    }
    else ( is_page('review') ){
         // Insert HTML/PHP for Review - code shown below
    }
?>

这对我不起作用...不知道为什么?我已经展示了我尝试在下面插入的新闻/评论代码

HTML/PHP for News

<div class="news">
  <?php query_posts('cat=-89');?>
  <?php while ( have_posts() ) : the_post(); ?>
  <article class="summary">
    <div class="content_wrap cont_page ">
      <header>
        <h2> <a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?> </a></h2>
        <p> <span class="author">
          <?php the_author() ?>
          </span><br />
          <span class="date">
          <?php the_date(); ?>
          </span> 
      </header>
      <div class="entry-content">
        <?php the_excerpt(); ?>
               <?php include('share.php')?> 
      </div>
      <aside class="right">
        <?php the_post_thumbnail(); ?>
      </aside>
    </div>
  </article>
  <?php
  <?php endwhile; ?>
</div>

用于审查的 HTML/PHP

<div class="review">
  <?php query_posts('cat=89,246,247,248');?>
  <?php while ( have_posts() ) : the_post(); ?>
  <article class="summary 
  <?php
  // Custom posts stylings
if ( has_post_format( 'video' )) {
    echo 'video';
} elseif ( in_category('orange') ) {
    echo 'orange';
} else {
    echo 'white';}
?>

  ">
    <div class="content_wrap cont_page ">
      <div class="entry-content">
        <?php  if ( has_post_format( 'video' )) {
    // Get the video URL and put it in the $video variable
                        $videoID = get_post_meta($post->ID, 'video_url', true);
                        // Echo the embed code via oEmbed
                        echo '<iframe src="http://player.vimeo.com/video/'.$videoID.'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="405" height="285" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'; 
                    //  echo '<iframe src="http://player.vimeo.com/video/'.$videoID.'" width="405" height="285" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
            }
                else the_post_thumbnail();
 ?>
      </div>
      <aside class="right">
        <header>
          <h2> <a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?> </a></h2>
        </header>
        <div class="entry-content">
          <?php the_excerpt(); ?>
        </div>
        <?php include('share.php')?>
      </aside>
    </div>
  </article>
  <?php
// DISABLED/S default format
// get_template_part( 'content', get_post_format() ); ?>
  <?php endwhile; ?>
</div>

使用页面 ID 作为 is_page() 的参数,以便更准确。您可能会在某个时候编辑页面名称,然后代码将失败。

if(is_page(10)) { 
    // do something
  } else if (is_page(20)) {
    // do something
  } else { 
    // do something else
}

我很确定这是一个错字,但如果以下情况,下面的行应该是其他的:

else ( is_page('review') ){

此外,在两个模板之后,您都有额外的开放 php 大括号。