【问题标题】:Constructing conditional statement in WordPress在 WordPress 中构建条件语句
【发布时间】:2013-03-22 18:25:33
【问题描述】:

我有一个 PHP 的 sn-p,我只想出现在我指定的 WordPress 主页上。

我已经成功地在 WordPress 网站的其他部分使用了条件语句,但是这个让我感到困惑。

这是sn-p:

    <div id="home-post">
    <h1 id="homePost">Latest News</h1>  

    <?php

    $args = array( 'numberposts' => 1 );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) : setup_postdata($post); ?>

    <h2><a class="homeLink" href="<?php the_permalink(); ?>"><?php the_title(); ?>:</a></h2>

    <?php the_content(); ?>

    <?php endforeach; ?>

    </div>

此代码运行良好,但出现在所有页面上,而我希望它仅在 ID 为 102 的页面上激活。

我猜条件语句必须是这样的:

    <?php
    if (is_page ( '102' )) {

    ... snippet here ...

    } ?>

然而,当我在整个 sn-p 周围应用这个额外的 PHP 时,它只会破坏 sn-p 的语法。

谁能给点建议?我不是最流利的PHP,如果问题有点混乱,很抱歉,但任何帮助都会得到很大的帮助。

提前非常感谢, 马特

【问题讨论】:

    标签: php wordpress code-snippets conditional-statements


    【解决方案1】:

    使用

    <?php if(is_home()) : ?>
    <?php
      $args = array( 'numberposts' => 1 );
      $lastposts = get_posts( $args );
      foreach($lastposts as $post) : setup_postdata($post); 
    ?>
    
    <h2><a class="homeLink" href="<?php the_permalink(); ?>"><?php the_title(); ?>:</a></h2>
    
    <?php the_content(); ?>
    
    <?php endforeach; ?>
    
    </div>
    <?php endif; ?>
    

    【讨论】:

    • 完美运行。非常感谢幸运索尼。
    【解决方案2】:
    <?php if(is_page('homepage'): ?>
    content here
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 1970-01-01
      • 2012-12-01
      • 2013-10-06
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多