WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Publishing certain number of posts under same category? (5 posts)

  1. arturocivit
    Member
    Posted 1 year ago #

    Hi there, one question, I have this HTML code in my document:

    <div id="recent-news">
       		<h2>Recent News</h2>
    		<div id="rbox1">
    			<p class="date">October 28, 2012</p>
    			<p class="link"><a href="#">In posuere eleifend odio quisque semper augue mattis wisi maecenas ligula.</a></p>
    			<p> Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. </p>
    			<p class="button-style1"><a href="#">Read More</a></p>
    		</div>
    
            <div id="rbox2">
    			<p class="date">October 23, 2012</p>
    			<p><a href="#">Pellentesque viverra vulputate enim. Aliquam erat volutpat convallis dictum. </a></p>
    			<p>Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio.</p>
    			<p class="button-style1"><a href="#">Read More</a></p>
    		</div>
    
    		<div id="rbox3">
    			<p class="date">October 20, 2012</p>
    			<p><a href="#">Suspendisse dictum porta lectus. Donec placerat odio vel elit. Nullam ante orci.</a></p>
    			<p>Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. </p>
    			<p class="button-style1"><a href="#">Read More</a></p>
    		</div>
    
    		<div id="rbox4">
    			<p class="date">October 18, 2012</p>
    			<p><a href="#">Donec placerat odio vel elit. Nullam ante orci, pellentesque eget, tempus quis.</a></p>
    			<p>In posuere eleifend odio quisque semper augue mattis wisi maecenas ligula. Pellentesque viverra vulputate enim.</p>
    			<p class="button-style1"><a href="#">Read More</a></p>
    		</div>
         </div>

    As you can see, there's a main div that holds 4 boxes of news, the question is, I want to have there the last 4 entries of the news category, how can I achieve this?

    Thanks in advance!

    Arturo

  2. Jay Hoffmann
    Member
    Posted 1 year ago #

    You'll have to set up a get_posts function instead of hard-coding the HTML. This will dynamically load your latest posts. You will need to make sure you include the excerpt and date as well.

    Yours would end up looking something like this (though probably not this exactly):

    <?php
    global $post;
    $args = array( 'numberposts' => 4, 'category' => NEWSCATIDNUMBER );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<div id="rbox">
    <p class="date"><?php the_date(); ?></p>
    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    <p><?php the_excerpt(); ?></p>
    </div>
    <?php endforeach; ?>

    Make sure to fill in NEWSCATIDNUMBER with the ID for your News category.

    A good place to start is to read up on the get_posts function in the Codex and see where that takes you.
    http://codex.wordpress.org/Template_Tags/get_posts

    Another option is to get a plugin like Special Recent Posts to do that heavy lifting for you.
    http://wordpress.org/extend/plugins/special-recent-posts/

  3. arturocivit
    Member
    Posted 1 year ago #

    Thanks so much for your help jhoffm34, I did it but the thing is, how to make it appear side by side like the div boxes are place din the HTML code I have in my first post, so far is working but not exactly like they should be. Thank you so much!

  4. Jay Hoffmann
    Member
    Posted 1 year ago #

    You have to use CSS to float the rbox id to the left. In your CSS stylesheet, add:

    .rbox {
    float: left;
    }

    In addition to any other elements you already have. You might have to tinker a little bit with it, but that's a good place to start.

  5. arturocivit
    Member
    Posted 1 year ago #

    jhoffm34 thanks so much, that solved the problem and helped me a lot to understand how certain things works on WP, gracias!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.