How to limit 1 image per post on homepage only?

/** * @author: wpreceipes * @link: [1]: http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it */ function wpse18215_catch_that_image() { global $post, $posts; $first_img = ”; ob_start(); ob_end_clean(); $output = preg_match_all( ‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches ); $first_img = $matches[1][0]; //Defines a default image if( empty( $first_img ) ) { $first_img = ‘/images/default.jpg’; } return $first_img; } Example: echo catch_that_image(); or: /** * @author: Marcelo … Read more

Alternate header image

You can try this Random from a set of images : <img src=”http://www.mywebsite.com/images/image_<?php echo rand(1,10); ?>.jpg” alt=”” /> This would load a random image from image_1.jpg, image_2.jpg … to image_10.jpg or $images = array(“cool_image.jpg”, “nice_pic.jpg”, “sunset.jpg”); $rand = array_rand($images); <img src=”http://www.mywebsite.com/images/<?php echo $images[$rand]; ?>” alt=”” /> This would select a randow image from thoses specified … Read more

Include captions

st Note: // Prepare the code for safety $images = $wpdb->get_col( $wpdb->prepare( ” SELECT ID FROM %s WHERE post_type=”attachment” AND ID IN ( %s ) ORDER BY menu_order ASC “, $wpdb->posts, $meta ) ); nd Note: Look into what you already got: // Inside the foreach loop echo ‘<pre>’; var_dump( $att ); echo ‘<pre>’; rd … Read more

Query for recent images across multiple posts

You can use get_posts or create a new WP_Query using the following args (or something similar). <?php $args = array( ‘post_type’ => ‘attachment’, // attachment post type ‘post_status’ => ‘inherit’, // all attachments have this post status ‘post_mime_type’ => ‘image’, // make sure you get images only ‘posts_per_page’ => 5 // however many images you … Read more

Metabox with multiple fields added by user and upload box

Here is how I’ve done it. No changes to the save function are needed. I’ve been trying to save data from both fields at once, thats why it didn’t worked. <input class=”upload_file” type=”text” size=”45″ id=”picture_%1$s” name=”picture_%1$s” value=”%6$s” /> <input class=”upload_button button” type=”button” value=”Upload File” /> <input class=”upload_file_id” type=”hidden” id=”picture_%1$s_id” name=”items[%1$s][picture]” value=”%7$s” /> The value stored … Read more

Randomly add a ‘ul’ list of images

Adding the following to your theme’s functions.php file will let you use a shortcode ( [wpse73055-random-images] ) with an optional num parameter (i.e. [wpse73055-random-images num=5] to display 5 random images). function wpse73055_get_random_images( $atts ) { extract( shortcode_atts( array( ‘num’ => 10 ), $atts ) ); $args = array( ‘post_type’ => ‘attachment’, ‘post_mime_type’ =>’image’, ‘post_status’ => … Read more

Image display from custom field

If you use update_post_meta() than also use get_post_meta() (see Codex) and not get_post_custom $order = get_post_meta( $post->ID, ‘order’, true ); $image = get_post_meta( $post->ID, ‘image’, true ); And than check in your HTML source what your image tag looks like. I bet it will look like this: <img src=”https://wordpress.stackexchange.com/questions/75550/coffee_star.jpg” /> What about the path to … Read more