<?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:
- How to get URL of current page displayed?
- Getting only direct child pages in WordPress with get_pages
- What is This esc_html_e() i wordpress php?
- Build a content and excerpt grid loop with paging and options for # of posts
- Specific Loop For 2 Within Each
- Two posts in same div – WP loop
- Store loop into array
- Looping through WP_Post Object
- Show Custom Taxonomy Title
- How to exclude specific category from the get_the_category(); array
- spliting posts into two columns
- How to speed up a wordpress function with multiple loops?
- How can I add pagination and how can I change thumbnail size?
- How to Display a Single Post Excerpt
- $wpdb->flush(); breaks the loop
- Use object in template part
- Get first URL from post content
- Changing layout with wp_customise
- Show css depending on activity type in BuddyPress activity-loop [closed]
- Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?
- Iterate through posts based on array of categories
- get author_name from queried post
- How to add condition in wordpress loop? [closed]
- Alert Bar section within WP loop is displaying even though there are no posts
- Checking array against author id in loop
- Load wordpress content in other domain
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- Checkbox doesn’t stay checked, conten
- Can we count the WordPress Loop
- Get post id within comments loop
- Syntax error when I try to insert my loop into an unordered list? [closed]
- Add content after the first post in WP Loop
- Custom Pagination is Broken
- How to overwrite orderby with add_query_var
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- Last class on last headline?
- 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)
- How to pass a variable to get_template_part that’s updated every time the template part is called?
- use loop to return blog details
- How to get current post category details inside “loop”?
- WP_Query multiple post results
- Convert Featured products loop into Recent products loop?
- How to add post meta in while loop?
- Echoing a CSS class based on category of post in a list
- Posts are not looping through correctly
- Limit tags shown in post
- How to pick “full/thumbnail” images in the loop?
- Create Div Wrapper On 3rd Iteration Or When Query Loop Items Run Out
- How to Fix an Archive.php That Displays All Posts?
- Php code error in wordpress if else statements [closed]
- pagination broken – clicking next displays “page not found”
- Search.php gets metadata from first post
- Add class to every other posts using get_posts
- Wp Query : Order by distance lat,lon
- How to Add a cutsom slug to my custom author role
- How do I get pagination for get_posts() in WordPress or do I have to rewrite the whole code?
- Give ID to divs inside the loop
- Display articles with a different template in the home page | Solved |
- How to output the taxonomies that are assigned to each custom post?
- Check if current post in loop is last on current page
- Looping through dummy posts and showing them in the main index.php
- Custom Loop.php having a loop inside a loop
- ACF – Get ID of relationship field in flexible content
- WordPress Ajax filter: Create two loops for different output styles?
- How to display sticky post always at the top (before regular post) in wordpress?
- adding or removing endforeach; throws error!
- Return to the beginning of the results of a for loop
- How to count posts posts if they are in a separate content page?
- Adding PHP to an if else loop
- ACF background-color per post in a WordPress loop
- Thumbnail wouldn’t scale to size properly
- How to sort (orderby) a query done by a template function before the ‘foreach’ loop?
- Filter loop by ‘price’ with meta_query
- Adding custom PHP to existing loop in Genesis
- How to Pass Current Taxonomy Terms into new WP_Query?
- Problems with the WordPress loop [closed]
- Add div after every 4 posts then every 2 posts for a responsive loop
- WordPress while loop with aphabetic counting like one, two, three
- Retrieving specific images from Media Library
- Add post class in custom loop (WP_QUERY)
- Loop returns more items than exist?
- Custom Taxonomy Link Text Echo Name Not Slug
- Post formating on Home page
- divide custom field values in div every two values
- Adding unique marketing messages between certain products whilst using isotope/Infinite Scroll
- I want my wordpress loop to only display posts of the standard format
- Automatically create a loop for post ID
- Pagination on Custom Loop
- why is this content-template not showing any of my blog-entries?
- the_post_thumbnail unless video id is added
- Trying to display ads on only a third of posts in a loop. Keep getting Error 500?
- How to call the_author_meta and make it work?
- WordPress post pagination on custom template not working
- Implement “No related posts for this entry” into the loop?
- Excerpt all post content Content Same Size without word cutting off
- Options.php loop won’t show!
- How do I display WooCommerce products in my query to rows of 3?
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress
- How do I use fields => ids in an array with WP Query?