I’m working on a modified wordpress loop where in it has an if and else condition inside. The code is down below.
What I’m trying to do is add a container that will hold every post that meets the condition. This will group each condition.
<div class="container">
<?php if (have_posts()) : ?>
<?php
$i = 1;
while (have_posts()) {
the_post();
// Add a DIV CONTAINER that holds the FIRST CONDITION
if ($i <= 3) {
the_title();
the_content();
// Add a DIV CONTAINER that holds the FIRST CONDITION
// Add a DIV CONTAINER that holds the SECOND CONDITION
} elseif ($i <= 9) {
the_title();
the_permalink();
// Add a DIV CONTAINER that holds the SECOND CONDITION
// Add a DIV CONTAINER that holds the THIRD CONDITION
} elseif ($i <= 13) {
the_title();
the_permalink();
// Add a DIV CONTAINER that holds the THIRD CONDITION
// Add a DIV CONTAINER that holds the FOURTH CONDITION
} elseif ($i <= 15) {
the_title();
// Add a DIV CONTAINER that holds the FOURTH CONDITION
// Add a DIV CONTAINER that holds the FIFTH CONDITION
} else {
the_title();
// Add a DIV CONTAINER that holds the FIFTH CONDITION
}
$i++;
}
?>
<?php else : ?>
<h2>No Posts Found</h2>
<?php endif; ?>
</div>
If I add an echo '<div class="FirstCondition">';
where my comments are, this is what happens.
It only repeats the div container that I added. What I need is to add a simple DIV Container that HOLDS all posts that meet the criteria.
The final output would be like this:
<div class="FirstCondition">
WordPress Published Post that meets the criteria for the First Condition
WordPress Published Post that meets the criteria for the First Condition
WordPress Published Post that meets the criteria for the First Condition
WordPress Published Post that meets the criteria for the First Condition
</div>
<div class="SecondCondition">
WordPress Published Post that meets the criteria for the Second Condition
WordPress Published Post that meets the criteria for the Second Condition
WordPress Published Post that meets the criteria for the Second Condition
WordPress Published Post that meets the criteria for the Second Condition
</div>
<div class="ThirdCondition">
WordPress Published Post that meets the criteria for the Third Condition
WordPress Published Post that meets the criteria for the Third Condition
WordPress Published Post that meets the criteria for the Third Condition
WordPress Published Post that meets the criteria for the Third Condition
</div>
<div class="FourthCondition">
WordPress Published Post that meets the criteria for the Fourth Condition
WordPress Published Post that meets the criteria for the Fourth Condition
WordPress Published Post that meets the criteria for the Fourth Condition
WordPress Published Post that meets the criteria for the Fourth Condition
</div>
<div class="FifthCondition">
WordPress Published Post that meets the criteria for the Fifth Condition
WordPress Published Post that meets the criteria for the Fifth Condition
WordPress Published Post that meets the criteria for the Fifth Condition
WordPress Published Post that meets the criteria for the Fifth Condition
</div>
Is this possible? how? please help.
Please try the following:
Step 1: The loop
Step 2: The output
To output the parts we can use:
Step 3: Our custom function
We define our custom function to return the corresponding template:
or using the great idea of this answer here:
We should also consider wrapping our loop with:
to make sure our function exists before we try to use it in the loop.
Also do a similar check when we define the function, just to make sure it doesn’t exists.
Step 4: The template part(s)
We create the file
content-part.php
in the current theme directory:If you want different templates corresponding to each condition you can just create the files:
and modify the
get_mypart( 'part' )
toget_mypart( 'part1' )
etc.Step 5:
Have a cup of coffee and enjoy life 😉
ps: Image borrowed from Wikipedia.
–Hope this helps.
Store the outputs of divs into variables while looping, output after the loop ends.
Check this one it is close to your problem.
Thanks