Latest 5 posts from each category
-
Hi everyone,
I’m a newbie to wordpress & php coding. I’m using Arthemia v.2 theme and I want to change the way the posts are displayed on the frontpage.
As you can see from the Arthemia demo website (http://michaelhutagalung.com/arthemia/), posts are displayed with the title, image and excerpt with latest on top.
How can I display the posts by category? I have 5 categories and for each category in the blog, I only want to show the latest 5 posts displayed on the frontpage.
Thanks in advance!
-
Forgot to mention that I don’t want to show the image or excerpt of each post. I like to follow news website style of showing the post titles in a list and possibly have a ‘More’ link to show more posts from the category.
Like CNN in US, World, Business categories on the front page:
http://www.cnn.com/Similar to this theme, this is what I’m looking for:
http://sponsoredwp.info/brightness/
How can i include php code to display the section: Latest Posts in XXX Category?
Any hints, simple tutorials or php code will be highly appreciated.
this could be a possible frame work for what you like to do:
it will go through all categories; and post up to 5 latest posts with the top line:<?php wp_reset_query(); $cats = get_categories(''); foreach ($cats as $cat) : $args = array( 'posts_per_page' => 5, 'cat' => $cat->term_id, ); query_posts($args); if (have_posts()) : echo '<h2>Latest Posts in '.$cat->name.' Category</h2>'; while (have_posts()) : the_post(); ?> <!-- this area is for the display of your posts the way you want it --> <!-- i.e. title, exerpt(), etc. --> <?php endwhile; ?> <?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>'; endif; wp_reset_query; ?> <?php endforeach; ?>you need to fill in the part what you want to display for each post, and how.
good luck πThis is great, thanks alchymyth :-). I’ll give it a try and I’ll get back to you.
@nandla
please start a new topic and describe your problems there.This is a spectacular trick
i has tried your code but i found a problem, the post can’t display on each category title, and I tried adding a code on the code that you created, and look like this :
<?php wp_reset_query(); $cats = get_categories(''); foreach ($cats as $cat) : $args = array( 'posts_per_page' => 5, 'cat' => $cat->term_id,); query_posts($args); if (have_posts()) : echo '<h2>Latest Posts in '.$cat->name.' Category</h2>'; ?> <?php while (have_posts()) : the_post(); ?> <div> <h2> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> </h2> </div> <?php if ( is_category($vidcat) ) { the_content(); } else { echo strip_tags(get_the_excerpt(), '<a><strong>'); } ?> <!-- this area is for the display of your posts the way you want it --> <!-- i.e. title, exerpt(), etc. --> <?php endwhile; ?> <?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>';?> <?php endif; wp_reset_query; ?> <?php endforeach; ?>But i’m still confused, how to display post just in category parent, not a sub category?
try and change this line in the query_posts() $args:
'cat' => $cat->term_idto
'category_name' => $cat->slughttp://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters
Wow, thank you… π
I has tried it, and it’s work for me, very good trick
But I still have a problem anymore.
On the front page showing all category titles, and postings which I have selected also appear in each category title that appears on the front page
Sorry if my question make you confused, i will tried to manipulate that code again. But if you can do it, maybe you can give me a one example again π
Thanks π
On the front page showing all category titles, and postings which I have selected also appear in each category title that appears on the front page
i am not too sure what that means – can you post a link to your site to illustrate this issue?
and maybe paste the full code of your template into a http://wordpress.pastebin.com/ and post the link to it here?
i’m not sure too with that my means π because i’m very confused how to tell about my problem, sorry π
My website that is still not online, I still build it on localhost
Firstly, here is example about my category on my localhost website -> http://bit.ly/htBacb
i tried to display the post from category -> BERITA (is category parent) on front page, but look like this : http://www.multiupload.com/M535TRREM4 (sorry if i has upload there)
and this the code of index.php : http://wordpress.pastebin.com/kEKxn1f7
the code is made to show 5 posts for each category in the site;
however, you seem to be trying to show just 5 posts of one category.
can you confirm that this is what you want to do?
I think he wants to display only posts from those 5 categories below Berita, i.e. Gadgets, Games, etc. If that is the case you should read http://codex.wordpress.org/Function_Reference/query_posts π
Nevertheless, I don’t think using query_posts multiple times like this is ideal. If you don’t mind the load (i.e. SQL queries) used by those queries then it’s ok.
Not like that…
This definition is just example :
i tried to display the post from category -> BERITA (is category parent) on front page
i just wants to display only post from this 3 Category Parent :
1st -> ARTIKEL
2nd -> BERITA
3rd -> DOWNLOADWhen I tried it, all the title and the post from each sub categories, appears too on the front page
thanks for making this clear;
to only show the parent categories’ posts, change this part of the code:
foreach ($cats as $cat) : $args = array( 'posts_per_page' => 5, 'category_name' => $cat->slug,); query_posts('category_name=berita'); if (have_posts()) :to this version:
foreach ($cats as $cat) : @h@ if($cat->category_parent) continue; //this line avoids to show posts of sub categories $args = array( 'posts_per_page' => 5, 'category_name' => $cat->slug,); @h@query_posts($args); // reset to original if (have_posts()) :explanation:
this partif($cat->category_parent) continue;checks if the category is a sub category, and then skips the loop.Woooww…. GREAT….SPECTACULAR
Your code is full working….
Thank you very much alchymyth
Now I will not bother again with that code π
and this is the conclusion of the code :
<?php wp_reset_query(); $cats = get_categories(''); foreach ($cats as $cat) : if($cat->category_parent) continue; //this line avoids to show posts of sub categories $args = array( 'posts_per_page' => 5, 'category_name' => $cat->slug,); query_posts($args); // reset to original if (have_posts()) : echo '<h2>Latest Posts in '.$cat->name.' Category</h2>'; ?> <?php while (have_posts()) : the_post(); ?> <div> <h2> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> </h2> </div> <?php if ( is_category($vidcat) ) { the_content(); } else { echo strip_tags(get_the_excerpt(), '<a><strong>'); } ?> <!-- this area is for the display of your posts the way you want it --> <!-- i.e. title, exerpt(), etc. --> <?php endwhile; ?> <?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>';?> <?php endif; wp_reset_query; ?> <?php endforeach; ?>Once again thank you very much alchymyth , you are so GREAT … π
The topic ‘Latest 5 posts from each category’ is closed to new replies.