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; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly. Thank you so much Lucky Soni.
0
<?php if(is_page('homepage'): ?>
content here
<?php endif; ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.