Have lasts posts in index show title only
-
Hello everyone,
I’ve seen some blogs, mostly magazine themes, have the last posts of their index only show the headline. For example, the last 5 posts of the blog will show the title only.
Does anyone know how to achieve this?
Any help would be appreciated. Thanks in advance.
??
-
You just need to put a counter in the loop and show the full post when it is 1:
<?php if (have_posts()) : $count = 1; while (have_posts()) : the_post(); if ($count == 1 && (is_home() || is_front_page())) { // Do the code for the full post } else { // Do the code for the titles } endwhile; // Do pagination code else; // Do not-found endif;
Thanks so much, I’ll try this and see how it goes.
Even easier you can place:
query_posts('showposts=5');
Before the “while” and it will only get the last 5.
Please notice you’ll have to use something like:
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
Inside the loop.
Here a full example:
<?php query_posts('showposts=5'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> </div> <?php endwhile; ?> <?php else : ?>
Regards,
quicoto.
I think your example will not show the first full post. And, if you use two separate loops, pagination will not work.
What exactly does $count = 1 mean? Does that mean the last post or the first post?
Is this what it means: if it’s the 1st post, show full post. If it’s not the 1st post, show title only?
This statement:
if ($count == 1 && (is_home() || is_front_page())) { // Do the code for the full post } else { // Do the code for the titles }
in English says:
If count is one and this is either the home page or the front page, show the full post otherwise show the title.
But, I left out a little bit of code. The sample if statement should be:
if ($count++ == 1 && (is_home() || is_front_page())) {
Note the ++!
Ok this works! Thanks so much! But it only shows the 1st post as a full post. The rest are titles.
How do I write it so that it’s the last 5 posts of the front page?
OR how would I write it I wanted it to be posts 5th-10th on the front page?
Ok I got it.
I have 20 posts within the first page.
This is how I wrote it for my theme:
<?php if (have_posts()) :
$count = 1;
while (have_posts()) : the_post(); ?><?php
if (($count < 16) && (is_home() || is_front_page())) { ?>// I wrote less than 16 so it would only do the last 5 posts of the page.
// Code for the full post
<?php } else { ?>
” rel=”bookmark”><?php the_title(); ?>
<?php } ?>
<?php $count++; endwhile; ?>
Then I had to add this counter to create the increment from the original counter.
<?php endif; ?>Thanks for all the help, vtxyzzy.
Would you or anyone know how to show only the headline for 4 the most recent posts of the previous day?
You are welcome. I don’t have a ready answer for getting posts from the previous day, but I suspect you will need a filter for the query. I will look around some, but I think you should open a new thread to get others to look.
If you will, please use the dropdown at top right to mark this topic ‘Resolved’.
OK, here is a way to get the posts from the previous day:
<?php //based on Austin Matzko's code from wp-hackers email list function filter_where($where = '') { //posts from yesterday $where .= " AND post_date = '" . date('Y-m-d', strtotime('-1 day')) . "'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts($query_string . '&posts_per_page=4'); // Loop to display the posts ?>
I think this “calls” the posts from the most recent 4 posts from the previous day, but I’m not sure if that’s what I was looking for.
I might have been unclear. But here’s what I’m looking for.
May 25 (Current Day)
Post 1
Post 2
Post 3
Post 4
Post 5
May 24 (Previous Day)
Post 6
Post 7
Post 8
Post 9
Post 10
How do I make it so that Post 6-9 shows their title only, and Post 10 shows up regularly?
Yep, I misunderstood what you want. A few questions:
Do you want titles for posts 1-4 and a full post for 5, or full only for post 10?
How many days do you want to go back – only to yesterday?
What do you want to do when there are no posts for yesterday?
What do you want to do when there are less than 5 posts for a given day?
This could get really hairy!!
A full post for 1-5, title only 6-9, then a full post for 10.
And I suppose we just want the day to go back to yesterday. If there’s no posts for yesterday, then how about the day before? Or we could just do nothing, no change?
And if there are less than 5 posts for a given day, then maybe no change? Or just change it to title only anyway?
When you put it that way, this does sound very messy! I hope this isn’t too much trouble.
The best way I can think of to do this is to use two separate loops; the first one for today, and the second for yesterday.
This example (adapted from the Codex) will return posts from just today:
$today = getdate(); $args = array( 'year' => $today['year'], 'monthnum' => $today['mon'], 'day' => $todsy['mday'], 'posts_per_page' => 5 ); query_posts($args);
For the second loop, something like this (UNTESTED):
$yesterday = getdate(strtotime('-1 day')); $args = array( 'year' => $yesterday['year'], 'monthnum' => $yesterday['mon'], 'day' => $yesterdsy['mday'], 'posts_per_page' => 5, 'caller_get_posts' => 1, ); $newquery = new WP_Query($args); if ($newquery->have_posts()) { $count = 0; while ($newquery->have_posts()) { $newquery->the_post(); if (++$count < $newquery->post_count) { //show title only } else { //show full post } } }
- The topic ‘Have lasts posts in index show title only’ is closed to new replies.