I need to make a grid similar to this:
Few keywords: specific size pattern, bigger points in higher.
All boxes are custom posts - lets call every box a post.
All different sizes posts have different meta_key called $size:
Yellow/big $size= 1;
Orange/medium $size= 2;
Blue/small $size= 3;
All posts have also meta_key value called $points which are totally different for each (can be anything from 0 to 10000). $points is currently used as orderby.
This is how I call all these posts:
$custom_posts= new WP_Query(array(
'post_type' => 'custom-post',
'meta_key' => 'points',
'orderby' => 'meta_value_num'
));
if ( $custom_posts->have_posts() ) :
echo '<div id="post-items"><ul class="row list-unstyled">';
while ( $custom_posts->have_posts() ) : $custom_posts->the_post();
$size= get_post_meta( $post->ID, 'post_size', true );
$points = get_post_meta( $post->ID, 'post_points', true );
echo '<li>';
if($size == 1) {
?>
//Big yellow box HTML
<?php
}
else if($size == 2) {
?>
//Medium orange box HTML
<?php
}
else if($size == 3) {
?>
//Small blue box HTML
<?php
}
echo '</li>';
endwhile;
echo '</ul></div>';
wp_reset_query();
else :
?>
<p class="text-muted"><?php _e( 'No Posts.', 'aa' ); ?></p>
<?php
endif;
QUESTION:
Any ideas how to query them in specific $size pattern also considering $points (higher points in higher, but do not break the order - LIKE ON MY IMAGE)?
Im working my arse off as we speak (it's 3rd day now on this problem!!) & I would appriciate every idea, comment or source!
What could be useful but I haven't found a working solution yet (I'll add more if I come across some):
Using modulus:
PHP loop: Add a div around every three items syntax
UPDATE 1:
I am not a Wordpress expert by any stretch, so there may be an easier way to do this with a foreach loop, but here it is with a while as you have it in the question:
// This will be used as a counter
$i=0;
while ( $custom_posts->have_posts() ) : $custom_posts->the_post();
// Get size based on counter
switch($i) {
case 0:
case 5:
$size = 1;
break;
case 1:
case 2:
case 3:
case 4:
case 6:
case 7:
$size = 2;
break;
default:
$size = 3;
}
$points = get_post_meta( $post->ID, 'post_points', true );
echo '<li>';
if($size == 1) {
?>
//Big yellow box HTML
<?php
}
else if($size == 2) {
?>
//Medium orange box HTML
<?php
}
else if($size == 3) {
?>
//Small blue box HTML
<?php
}
echo '</li>';
// Increment the counter
$i++;
endwhile;
If I understand your question correctly, this should always display the sizes in the order that you have them (1,2,2,2,2,1,2,2,3,3,3,3,3,3, etc.). Also, if there are less than that number of entries, it will still work, and if there are more, size 3 is the default, so anything else after this will display as 3.
Related
I have checked similar questions in this forum but I can't find any answers that show me exactly how I can do this.
I've set up a WP page template that lists all categories (and their associated posts) alphabetically, in 2 columns. The display is similar to that shown below:
The code is working fine.
However, I want to change the code so that I can pass it a letter (for example, 'A') so that only Categories beginning with the letter 'A' (or whatever letter is chosen) - and their posts - are shown.
For example, passing the letter 'A' would result in the following display:
The code I'm using is as follows:
<?php
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'>";
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'>";
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$customfieldvalue = get_post_meta($post->ID, "PDF", true);
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";
}
?>
My question is, how do I edit the code so that a letter ('A', 'B', 'C' etc) can be passed to the code and have only the Categories beginning with that letter displayed?
UPDATE
It's been suggested that Lafif Astahdziq's solution, posted some time ago in answer to another question, is also an appropriate answer to my question.
At a casual glance it may be. However, having read Lafif's answer, I'm afraid I cannot understand how his code works and how I might use it to improve my code and solve my question.
If a particular answer 'works perfectly' but cannot be understood by the individual posing the question, is that a satisfactory outcome?
If I accept Lafif's answer, then I'll have to ask a further question - how do I use Lafif's code?
Finally, I am not trying to deride Lafif's answer in any way. I'm sure it was made with the best of intentions in response to the earlier question.
You could do a custom query to the Wordpress database, where you send the search letter in the URL.
Where the query would be something like(simplified):
$letter = $_GET['firstLetter'];
$sql = "SELECT * FROM wp_posts WHERE cat_name LIKE '" . $letter . "%' ";
You can read about the database relations regarding taxonomies here
I think I am pretty close to the solution but I cannot figure out the logic for where I should close the last div.
I have a wordpress query which requests four posts.
I want to wrap every two items in <section class="sponsor-row">
So I have implemented a counter in my query that counts each post, and if it finds two posts have been output it closes the <section> div off.
But I cannot work out how I should work out if the row should be closed again.
Can anyone figure this out? How can I make it wrap every two outputs in <section class="sponsor-row"> ?
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$counter = 0;
echo '<section class="sponsor-row">'; // Opening the section here
if ( has_post_thumbnail() ) {
$counter++;
echo '<div class="grid half">';
echo '<a class="sponsor" href="'.get_the_permalink().'" target="_blank">';
the_post_thumbnail( 'full' );
echo '</a>';
echo '</div>';
if ($counter <= 2){
echo '</section>'; // closing the section if two posts
$counter = 0;
}
}
}
}
Full query here: http://pastebin.com/e6RzZLR5
if you do if ($counter <= 2){ then it will close it each time it is less or equal 2, which means it will close it twice for each item. You should use if ($counter == 2){ and $counter = 0; should be at the top before the query, otherwise you will set it to 0 each loop.
See comparison in php
I've tried what I could on this and am still stuck, so I'm looking for some help. I'm sure there's something small I'm overlooking or am not aware of, so I'd be grateful for another set of eyes to take a look!
I'm trying to use a switch within a Wordpress while loop to set dimensions on post thumbnails for specific posts. The switch uses an auto-incrementing value ($count). Inside the loop, $count will return the right number for div ID's, but it will not work with the switch. All of the thumbnails go to the size defined before the loop begins (see $thumbsize)
Here's the code:
// Setup loop to pull only posts tagged slider
$max = 6;
$args = array('tag' => 'slider','posts_per_page' => $max);
$featuredPosts = new WP_Query();
$featuredPosts->query($args);
// Defaults for post thumbnail display
$thumbargs = array('class' => 'featured-blocks-img');
$thumbsize = array(640,360);
$count = 0;
// Begin loop
if ($featuredPosts->have_posts()) : while ($featuredPosts->have_posts()) : $featuredPosts->the_post();
$count++;
// Get post category and format for div class name
$category = get_the_category();
$catname = $category[0]->cat_name;
$catdash = 'cat-';
$catdash .= str_replace(' ', '-', $category[0]->cat_name);
$catdash = strtolower($catdash);
// Change post thumbnail size conditionally
switch ($count) {
case 2:
case 5:
case 6:
$thumbsize == array(320,260);
break;
default:
$thumbsize == array(640,360);
} // End switch
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div id="home-featured-post-<?php echo $count;?>" class="featured-blocks-post <?php echo $catdash; ?>">
<h2 class="home-featured-title"><?php the_title(); ?></h1>
<?php the_post_thumbnail($thumbsize, $thumbargs); ?>
</div>
</a>
<?php
endwhile;
endif; // End loop
And here it is in Gist form if that's helpful to anyone: https://gist.github.com/anonymous/8984741
I tried to add comments that would provide some context.
Any ideas of what's happening? I can provide the resulting HTML source if that would help also.
Looks like you aren't actually setting $thumbsize in the below code
$thumbsize == array(320,260);
== is comparing $thumbsize to that array, not creating an array with those values.
You really want it to just look like this:
$thumbsize = array(320,260);
My loop displays posts in two columns using this:
<?php
if (have_posts()): while (have_posts()) : the_post();
$count++;
?>
<?php if ($count == 1) : ?>
<div class="home-ci-row">
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div>
<div class="home-ci-gap"></div><!-- /* the gap */ -->
<?php elseif ($count == 2) : ?>
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div> <!-- main-column-item-wrap -->
</div><!-- /* home-ci-row*/ -->
<?php $count = 0; ?>
<?php else : ?>
// No posts
<?php endif; endwhile; endif; ?>
You can see that the <div class="home-ci-row"> opens in the first count & closes in the second one </div>
so when my loop has an even number of posts works great, but with odd number it doesn't close the div
so My idea is this: If loop has even number
If loop has odd number of posts
By the way, you can do something like:
<?php
$count=0;
while(have_posts()){
if($count%2==0){
echo '<div class="home-ci-row">';
//draw your left div here
}else if($count%2==1){
//draw your gap here
//draw your right div here
echo '</div>';
}
$count++;
}
//close div if count is an odd number
if($count%2==1) echo '</div>';
?>
Is it possible to swap to a for loop? Is this what you need?
for ($i = 0; $i < $numberOfElements; $i++)
{
//if (odd number) && (this is the last element)
if (($i % 0 == 1) && ($i == $numberOfElements - 1))
{
//Special Case
}
else
{
//Normal Case
}
}
Caveat: Watch out for errors, PHP isn't my strongest language
This question was answered on WP Development StackExchange by fischi
Quoting from his answer:
You could do this much easier. As you are making a layout that can be achieved by floats, there is no need to declare a Row every second time.
In my Code example I just youse the $count to determine the Class of the HTML-Element. In combination with the total Number of Posts displayed.
If the total number of posts $wp_query->post_count is reached by the $count AND the total number is odd, I give the Element the Class fullwidth. In the same way, I determine if it is the first or the second (see the IF-Statement).
All I need to do afterwards is output the corresponding Class for each HTML-Element in the Loop. Besides the Class, no Element is diffferent from each other.
I use the Modulo Operator in PHP ( % ) to determine odd/even. It delivers 1 if I use $count % 2 and $count is odd. If you are not sure about this operator, read about it here.
So your code could look like this:
<?php
$count = 0;
if (have_posts()): while (have_posts()) : the_post();
if ( ++$count == $wp_query->post_count && ( $wp_query->post_count % 2 ) == 1 ) {
// if final count is reached AND final count is odd
// full width item
$postclass = "fullwidth";
$opentag = '';
$closingtag = '</div>';
} else if ( ( $count % 2 ) == 1 ) {
// if $count is odd it is the first item in a 'row'
$postclass = "halfwidth first";
$opentag = '<div class="home-ci-row">';
$closingtag = '';
} else {
// second item in a row
$postclass = "halfwidth second";
$opentag = '';
$closingtag = '</div>';
}
?>
<?php echo $opentag; ?>
<div class="main-column-item-wrap <?php echo $postclass; ?>">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div><!-- main-column-item-wrap -->
<?php echo $closingtag; ?>
<?php endwhile; endif; ?>
I can't seem to figure this out from the php manual. The following code is giving me the 2nd post that is returned. What I would like to do is get 2 posts and then stop.
$count=0;
$related = p2p_type($connected_type)->get_related( get_queried_object_id() );
// Display related posts
if ( $related->have_posts() ) :
$count++;
while ( $related->have_posts() ) : $related->the_post();
if ($count == 2) {
echo the_title();
} else {
echo 'this post won't show up';
}
endwhile;
// Prevent weirdness
wp_reset_postdata();
endif;
you can use if($count >2) break; or break; in the else
break ends execution of the current while .
for more information check this link php manual break