Forums

[resolved] Latest 5 posts from each category (20 posts)

  1. soliya
    Member
    Posted 2 years ago #

    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!

  2. soliya
    Member
    Posted 2 years ago #

    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/

  3. soliya
    Member
    Posted 2 years ago #

    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.

  4. alchymyth
    The Sweeper
    Posted 2 years ago #

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

  5. soliya
    Member
    Posted 2 years ago #

    This is great, thanks alchymyth :-). I'll give it a try and I'll get back to you.

  6. nandla
    Member
    Posted 2 years ago #

    this doesn't work for me. it only shows last category's posts at jobs.pkfunda.com main page

  7. alchymyth
    The Sweeper
    Posted 2 years ago #

    @nandla
    please start a new topic and describe your problems there.

  8. renofizaldy
    Member
    Posted 1 year ago #

    @alchymyth :

    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?

  9. alchymyth
    The Sweeper
    Posted 1 year ago #

    try and change this line in the query_posts() $args:

    'cat' => $cat->term_id

    to

    'category_name' => $cat->slug

    http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters

  10. renofizaldy
    Member
    Posted 1 year ago #

    @alchymyth :

    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 :D

    Thanks ;)

  11. alchymyth
    The Sweeper
    Posted 1 year ago #

    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?

  12. renofizaldy
    Member
    Posted 1 year ago #

    i'm not sure too with that my means :( because i'm very confused how to tell about my problem, sorry :D

    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

  13. alchymyth
    The Sweeper
    Posted 1 year ago #

    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?

  14. OddOneOut
    Member
    Posted 1 year ago #

    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.

  15. renofizaldy
    Member
    Posted 1 year ago #

    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 -> DOWNLOAD

    When I tried it, all the title and the post from each sub categories, appears too on the front page

  16. alchymyth
    The Sweeper
    Posted 1 year ago #

    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 part if($cat->category_parent) continue; checks if the category is a sub category, and then skips the loop.

  17. renofizaldy
    Member
    Posted 1 year ago #

    @alchymyth

    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 ... ;)

  18. peshawa
    Member
    Posted 1 year ago #

    in who php file i have to add that cod?

  19. KipV
    Member
    Posted 1 year ago #

    @ Alchymyth

    AS you helped Soliya first, i have the same need, but only want my listings to be on one page and only in my podcast sidebar for podcast categories. you can see my other post for details. Would your code work for that and if so, where do i put it? im using infocus with tsg podcast plugin.

    thanks for helping so many of us!

  20. renofizaldy
    Member
    Posted 1 year ago #

    @peshawa

    just put that code inside index.php

Topic Closed

This topic has been closed to new replies.

About this Topic