Ready to get started?Download WordPress


[resolved] Single Post on Home Page (8 posts)

  1. cableghost
    Posted 4 years ago #

    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?


  2. Joni
    Posted 4 years ago #

    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.

  3. henkholland
    Posted 4 years ago #

    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.

  4. cableghost
    Posted 4 years ago #

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

  5. henkholland
    Posted 4 years ago #

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

  6. cableghost
    Posted 4 years ago #

    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?

  7. Joni
    Posted 4 years ago #

    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 //
  8. cableghost
    Posted 4 years ago #

    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'))); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic