0

I've finally figured out how the post loop works (very new to coding), and I'm trying to figure out the best way to add a class to individual posts I'm displaying (title, author, etc when I add all of them) in the following code:

    $args = array( 'numberposts' => '5' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
            echo $recent["the_post_thumbnail"];
        echo '<ul><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </ul> ';?>
        <ul><?php echo $recent["post_excerpt"];?></ul>
        <?php 
        }
        wp_reset_query();
    ?>

    <?php 

2 Answers 2

2

Not totally clear what you're asking, but post_class is probably your best bet.

<?php post_class(); ?>

More info: https://codex.wordpress.org/Function_Reference/post_class

1
  • I added an array that would work with other meta data, so it's slightly different, but the question remains the same. Basically, it's a home page, and the code is for an element that shows the most recent posts. I'm wanting to wrap each post (like title, image, etc) in a div, rather than the whole section in one. So something kind of like this: geektripp.com If you look at the posts under the section "New" you will see how each post has it's own little area.
    – geektripp
    Dec 28, 2016 at 5:36
0

If you are showing list of latest post then here is the easier way of it in which you can add classes and DIV as per your wish.

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => 'post',    //Post type that you want to show
        'posts_per_page' => 5,  //No. of Pages to show          
        'offset' => 0,   //excluding the latest post if any
        'paged' => $paged  //For Pagination
    );

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();?>
    <?php /*** Title of Post ***/ ?>
    <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title();?></a></h3>
    <?php /*** Title of Post ends ***/ ?>
    <?php 
    /*****     Thumbnail     ******/
    the_post_thumbnail(
        array(120, 90), 
        array(

        'class' => 'enter_class',  //custom class for post thumbnail if any 
            'alt' => 'post thumbnail', //post thumbnail alternate title
        'title' => 'my custom title'   //Title of thumbnail
        )
    );
/*******     Thumbnail Ends   ********/
/***  Post contents/Description  ***/
the_excerpt();
/***  Post contents/Description ends  ***/
?>  
<?php      
endwhile;

UPDATE

If you want to add div on individual post then you need to add div inside while loop.

If you want to wrap your every post in individual div then add div in while loop in above mentioned code like this

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="enter_class_name">
 <?php
 //Rest of the code in while loop like title,description and thumbnail code will come here.
 ?>

</div>

NOTE Make sure you don't add DIV inside php tags (<?php ?>). Specify your DIV outside php tags.

And you can also add DIV in parts of post also. For example if you want to add DIV to wrap thumbnail of every posta individually then you can also do that easily.

To do that you have to read all comments in code to understand what code is using to show thumbnail of post and simply wrap that code inside DIV. And your post thumbnail will be enclosed in separate DIV.

In my given code this piece of code inside while loop is responsible for showing post thumbnail

<?php 
/*****     Thumbnail     ******/
the_post_thumbnail(
    array(120, 90), 
    array(

    'class' => 'enter_class',  //custom class for post thumbnail if any 
        'alt' => 'post thumbnail', //post thumbnail alternate title
    'title' => 'my custom title'   //Title of thumbnail
    )
);
/*******     Thumbnail Ends   ********/

And to wrap thumbnail inside div, just wrap this code inside div(outside php tags) like this

<div class="enter_class_name">
    <?php 
    /*****     Thumbnail     ******/
    the_post_thumbnail(
        array(120, 90), 
        array(

          'class' => 'enter_class',  //custom class for post thumbnail if any 
          'alt' => 'post thumbnail', //post thumbnail alternate title
         'title' => 'my custom title'   //Title of thumbnail
        )
    );
    /*******     Thumbnail Ends   ********/
    ?>
</div>

Like this you can also wrap your Title of post or Description of post in separate DIV just by enclosing their respective code in DIV.

BUT make sure to put all DIVs only inside while loop. Only then your posts will get separate DIV.

And it is strongly recommended that you read all comments line attach to the code carefully to understand every line of code is performing what kind of action/activity. This info will help you to add DIV as per your need.

7
  • I think I'm a little confused. I didn't want to add pagination. I essentially wanted to be able to wrap each post. For example something kind of like this: geektripp.com If you look at the posts under the section "New" you will see how each post has it's own little area.
    – geektripp
    Dec 28, 2016 at 21:58
  • Don't get me wrong. Thank you very much for the code! It's awesome actually. Like it a lot better than what I did have.
    – geektripp
    Dec 28, 2016 at 22:01
  • WAIT! I see now. It wrapped the first post, but it doesn't wrap each post. Still, sooo very close. I just wish I could understand the code better to fix it myself. :(
    – geektripp
    Dec 29, 2016 at 0:10
  • @geektripp I am little busy but will back to you soon and help u our :)
    – Rishabh
    Dec 29, 2016 at 6:00
  • @geektripp Just a piece of advice until I get back to you, Put your div inside while loop only (not outside while loop) else you won't get individual div on every single post. This is start of while loop (while ( $loop->have_posts() ) : $loop->the_post();?>) and Last line if my code is end of while loop
    – Rishabh
    Dec 29, 2016 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.