The $current_post
property of the WP_Query
object for the Loop should tell you which post you are on. Something like this should work:
<?php $pclass = ($wp_query->current_post === 1) ? 'middle-post-class' : 'first-last-class' ; ?>
<div id="post-style" <?php post_class($pclass); ?> >
<h1 class="title"><a href="https://wordpress.stackexchange.com/questions/87942/<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
</div>
The built in Loop counter– $current_post
— starts from 0. That is why the second post is 1, and not 2.
Caveats: Your theme may already be using post_class
somewhere else. You may not want to use it a second time. And if this is a custom WP_Query
Loop the name of the object may be different– that is, $my_query
instead of wp_query
, for example.
Related Posts:
- Is it possible to bold certain words in a post title?
- Is this the best (and only) option to style CSS for a given post type?
- adding a custom css class to post
- Custom colors for post rows based on post meta value
- How To Add Perfect Apostrophe And Quotation Mark In WordPress Posts?
- Hide add new page button
- Limit function to specific post category
- How To Keep Posts from Being Displayed Two or More Times on My Homepage? [closed]
- Latest 5 posts (custom post type), each post different CSS
- WordPress Theory Lesson – Image Columns within posts with alignment?
- How to style the last post on the front page different than the others?
- Add a class to post title if a link ends with a certain extension
- Copying and pasting emoji in a wordpress post
- Style Differently Edited Posts
- How to make classes for posts?
- CSS: How can I show a photo beyond the template’s column, but contained within the browser window? [closed]
- Making a custom Pagination for multi page blog post
- Can I search posts and pages for css classes?
- How to add tags (custom taxonomy) to post class css?
- Is there a way to specify an extra class for a post or page with no title?
- How to remove in-line style of WordPress’s post content only?
- Grid post page on WordPress with Bootstrap, the_excerpt(); Problem
- Every second post different class in blog view
- Make a vertical dynamic list of posts in alphabetically order and in columns [closed]
- How to make different custom post layouts?
- Font shows up as Arial instead of Times [closed]
- How to style archive post titles… but only those posts who have comments?
- My Custom Post Type Still Using index.php
- How can I add a class to particular paragraphs in my post?
- add div after image in post [closed]
- How do I switch css class on post
- displaying the categories post
- How to separate each individual blog post?
- Locally change the font family [closed]
- YARPP php question
- Right align a youtube video
- How do I apply a template to my single posts?
- Indenting within a blockquote
- Different background colors for posts with WordPress
- Turn off display for custom sidebar post widget when on the post it’s displaying
- How to allow arbitrary inline CSS in posts?
- How to highlight or show new to the particular posts list
- Css and function issue on author archive page
- WordPress Recent Post with the badge “New” on the side of the title post
- Centre Blog Index (Posts Page) on Home.php
- I have two post types in the same query, how can I use CSS to select only one post type?
- how can i fix my display post in my index.php
- display content in multiple columns
- How can I alternate styling of images in a post? [closed]
- Hide title in Merlin Theme on posts and pages only
- How to add a class to links inside posts text
- Background image in posts
- How can i put image to come first than the title?
- Automatically add class to link based on link text
- Change a div background color on Post ID
- CSS – Change height of Storefront / WooCommerce product categories, but not products
- How to display my categories in a list (to a post, not the sidebar)?
- Difficulty hiding a span per a body class within WordPress
- posts blocks don’t show up correctly
- Get WordPress post content by post id
- Get page ID of page that is set as the posts page
- Get Posts Under Custom Taxonomy
- How to limit the number of posts that WP_Query gets?
- Stop WordPress Wrapping Images In A “P” Tag
- When is the ‘post_content_filtered’ column in database cleared by WordPress?
- Turn a URL into an Attachment / Post ID
- GET the excerpt by ID
- Stop WordPress automatically adding tags to post content
- Determine if page is the Posts Page
- How to get ‘post_content’ without stripping tags?
- How to add an admin notice upon post save/update
- Print Current Post Index number within Loop
- Should we trust the post globals?
- Optimize post insert and delete for bulk operations?
- Get the Current Page Number
- How do you add thumbnail support for custom post types?
- How to mark every 3rd post
- Change Posts per page count
- Keep featured content post in homepage with original order
- How to display related posts from same category?
- Can posts have parents?
- Is there a action hook for the “Empty Trash” button?
- How to end the excerpt with a sentence rather than a word?
- How do I check if I linked to a post before I delete it?
- How to change the case of all post titles to “Title Case”
- Appending content with broken in 4.4
- How to get posts published between a date and today?
- ZIP up all images displayed in a [gallery] and offer as download link
- How to detect single.php (but not single-portfolio.php)?
- How to check if post meta key exists or not in wordpress database
- How to remove a column from the Posts page
- removing inline styles from wp-caption div
- What is the use of to_ping and pinged column?
- How to use “Add link” pop up for a WordPress widget
- Remove Categories / Tags From Admin Menu
- How to retrieve text only from wp_content() not from wp_excerpt()?
- Stop WP from creating “Sample Page” and “Hello World!” post
- Where is the old post permalink slug stored?
- How to Create Editable Blocks of Text for the Homepage?
- Keyboard shortcut for updating a page or post?