0

我有一个 PHP 片段,我只想出现在我指定的 WordPress 主页上。

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

这是片段:

    <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 ...

    } ?>

然而,当我将这个额外的 PHP 应用到整个代码段时,它只会破坏代码段的语法。

有人可以建议吗?我不是最流利的 PHP,如果问题有点令人困惑,我很抱歉,但是任何帮助都会得到很大的帮助。

非常感谢,马特

4

2 回答 2

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; ?>
于 2013-03-22T10:24:43.400 回答
0
<?php if(is_page('homepage'): ?>
content here
<?php endif; ?>
于 2013-03-22T10:25:33.603 回答