treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Latest Blog Post On Homepage?

  • I have a Wordpress Homepage and a Blog Page. Somehow I would like to have the latest/most recent blog post to be on the homepage. I tried googling and inserting a few php codes but none seemed to work.

    Anyone know of a quick tutorial or solution to this?
  • I'm assuming I could use this php code and reference that... if it's the homepage, display recent post.


    <?php if { is_home(); ?>

    ...get recent post


    whatever, i can't code php, but you get my point.
  • You could just use query_posts within that if statement at the start of your loop, or you could use a custom loop for index.php and use it in there:
    <? php query_posts('posts_per_page=1'); ?>
  • By the way I really recommend chris's book diggin into wordpress, it explains this in there. The PDF copy is only $27 and well worth it if you're considering using wordpress on a regular basis.
  • Here's my problem though, my index.php is for domain.com/blog and not domain.com. So if I change anything to the index.php file, it changes the blog page. Would I have to create a seperate php file for my home page or?

    I have these pages made on wordpress

    -Home (My "front page")
    -Blog (Where My Blog Posts are set at on my settings)
    -Services (Services Page)
    -Portfolio (Portfolio Page)
    -Contact (Contact Page)
  • I always prefer the second option, someone else might be able to advise you better on a way to achieve it using the first way though. One option could be to pull the latest post using RSS, although there may be a better way.
  • Thanks. Basically I want exactly how @ChrisCoyier has his set up on his personal website's homepage and blog.

    He has his latest post on the homepage AND a blog page for multiple posts.


    I should mention I put my wordpress files in a folder called "wordpress" so I had to copy the index.php file (below) to my root directory.



    <?php
    /**
    * Front to the WordPress application. This file doesn't do anything, but loads
    * wp-blog-header.php which does and tells WordPress to load the theme.
    *
    * @package WordPress
    */

    /**
    * Tells WordPress to load the WordPress theme and output it.
    *
    * @var bool
    */
    define('WP_USE_THEMES', true);

    /** Loads the WordPress Environment and Template */
    require('./wordpress/wp-blog-header.php');
    ?>
  • Here is a visual.

    image
  • ahh, what you need to do is the query posts as Johnny suggested and create a separate page for your homepage and your blog, page-slug.php, slug being the title of the page, created in wp admin, then have your index be a basic loop, with links to header and footers as index.php is mainly a fallback if there isn't any other pages for them to use, so if there isn't a single.php, or a page.php, they fall back to index.php. check out template hierarchy here:
    http://codex.wordpress.org/Template_Hierarchy
  • I found an alternate way to do this without creating custom pages.

    I installed "inline posts" plugin for wordpress.

    This way I just enter "[[post ID]]" in my home page and it pops up. The only downside is I will have to do this for every post I create. The upside is, it only takes 30 seconds.


    But if someone wants to make the process more clearly for others, it is much appreciated!
  • that works also^^