• Here is what I’m trying to do. I only want one specific post to show on the main page of the blog. I want viewers to have to go to the categories on the right to view more posts. The post I want to show on the main page is post id #1. It’s the first post created in the blog, and just a welcome message of sorts. I’ve edited the loop in index.php to the point where it’s only showing one post. But even though I replaced “<div class=”post” id=”post-<?php the_ID(); ?>”>”, with “<div class=”post” id=”post-1″>”, it’s showing the most recent post, rather than the first one.

    Anybody know how I can tell it to only show that first one? I don’t even care if you can comment on it. I’m almost to the point where I’m just going to delete the code pulling in posts, and just add in my own info for a welcome message.

Viewing 12 replies - 1 through 12 (of 12 total)
  • <?php query_posts('p=1'); ?>

    Follow that up with all your code to format whatever pieces of that post you want to show on your main page and you should be all set.

    You probably want

    <?php if (is_home()) {query_posts(‘p=1’);} ?>

    so that it only effects the main page If you do just what tsguitar said then I believe affects a bunch of pages (depending).

    And to get the next/previous doesn’t show up in other pages and work, You might have to do something like

    if (is_home()){ $page = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(“p=1&paged=$page”);
    }

    Thats all from fiddling with the code and looking around for about a week, it works for me.

    Thread Starter ctw

    (@ctw)

    Awesome! Thanks a lot. I’ll try this out and report back on whether or not I get it to work.

    If you put my code into the main page template, it will only impact the main page. It’s redundant to use the is_home conditional because that main page template *is* home already, nothing else.

    I actually took that code from a site I’m running that does almost exactly what ctw wants to do so I know it works fine (on a WP 2.0.4 install!).

    sanchit, your code looks more complicated than is needed, but let’s see what the report back is. It shouldn’t be that lengthy to get this working the way ctw needs it to be.

    Thats if he doesn’t have a home.php like I don’t. Just making it specific and avoiding the problems that I ran into.
    I am running 2.2 and am using my code on my site! ??

    Got it. I don’t have home.php, but I do have index.php. And yeah, if that code is put anywhere other than index.php, that condition is needed. What’s that second bit of code you posted? If the query_post is only asking for one post, there will never be next or previous links.

    I don’t have one post on a page. But if you use query post then the silly forward / back links don’t work. I don’t think it’ll affect him since he only has one post but its there if he has that issue in the future if he changes his mind.

    I also included it because if he has a workaround to having pages include posts he’ll need that to make sure the next/back posts are working.

    This is my website and the comics section has a comic switcher thing setup with the comics being actual posts solely in the Comic category.

    Since I just have index.php handle all pages and the main page I needed that code to make everything functional.

    The only issue I have is that I want the latest day’s worth of posts in the front page (Front page already excludes all comic posts) AND the next/back thing to work to switch between days. But everyone always dodges that question in the support thread…

    I might need to do something crazy and maybe use the wp-calendar code and fiddle with it but if theres an easier way I’d rather do that first.

    I think my sites running on the bare minimum stuff.. index.php archive.php search.php comments.php… I don’t really get the difference between search.php and archive.php but thats not too much of an issue. So yeah… I’ve had to do a LOT of fiddling around these days to get the site working the way I want it.

    Thread Starter ctw

    (@ctw)

    Ok, well I can’t seem to get it to work. I’m modifying the code in index.php. But it’s still showing the newest post only, rather than the one I’m specifying, which is post #1. Here’s what my code in index.php looks like:

    <?php get_header(); ?>

    <div id=”content” class=”narrowcolumn”>

    <?php if (is_home()) {query_posts(‘p=1’);} ?>

    <?php if (is_home()){ $page = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(“p=1&paged=$page”);
    } ?>

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h2>
    <small><?php the_time(‘F jS, Y’) ?> <!– by <?php the_author() ?> –></small>

    <div class=”entry”>
    <?php the_content(‘Read the rest of this entry »’); ?>
    </div>

    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
    </div>

    <div class=”navigation”>
    <div class=”alignleft”><?php next_posts_link(‘« Previous Entries’) ?></div>
    <div class=”alignright”><?php previous_posts_link(‘Next Entries »’) ?></div>
    </div>

    </div>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    Thread Starter ctw

    (@ctw)

    Oh well. I went ahead and just pulled out the code for pulling in posts, and instead created a static welcome message. It looks good, and serves my purpose. Not as easy for my users to edit, but oh well.

    Thank you so much for the time you put into helping me. It’s much appreciated.

    Hmm. Nobody ever thought about giving a try to the get_a_post plugin?

    It does exactly what was asked for here: display a “specific” post (defined by ID) wherever you want:
    https://guff.szub.net/2005/01/27/get-a-post/

    It didn’t work because you didn’t use The Loop!

    This code below should give you exactly what you want. And didn’t know a “get_a_post” plugin existed.

    You had a tiny error in the code with the forward/next links, you just switched them around. You will note however that they won’t show up on the main page. This is because theres only one post to always display there.

    <?php get_header(); ?>
    <?php if (is_home()) {query_posts('p=1');} ?>
    <?php if (is_home()){ $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("p=1&paged=$page");} ?>
    <div id="content">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <div class="entry">
     <h2 class="etitle" id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content('Read the rest of this entry ?'); ?>
    </div>
    <p class="postmetadata"><?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?>
    <?php comments_template(); // Get wp-comments.php template ?>
    </div>
    <?php endwhile; else: ?>
    <?php _e('Sorry, no posts matched your criteria.'); ?>
    
    <?php endif; ?>
    <div class="navigation">
    <div class="alignleft"><?php previous_posts_link('? Previous Entries') ?></div>
    <div class="alignright"><?php next_posts_link('Next Entries ?') ?>
    </div>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    sokai

    (@sokai)

    Hi! ??

    Can someone tell me why the following code breaks my pagination in WP 2.3.1? – I try to show all posts on the “author” page (from the author) with two loops (for my two caterories) with each five posts…

    This is the code I use for the category-loop:

    <?php  $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string . "&category_name=texte&showposts=5&paged=$page"); // the first category loop
    if (have_posts()) : ?>
    <h1>category1</h1>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <h1><a>" rel="bookmark" title="permalink ''<?php the_title(); ?>''">&ldquo;<?php the_title(); ?>&rdquo;</a> at <?php the_time('j. F Y') ?> &raquo;</h1>
    </div>
    <?php endwhile; ?>
    <?php wp_pagenavi('<p class="center">','</p>','back','next',5,false); ?>
    <?php endif; rewind_posts(); ?>

    I get the correct pagination-numbers (in my case “3”) but if I click on “3” (=page 3) I get “404”… ??

    Thanks a lot and best regards,
    Kai

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘1 specific post on main page’ is closed to new replies.