Forums

Get Newest post for Category (8 posts)

  1. MACscr
    Member
    Posted 2 years ago #

    Im kind of making my own sidebar and I have a unique design for each category i list (not all) and i would like to somehow retrieve the newsest post from that category. I havent been able to find any easy way to do this without just querying the db directly.

    To make things a little more complicated. I have a category with subcategories and i want the newest post no matter what subcategory its in. I still want to list its subcategory name though.

    Any ideas/suggestions? Ive only been using WP for 2 days now, so I have a lot to learn.

  2. richsipe
    Member
    Posted 2 years ago #

    Here is the code to use. Just make sure you put your actual category number instead of CATNUMBER in the code below. You can find your category ID in the category section of the admin.

    <?php $posts = get_posts( "category=CATNUMBER&numberposts=1" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    
    <div class="post">
    DO YOUR THING HERE
    </div>
    
    <?php endforeach; ?>
    <?php endif; ?>

    If you use the main category it will automatically include all of the sub categories. Let me know if you have any issues.

  3. MACscr
    Member
    Posted 2 years ago #

    sweet, thank you so much!! Will give it a try right away.

  4. MACscr
    Member
    Posted 2 years ago #

    Works perfect except for the issue with subcategories. I have a category called "Character Blogs", then each Character has his own subcategory, like "Jason Smith" or whatever. The problem is that i want to list a new post that falls under "Character", no matter what subcategory its in. Any ideas on this? I really appreciated your initial help as well.

  5. MACscr
    Member
    Posted 2 years ago #

    shoot, looks like i cant create permalinks from those results as well. Hmmm.

  6. MACscr
    Member
    Posted 2 years ago #

    ok, this is dirty, but its gets a list of child category ids and then comma seperates them so they can be used in the above function.

    function list_childcat_ids($id) {
    
    	$query = "SELECT cat_ID FROM wp_categories WHERE category_parent=".$id.""; 
    
    	$result = mysql_query($query) or die(mysql_error());
    
    	while ($array = mysql_fetch_array($result))	{
    		$data[] = $array[0];
    	}	
    
    	$cat_id_list = implode(",", $data);
    
    	return $cat_id_list;
    }

    Now i just have to figure out how to get the proper urls.

  7. MACscr
    Member
    Posted 2 years ago #

    ok, got everything working now. Here example code for what i used to display the newest post from any subcategory from a particular parent category. (lol, if that makes sense)

    <?php $catid = list_childcat_ids('3'); ?>
    <?php $posts = get_posts( "category=".$catid."&numberposts=1" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    
    <div class="post">
    <a href="<?php echo get_permalink($post->ID); ?>" ><?php echo $post->post_title; ?></a>
    </div>
    
    <?php endforeach; ?>
    <?php endif; ?>
  8. vkaryl
    Member
    Posted 2 years ago #

    Wow.... you learned a lot fast! Good job!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.