2013-03-22 43 views
0

我有一段PHP,我只想出現在我指定的WordPress主頁上。在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> 

此代碼的工作完美,但會出現在所有頁面上,而我希望它只是激活頁面上的102

的ID我猜測的條件語句必須是這樣的:

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

    ... snippet here ... 

    } ?> 

然而,當我申請圍繞整個片段這種額外的PHP包裝,它只是破壞了片段的語法。

有誰能提醒一下嗎?我不是PHP中最流利的,所以很抱歉如果問題有點混亂,但任何幫助都會得到很大的迴應。

提前許多感謝, 馬特

回答

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; ?> 
+0

這一個工程了。 – thenetimp 2013-03-22 10:26:30

+0

完美的作品。非常感謝Lucky Soni。 – matttickner 2013-03-22 11:08:42

+0

很高興它爲你工作。 :) – 2013-03-22 11:15:18

0
<?php if(is_page('homepage'): ?> 
content here 
<?php endif; ?>