WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent Catgory Posts (11 posts)

  1. joshrodgers
    Member
    Posted 3 years ago #

    I am using a query to return posts from a specific category, what I want to do is display a div if it is the latest 3 posts of a category - is this possible? Thanks,

    Josh

  2. Digital Raindrops
    Member
    Posted 3 years ago #

    Query Posts does not accept the number of posts, but you could pass in 'posts_per_page'

    I prefer to use get posts and rewind after, if I had a page called news and a category called news I could have a block of text in a page template, with 3 related posts under!

    I use a page template like this example oldest post first in a series.

    Untested for syntax:

    <?php
    $pageslug = $post->post_name;
    $args = array(
    	'category_name' => $pageslug,
             'order' => 'ASC',
    	'numberposts' => 3
    );
    
    if ( get_posts($args) ) {
    	get_template_part( 'loop', 'category' );
    }
    rewind_posts();
    ?>

    HTH

    David

  3. joshrodgers
    Member
    Posted 3 years ago #

    David,

    Thank you for your response. I'm not sure where to put this...does it go outside the loop like a query, or inside?

    The order is random, which means that I couldn't use a simple counter to accomplish what I need.

    I got really close with:

    <?php if ($post == $posts[0] || $post == $posts[1] || $post == $posts[2]) { ?>

    Which works, but that filters all the posts.

    I'd like it to be for a single category.

    Something like:

    <?php if (in_category("logos") && $post == $posts[0] || in_category("logos") && $post == $posts[1] || in_category("logos") && $post == $posts[2]) { ?>

    but that doesn't work :(

    Any ideas? Thanks,
    Josh

  4. Digital Raindrops
    Member
    Posted 3 years ago #

    Ok do you mean one category with many posts, and you want the latest three posts first and in a different div style?

    You could likley do that easy with a counter but I do not understand the 'random' part.

    Post the whole page or loop code up to http://pastebin.com and paste the link in a reply.

    That will get a quicker answer and stop playing 'reply ping-pong'

    David

  5. joshrodgers
    Member
    Posted 3 years ago #

    David,

    Thank you for your reply.

    I have a category called logos, that I have set to be displayed randomly. What I am looking for is for the latest three posts in the category logos to display a div (basically I am putting a "new" image on top of it).

    The current code works, but it is filtering all the posts, not just the posts in the logo category (if that makes sense). I apologize for the confusion and appreciate the help!! You can find what I have at: http://pastebin.com/B69wLAgv

    Thanks,
    Josh

  6. Digital Raindrops
    Member
    Posted 3 years ago #

    The code looks ok it might just be the order of the arguments.

    The random array returned would not be in latest ID order, $post[0] may not be the latest post in a random array, so the code in the link below, does two queries the first to get the three latest posts, then the random query.

    In the loop look to see if the ID is in the latest posts array.

    I have not tested this it is just mocked up, so you need to check the calls and syntax, and test loacal.
    http://pastebin.com/zxE5JVrb

    HTH

    David

  7. joshrodgers
    Member
    Posted 3 years ago #

    David,

    Thank you so much for the code. I pasted it exactly as it was shown and it didn't do anything. Everything displays correctly, the gallery, the site, but the code doesn't display the div that I need :(

    Any ideas? Thanks,
    Josh

  8. Digital Raindrops
    Member
    Posted 3 years ago #

    Ok,
    I turned the code into a twenty ten child theme and template page:

    The Theme:
    Create a twenty ten child theme folder add three files:

    style.css

    /*
    Theme Name: Twenty Ten logos
    Template: twentyten
    Version: 1.1
    License: GNU General Public License v2.0
    License URI: http://www.gnu.org/licenses/gpl-2.0.html
    This theme, like WordPress, is licensed under the GPL.
    */
    
    @import url('../twentyten/style.css');
    
    /* Add the image div styles here */
    .new {
    
    }

    New file page-logos.php
    http://pastebin.com/vbBb5tUC

    New file loop-logos.php
    http://pastebin.com/56uwSDSd

    Use a local testing environment and activate the child theme add some posts to category 'logos' with feature images, create a new page and apply the template

    To test with another category in loop-logos.php change category_name
    $args = array( 'category_name' => 'here', 'numberposts' => 3 );

    <?php
    $args = array(
    	'category_name' => 'here',
    	'posts_per_page'     => 24,
    	'orderby'    => 'rand'
    );
    ?>

    HTH

    David

  9. joshrodgers
    Member
    Posted 3 years ago #

    David,

    Thank you so much!! Everything works without problems or issues! You're the bomb,

    Josh

  10. Digital Raindrops
    Member
    Posted 3 years ago #

    No problem, just to complete a topic, others might like a similar solution.

    What was the stylesheet styles you used for the classes .thumb and .new

    We can see that the three latest posts also have the spacer.png, does that highlight the three images somehow as most recent?

    Mark the topic as resolved please :)

    David

  11. joshrodgers
    Member
    Posted 3 years ago #

    David,

    Here are the styles:

    .thumb {
    	border: 1px solid #ddd;
    	margin: 20px 20px 0 0;
    	position: relative;
    	width: 100px;
    	/* Round Corners */
    	-khtml-border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	border-radius: 5px;
    }
    
    .thumb img {
    	/* Round Corners */
    	-khtml-border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	border-radius: 5px;
    }
    
    .new a img {
    	background: transparent url("images/new.png") no-repeat top right;
    	height: 100px;
    	position: absolute;
    	right: -3px;
    	top: -3px;
    	width: 100px;
    	z-index: 80;
    	/* Round Corners */
    	-khtml-border-radius: 5px;
    	-moz-border-radius: 5px;
    	-webkit-border-radius: 5px;
    	border-radius: 5px;
    }

    The spacer is just covering the image :) I am using it purely for css purposes. You can see a screenshot here: http://www.box.net/shared/4100kta1jf

    Thanks,
    Josh

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.