I’ve been struggling with the PHP While control structure in WordPress. I definitely know this is possible, just don’t know exactly how to do it.
I’m using the WordPress database to store my widget controls form fields data, and I want to loop through the data with this code.
need to control structure by the 2 numbers defined in my widget control.
$data['houses_row']
$data['shown_rows']
Here’s what I’m trying to do:
I’m thinking of Houses and each one is on a plot of land as an analogy. Sort of.
How many houses to show per row? and How many rows to show?
If the row has more houses than the row can handle then those houses need to be hidden, which this can do naturally. I am thinking many houses to many rows.
I hope somebody can help me with my loop.
static $number_posts_shown = 1;
static $per_row_count = 0;
while ( have_posts() ) : the_post();
if ( $per_row_count <= $data['houses_row'] ):
echo "show house: #". $per_row_count ." ";
endif;
$per_row_count++;
// show rows
if ( $number_posts_shown <= $data['shown_rows'] ):
echo "<hr />";
endif;
$number_posts_shown++;
endwhile;
when shown_rows is 4 and houses_row(houses per row) is 4 I wanted the output to be this.
show house: #0 show house: #1 show house: #2 show house: #3
--------------------------------------------------------------------------------
blank
--------------------------------------------------------------------------------
blank
--------------------------------------------------------------------------------
blank
when shown_rows is 4 and houses_row(houses per row) is 2 the output is this.
show house: #0 show house: #1
--------------------------------------------------------------------------------
show house: #2 show house: #3
--------------------------------------------------------------------------------
blank row
--------------------------------------------------------------------------------
blank row
from your question, I get the idea that you want to display houses (stored in wordpress post) based on the format of:
The simplest solution is to store what you already have in temporary array first:
After you have all post data, begin the second loop to generate it:
Because I don’t have a wordpress installation at the moment, I’ll give you this code snippet to test it out:
The code above has the following result: