0

I have a snippet of PHP that I want to only appear on my designated WordPress homepage.

I have been successful with conditional statements on other parts of the WordPress site, but this one is bamboozling me.

This is the snippet:

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

This code works perfectly, but appears on ALL pages, whereas I want it to only activate on the Page with an ID of 102.

I'm guessing the conditional statement has to be something like:

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

    ... snippet here ...

    } ?>

Yet when I apply this extra PHP wrapping around the whole snippet, it just ruins the syntax of the snippet.

Can anyone advise please? I'm not the most fluent in PHP, so sorry if the question is a little confusing, but any help would be greatly received.

Many thanks in advance, Matt

2 Answers 2

1

use

<?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; ?>
1
  • Works perfectly. Thank you so much Lucky Soni. Mar 22, 2013 at 11:08
0
<?php if(is_page('homepage'): ?>
content here
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.