Support » Fixing WordPress » Single Post on Home Page

  • Resolved cableghost


    I would like to show just 1 STICKY post on my home page, but have archives set as in the ‘Reading’ Settings.

    I found the following code in the forum, however, it does show only 1 post, but unfortunately, my page also shows in addition, the sticky post I set.
    <?php get_header(); query_posts('posts_per_page=1'); ?>

    I also found this code to show only sticky posts, however, only 1 post shows in archives as well.
    <?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?>

    How can I make this code, or other code, just show 1 sticky post on my Home page, yet leave the ‘Reading’ Settings for archives?


Viewing 7 replies - 1 through 7 (of 7 total)
  • Create a category “sticky”. Make a note of its category ID. Then put this code in your home.php template. You need to style it of course.

    <?php $sticky = new WP_Query('cat=X&showposts=1'); ?>
    <?php if($sticky->have_posts()) : ?>
    <?php while($sticky->have_posts()) : $sticky->the_post(); ?>
    <?php the_content(__('More &raquo;')); ?>

    Where X in cat=X is your category ID.

    Or you can make a Page (not post) like: About me and select it in the Admin Panel under Settings/Reading as a static startpage.

    Thanks henholland. The problem with a static page is just that, it’s a page. I would rather have a post.

    Ok, do you have a separate archive.php template in your theme? That should control the display of the archive pages.

    I was able to set it up how I want using the sticky php code above…1 sticky post on my blog page and let the ‘Reading’ Settings dictate archives, etc.

    Now, this brings up another hurdle…I would like to make my sticky post title as the page’s <h1> and not the site title. Any suggestions?

    That was a stripped down example I gave you. Somewhere after the beginning of the loop and before the_content, put this:

    <h1><?php the_title(); ?></h1>

    You might have to assign it a class in case the <h1> tag is being used elsewhere. In that case, it would be something like:

    <h1 class="sticky"><?php the_title(); ?></h1>

    And in your stylesheet, create this:

    h1.sticky {
    // put your specific styles here //

    I was able to make it work. I commented out some code and added the following to my home.php:

    <?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Single Post on Home Page’ is closed to new replies.