Title: Add html code before first post when using WP Page Numbers
Last modified: August 19, 2016

---

# Add html code before first post when using WP Page Numbers

 *  Resolved [dchacons](https://wordpress.org/support/users/dchacons/)
 * (@dchacons)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/add-html-code-before-first-post-when-using-wp-page-numbers/)
 * Hi, I have a question
 * My index page shows all my post, shows 2 post per page, shows first the most 
   recent post. I used the WP Page Numbers plug-in for pagination, and works great.
 * But I need to do this, before the first index page post appears in page 1 (no
   matter the post category) I need to add some html code before the las post, so
   it have to look like this:
 * post page 1
    //some html code post 1 post 2
 * post page 2
    //empty post 1 post 2
 * post page 3
    //empty post 1 post 2
 * I´m new in WP, I have used it for 3 days (great by the way) I know that I have
   to add some code in the index.php between the loop of posts, I have like 15 posts
   but I used the function $wp_query->post_count() and it returns only 2 posts (
   posts per page and not the total).
    So basically what i need is a function that
   return me the total post (no matter the category) and having that I can add a
   counter and do something lige this while… if is_home() and if $count+1 == total{//
   add html code} show post

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [dchacons](https://wordpress.org/support/users/dchacons/)
 * (@dchacons)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/add-html-code-before-first-post-when-using-wp-page-numbers/#post-1846531)
 * Ok I get all post number, in this case 15 as I want, but the pboblem is that 
   as I display 2 for page and use pagination, every time I clic on a page number
   link the $counter stars again in 1 so the $counter+1 is never equal to 15 as 
   I want to know if is the last post in my index, this is the code, again the question
   is how to know that a post is the last one so I can put some html code before
   it?
 *     ```
       <?php
   
       get_header();
       if (have_posts())
       {
       $counter= 1;
       $post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ;"));
   
       echo '<p>total POSTS: ' . $post_count . '</p>';
   
         while (have_posts())
         {
       	echo '<p>Counter: ' . $counter. '</p>';
       	if($counter+1 == $post_count){?>
   
                //....HTML FOR FIRST POST
   
           <?php
           }else{
           	$contador++;
           }
       	art_post();
         }
         art_page_navi();
         if(function_exists('wp_page_numbers')) { wp_page_numbers(); }
   
       } else {
         art_not_found_msg();
       }
       get_footer();
       ```
   
 *  Thread Starter [dchacons](https://wordpress.org/support/users/dchacons/)
 * (@dchacons)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/add-html-code-before-first-post-when-using-wp-page-numbers/#post-1846542)
 * Ok I made it, wp rules 🙂 I select the ID of the last post (last date) and print
   the html only if the actual post id is the same as the last post id
 *     ```
       $post_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'
       	ORDER BY post_date DESC LIMIT 0 , 1;"));
   
         while (have_posts())
         {
   
         	if($post->ID == $post_id){?>
                 //add html code
       ...
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Add html code before first post when using WP Page Numbers’ is closed
to new replies.

## Tags

 * [First post](https://wordpress.org/support/topic-tag/first-post/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [dchacons](https://wordpress.org/support/users/dchacons/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/add-html-code-before-first-post-when-using-wp-page-numbers/#post-1846542)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
