WordPress

WordPress: After the First Post only on the Home Page

You’ll note on after the first page after the first post on my home page that I’ve added a Blaugh cartoon. I had one heck of a time figuring out how to simply show the cartoon in one single place on the site without pushing it into the sidebar where it didn’t belong. So… I did some digging and found a couple of themes that utilize some code to do just this. The code could be used to highlight or indicate your latest post… or simply add some content within the WordPress loop.

Here’s how you can do it:
<?php if(is_home() && $post==$posts[0] && !is_paged()) { ?>
Your Content that you want here!
<?php } ?>

For the Blaugh cartoon:
<?php if(is_home() && $post==$posts[0] && !is_paged()) { ?>
<script language="Javascript" type="text/javascript" src="http://blaugh.com/latest.php's=400"></script>
<?php } ?>

There you go! Just make sure that you put this content within the loop. I put it right before this line, so I can keep track of it:
<?php endwhile; else: ?>

14 Comments

  1. 1

    Douglas,

    I just wanted to drop you a quick thank you for this tip. I had written an article with a slightly different method that was less effective than yours and one of my readers pointed out your article to me.

    I have since modified my article and given credit where credit is due.

    Thanks again,

    John

  2. 3
  3. 4

    Can I implement this code in the sidebar so that the ads only appear in the sidebar on the homepage?
    If not, can you please enlighten me on how I can do this?
    Thank you!
    Tyler

  4. 5

    To get this code to work over in the sidebar, or anywhere outside of the while loop, just remove all the stuff about $post and $page from the code so you just have this;

    stuff goes here

  5. 6
  6. 7
  7. 9

    Many thanks for this tip Douglas. Is there a way to add ads on specific pages instead of homepage? I every page has a pageId and we can identify the loaded page based on that..i am looking out for specific pointers.

    Thanks in advance
    Vaibhav

  8. 10

    Really useful code, and I’ve been using it for a while. Is there a way to include an ‘else’ statement so that you can have ads that run on the homepage, and then others that run on every other page?

    I’ve been searching for ages and can’t find it!

    Hope someone can help.

    Thanks in advance.

    James

  9. 12

    Hello Douglas…

    I am working whit a plugin on my homepage… But the coderguidelines document has the restriction of don't touch the core theme code… so i am using the add_filter function…

    if(is_home()) add_filter('the_content','myfunction');

    But is doesn't work correctly…. this filter show me the pluging content in every post in my homepage… what can i do ? does exist another hook just for the content of the homepage ?

    Thanks…

  10. 13

Leave a Reply