WordPress.org

Ready to get started?Download WordPress

Forums

Latest post per category, from category 1 -8 on homepage (16 posts)

  1. netbuddy34
    Member
    Posted 5 years ago #

    Hi,

    Im trying to figure out how i could possibly have the latest post for each category listed on my homepage.

    I have say 8 boxes, for 8 differant categories.

    I would like to display the latest post for each of those categories in a specific order, not name or ID, i need to be able to set the order in a custom way.

    Is this possible without having a bunch of seperate loops? I could probably have 8 IF statements, but im wondering if there is something more compact?

  2. Georg Kevin Paquet
    Member
    Posted 5 years ago #

    You're using a WordPress Magazine style theme, right?

  3. tgiokdi
    Member
    Posted 5 years ago #

    You may want to do what I did on http://www.startrekbookclub.com with the query posts bit of code:

    '
    <?php query_posts('orderby=name&order=asc&cat=43&showposts=100'); ?>

    <?php while (have_posts()) : the_post(); ?>
    "><?php the_title(); ?>
    <?php endwhile;?>
    '
    This code will only show the titles of the posts, but you can use the content or the excerpt with it too to have some lead in text to the articles.

    I'm also using some code to show the children of a specific category:
    '
    <?php wp_list_categories('title_li=&orderby=name&order=asc&child_of=8'); ?>
    '

    my first time trying to past code into these forums so I apologize if this post ends up looking like carp. Which is a fish, just FYI.

  4. Dgold
    Member
    Posted 5 years ago #

    This page kind of tells you how to do it,
    http://codex.wordpress.org/Function_Reference/WP_Query

    Unfortunately the way it's written is kind of difficult to understand

  5. nando99
    Member
    Posted 5 years ago #

    just wanted to say that I tried what tgiokdi said and it worked GREAT!!

    thanks

  6. Dreamsinplay
    Member
    Posted 5 years ago #

    I am seeking to have my website updated ASAP, but due to the fact that I am a college student trying to start a non-profit organization I have a very limited budget.

    My current website is at http://www.dreamsinplay.org and uses WordPress.

    We are a very recently founded nonprofit organization which seeks to connect socially beneficial dreams with efficient and productive resources. In order to seek out the best “ideas”, we are seeking to enable our website to allow members to post ideas where other members will read, rate, and comment on them. With this complete, we will seek out the best ideas, and connect them to the necessary resources. I would like this part to replicate http://wordpress.org/extend/ideas with the titles of the post on the left and the ratings on the right. You will not be able to rate the idea from the listings, and must go to the ideas’ post to rate and comment.

    Currently, I have a wordpress plugin that allows registered members to submit posts. What I am looking now for is someone to help me create the ideas page with the titles on the left and the ratings on the right.

    I believe this should be a very quick and easy project, but haven’t been able to do it. I am very willing to do it myself, if someone could guide me. Again, I just need something that looks like this: http://wordpress.org/extend/ideas

    Furthermore, I would like to be able to do Idea Categories, such as weekly contest, monthly contest, etc. if someone has any suggestions about this.

    Thanks in advance for your time and consideration!

    Sincerely,
    Andrew CP Kelley

  7. c64glen
    Member
    Posted 5 years ago #

    I'm current having the same problem (so I hope you don't mind me tagging this along)

    The following bit of code display the latest post in six selected categories in my side bar. (excuse the code it's a little messy)

    <?php
    for ($i = 1; $i <= 6; $i++) {
    if ($i==1) {$oi_cat = "article";}
    if ($i==2) {$oi_cat = "music";}
    if ($i==3) {$oi_cat = "video";}
    if ($i==4) {$oi_cat = "podcasts";}
    if ($i==5) {$oi_cat = "blog";}
    if ($i==6) {$oi_cat = "links";}
    
    ?>
    <div class="spoiler clearfloat"> <IMG src="<?php bloginfo('stylesheet_directory'); ?>/images/t-<? echo $oi_cat;?>.png" alt="<? echo $oi_cat;?>" width="300" height="32" style="margin:0px; padding:0px;">
        <?php
    	 $query= 'category_name='.$oi_cat.'&showposts=1';
    	 query_posts($query);
    	?>
        <?php while (have_posts()) : the_post(); ?>
        <div class="clearfloat">
          <h4 class="title"><a href="<?php the_permalink() ?>">
            <?php the_title(); ?>
            </a></h4>
          <?
        $thumb = get_post_meta($post->ID, "thumb", true);
    if ($thumb != "")
    { ?>
          <img src="<?php $values = get_post_custom_values("thumb"); echo $values[0]; ?>"" alt="<?php the_title(); ?>" width="80" height="80" align="left" class="left" id="spoiler" />
          <?php }
      the_excerpt();  endwhile;?>
          <div class="right"><a href="/category/<? echo $oi_cat;?>/">More <? echo $oi_cat;?> &raquo;</a></div>
          <br />
        </div>
      </div>
      <?
    
      }
      ?>

    If I attempt to view my blog via the front page "/" or via a blog post "/blog post name/" I get a blank page. However if I view this on a category page e.g. "/category/article/" it works fine.

    any ideas why?

  8. davesgonebananas
    Member
    Posted 5 years ago #

    When you use query_posts in the sidebar you are overwriting the main query that contains the posts for the page you are on!

    I think you can use wp_reset_query() after the loop in the sidebar to get round this.

  9. c64glen
    Member
    Posted 5 years ago #

    Thanks, I actually fixed my problem (after many many hours) by replacing

    query_posts($query);
        while (have_posts()) : the_post();

    with

    `
    $recent = new WP_Query($query); while($recent->have_posts()) : $recent->the_post();
    `

  10. pesocky
    Member
    Posted 5 years ago #

    Thanks! I fixed same error.

  11. t31os
    Member
    Posted 5 years ago #

    Not sure if this be of use to any of you guys, but i made a function for looping through categories and posts...

    Accepts categories as an array...

    See here if of use..
    http://www.t31os.co.uk/wordpress-catx/

  12. digitalrenewal
    Member
    Posted 5 years ago #

    thank you tgiokdi!!! ive been trying to figure this out for 2 days. i knew it had to be simple, it built right into worpdress 2.8, but i couldnt get anyones code to work. :) im cartwheel happy!

  13. Rocksijen
    Member
    Posted 5 years ago #

    ohhhhhh !
    thanks! i am very very hapy!

  14. funkboybilly
    Member
    Posted 4 years ago #

    sorry, I posted wrong info...

  15. MAD000
    Member
    Posted 4 years ago #

    Hi all,
    I have been trying to find a simpler, more efficient way to display different posts depending on different conditions. For now I wrote the following script to retrieve posts for a specific category named 'Inspiration' and that should be displayed in descending order based on the number of comments (popularity). I would welcome any suggestions to shorten this (I feel there is another way!!)
    here is the code:

    <div class="inspirational-post-box"><!-- start -->
    <h1 class="heading-small">Inspirational Posts</h1>
    <?php $sql = "SELECT * "
    	. "FROM $wpdb->posts AS p "
    	. "WHERE p.post_status = 'publish' "
    	. "AND p.post_type = 'post' "
    	. "AND p.ID IN (SELECT tr.object_id "
    	. "FROM $wpdb->term_relationships tr "
    	. "WHERE tr.term_taxonomy_id IN (SELECT tt.term_taxonomy_id FROM wp_term_taxonomy AS tt "
    	. "INNER JOIN $wpdb->terms AS t ON t.term_id = tt.term_id "
    	. "WHERE (tt.term_id = (SELECT tt.term_taxonomy_id "
    	. "FROM $wpdb->term_taxonomy AS tt "
    	. "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id "
    	. "WHERE tt.taxonomy = 'category' AND t.name = 'Inspiration') OR tt.parent = (SELECT tt.term_taxonomy_id "
    	. "FROM $wpdb->term_taxonomy AS tt "
    	. "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id "
    	. "WHERE tt.taxonomy = 'category' AND t.name = 'Inspiration')))) "
    	. "ORDER BY p.comment_count DESC LIMIT 12";
    $result = $wpdb->get_results($sql);
    foreach ($result as $post) {
    setup_postdata($post);
    ?>
    <div class="entry-small">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img class="thumbnail-small" src="<?php bloginfo('template_url') ?>/images/<?php echo get_post_meta($post->ID, 'post-thumbnail', true); ?>" width="50" height="50" alt="Small Thumbnail" /></a>
    <h2 class="title-small">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    </h2>
    </div>
    <?php } ?>
    </div><!-- end -->

    [signature moderated Please read the Forum Rules]

  16. kirmann
    Member
    Posted 4 years ago #

    continue of the topic.

    case : we wanna split the first post (one category) with the second post or next post. And the next post is only two post.

    question : How the query to do it ?

    Thanks, before

Topic Closed

This topic has been closed to new replies.

About this Topic