show random image in loop if image is not set

My solution was really simple: $input = array(“defaultImage1.jpeg”, “defaultImage2.jpeg”, “defaultImage3.jpeg”, “defaultImage4.jpeg”, “defaultImage5.jpeg”, “defaultImage6.jpeg”); $rand_keys = array_rand($input, 2); echo ‘<img class=”story-carousel” src=”‘.$upload_dir[‘url’].”https://wordpress.stackexchange.com/”.$input[$rand_keys[0]].'” alt=””>’;

How to target thumbnails of the first post in the loop

You can get first post class similarly as you are adding last post class. In last post case, you were checking if current_post number is equal to post_count. But in this case when we want to get first post, we will check if current_post number in loop is equal to 0 or not. <?php $extraFirstClass … Read more

Stuck in the query loop

<div class=”pr_grid_cell clearfix”> <?php //for each category, show posts $categories=get_categories(); foreach($categories as $cat) { $args=array( ‘post_type’ => ‘products’, ‘showposts’ => -1, ‘category__in’ => array($cat->term_id) ); $posts = new wp_query($args); // This is the best way to if ($posts->have_posts()) while ($posts->have_posts()) { //condional if/while 2-in-1. If you dont check have_posts, you’ll get an ugly error when … Read more

Get Image tag from content of post

You can either make use of get_children or get_posts or even WP_Query to get the the attached image ID’s to the current post You can then use those ID’s to gt the attached image with wp_get_attachment_image() Here is a basic idea that you can use in a widget or sidebar. (CAVEAT: Untested) $args = array( … Read more

Sanitize Custom Field Value in Meta Query

The strtotime() function is what you need here: $date = date( ‘Y-m-d’, strtotime( $date ) ); //2015-04-10 with your example Note that you will need to do this when you save those values, not when running the query. You can’t convert data that’s already in the database on the fly when running a query. Edit: … Read more