<?php $class=""; // set your class to nothing outside the loop ?>
<?php if (have_posts()) : while ( have_posts()) : the_post(); ?>
<?php $class = ('first-container' == $class) ? 'second-container' : 'first-container'; // alternate classes ?>
<div <?php post_class('clearfix') ?> id="post-<?php the_ID(); ?>">
<h2><a href="https://wordpress.stackexchange.com/questions/77704/<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div class="<?php echo $class ?>">
If $class
equals “first-container” set it to “second-container” else set it to “first-container”. Because we set $container
to nothing initially, the first iteration will set $class
to “first-container”, after which you alternate.
Related Posts:
- Getting only direct child pages in WordPress with get_pages
- Query multiple custom post types in single loop
- Inject post (from specific category) between posts in Loop
- Knowing the total number of posts before to get into the loop
- Build a content and excerpt grid loop with paging and options for # of posts
- Installing wp3.2.1 on IIS; getting empty sessions
- How to loop over custom fields in a page template?
- Want to separate sections of posts. Can you restart loop?
- Alter secondary loop to exclude posts from current page category
- Specific Loop For 2 Within Each
- Two posts in same div – WP loop
- Infinite-Scroll Plugin and Jetpack Infinite Scroll Plugin – Adding to “Thoughts” Theme
- Woocommerce, recognize the loop of related products
- Easiest way to show total number of subpages
- Custom Loop through category menu to include sub categories
- Output 2 items within the Loop
- How to speed up a wordpress function with multiple loops?
- Custom excerpt function re-factoring
- How can I add pagination and how can I change thumbnail size?
- $wpdb->flush(); breaks the loop
- Use object in template part
- Get first URL from post content
- Dynamic Stylesheet loads but doesn’t finish
- Filter Select results based on selection
- Show css depending on activity type in BuddyPress activity-loop [closed]
- Iterate through posts based on array of categories
- Increase offset while looping
- How to add condition in wordpress loop? [closed]
- Alert Bar section within WP loop is displaying even though there are no posts
- remove post that has no content
- Checking array against author id in loop
- How do I Turn This Into An if statement?
- Load wordpress content in other domain
- Widgets in the loop if active
- Get post id within comments loop
- How to get post ID in a Page?
- PHP Warning: strip_tags() expects parameter 1 to be string?
- Echo a shortcode div after every 3 posts
- How to avoid duplicates when creating recent network posts
- variable value disappears in a second loop
- get_the_ID() in the footer returns wrong value
- Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
- Why my loop isn’t working? [closed]
- Only show first image in foreach loop
- How to get current post category details inside “loop”?
- How to pass current loop data into nested loop
- Get all posts as an array ID => Name
- How to get post ID from button and pass title to Modal
- WP_Query multiple post results
- Convert Featured products loop into Recent products loop?
- Wpdb get->results to out the the month from the db
- Display child page content of a specific parent on home page
- Do not duplicate posts with multiple categories in multiple loops
- Infinite Loop – WP_Query
- How to wrap every 3 posts in a div (and close the last div too) [closed]
- Dividing the loop to style post differently
- WordPress loop is not working
- How to create Loop code for Menu using WordPress?
- Problem in replacing the_content with the_excerpt()
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- How to Add a cutsom slug to my custom author role
- Give ID to divs inside the loop
- Pagination contents not changing in Front page WordPress
- How to output the taxonomies that are assigned to each custom post?
- Display all categories including sub categories under a list ul
- Where does php code to load data go?
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- Custom Loop.php having a loop inside a loop
- Latest posts feed with a specific post always first
- How to output values from a loop into a javascript array
- Exclude category and post from loop in custom category.php
- How to sort (orderby) a query done by a template function before the ‘foreach’ loop?
- Making list of posts with chosen description
- How fix error in the WordPress loop?
- what’s the syntax for if the image exists get the image?
- Add div after every 4 posts then every 2 posts for a responsive loop
- how to remove replicating a tag inside loop?
- My query keeps looping infinitely ! how to stop it?
- Efficient way of querying for a “fallback” post?
- Loop 1 user randomly
- Create a hierarchical loop at predefined markup requirements
- enumerating custom taxonomies?
- Problems With Query and/or Template Part and/or PHP
- Two loops in one function
- How to display thumbnail if post is assigned one otherwise not
- Pagenav Not appearing on custom Template
- Error trying import one category on page
- the_post_thumbnail unless video id is added
- Remove the_content From Loop
- Adding if statement to content for homepage
- Tables not showing divs and loop/php items
- WordPress post pagination on custom template not working
- Pagination in category.php not functioning
- For each loop will not append to the_content hook
- How to handle parent and child pages?
- How do I remove blog entries from home page Twenty Eleven child theme?
- How to create a loop that will display one post and stop?
- Pagination not working properly
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- Saving meta box data from selected option’s value in database is not working