Isolating and customizing first image in Wordpress post - php

I have a Wordpress site which uses a feature image on the posts which link to a page with the post's content (images, content, etc)
I separate out the images from the content with
<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
echo $images[1][$i];
}
?>
I want to target the first image of each individual post (it is not the featured image) I want to put
<h1><?php the_title(); ?></h1>
overtop the first image, to make it act as a header for the post, then I can have the other images and text underneath. not sure how to accomplish this
EDIT - clarification
My index.php shows the featured image of each post in a 'thumbnail gallery' style. each picture links to content-single.php where the content and images are displayed for that post (the featured image is NOT on this page). content-single.php is where i pull out the text and the images
I want to be able to put a title overlapping the first image of the post (NOT the featured image on index.php) because I want the first image of the content to act as a header for each single post page

Try This.
preg_match_all('/<img[^>]+>/i',$get_the_content(), $result);
echo "<pre>";
print_r($result);
For Frist Image Try this $result[0][0].
It Get All Images.

Try This.
$content = apply_filters ("the_content", $post->post_content);
preg_match_all('/<img[^>]+>/i',$content, $result);
echo "<pre>";
print_r($result);
i hope this is useful for you.!

Related

Bulk add featured image to existing Wordpress posts

I have cca 4500 posts in my Wordpress backend. Some posts doesn't have featured image. I was wondering is there any solution to bulk add featured image (placeholder if there is no "real" image)? I'm talking about backend, I now that I can do on front end but need way to add featured image in the backend if featured image is empty.
Can anybody help me with this?
You can do this using 3 ways.
Using plugin
https://wordpress.org/plugins/default-featured-image/
Or you can show dummy image to your code (when there is no featured image found)
check below code for front-end
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
AJAX in Admin (In custom form) - You can also achieve same using creating one option page in backend where you can give form with one image field, and on save you can update featured image if blank found using ajax.

How Set Image URL As Featured Image?

I have article with title "POST-TITLE" and I create image title same with post title, "POST-TITLE.jpg".
All image I put in my hosting, like this:
http://www.example.com/img/CATEGORY/POST-TITLE.jpg
In mytheme post-single.php, I have this code:
<img src="http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg" onError="this.src='http://cerpenkoran.net/img/dot.jpg';" title="Thumb for <?php the_title(); ?>" >
My answer:
What code I add the image url code above, so it as Featured Image?
OR how to combine this fix URL:
http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg
with this Featuread Image code:
<?php if (has_post_thumbnail( $post->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" />
<?php endif; ?>
Are you asking how to add a Featured Image to your WordPress website? If so, then follow the below instructions:
Firstly, you are going to need to add the relevant Theme Support inside your functions.php file. To achieve this, go into your functions.php file and enter the following code:
<?php
function my_theme_name_support(){
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'my_theme_name_support');
?>
Make sure that the above code is placed after the opening <?php tag (at the top of the functions.php file) but before the closing ?> tag (at the bottom of the functions.php file.)
You will see that there are two my_theme_name_support entries. You can change the wording of these to something more appropriate. Just make sure they match.
Now, head into your WordPress Dashboard and go to create a New Page or New Post. You should now be able to see an entry, entitled 'Featured Image'. As you scroll the page, you should see it appear on the right hand side. Simply select the link and add the required image.
This, alone, does not output the Featured Image. To output your Featured Image, you now need to enter the following code into the relevant file(s) on your server:
<?php the_post_thumbnail('thumbnail'); ?>
By following the above steps, you should now be able to dynamically call your Featured Images from your WordPress Dashboard and output them to whichever pages you want.
For more information, on Featured Images, check out this WordPress Codex Article >>>
Answer Extension
If you want the same image to appear for every Blog Post, so that you do not have to manually add a Featured Image to every Blog Post, you could:
Upload the image to WordPress, via their Media Uploader.
Then head to the single.php Blog Post Template File and enter the following code:
<img src="POST-TITLE.jpg" alt="post-title" height="42" width="42">
Be sure to make the following modifications, to the above code:
src: This is the link to the actual image. Therefore, change 'POST-TITLE.jpg' to the name of your desired image. Be sure to include the correct image format (.jpg, .png etc).
alt: This is a brief description of the image. No coding required here. Plain text will do.
height and width: Insert the correct dimensions. The dimensions are in pixels.
Please Note ...
single.php is the default Blog Post Template File. There is a chance that there are additional Blog Post Template files too. If this is the case, you will need to add the code to these other Template Files too.
You may find this WordPress Article, helpful, when working with Template Files.

Set default image for all posts

I am using this code to have the Featured Image set on all Pages as a banner, in the header. So all pages have different banners:
<img src="<?php $imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID) , 'full'); echo $imgsrc[0]; ?>" class="middle" alt="banner">
I don't want to use this for Posts because I need to set different small pics there. I need a way to automatically have all Posts use a default image banner that wouldn't take the spot of the Featured Image that I need freed up.
Is there a way to auto detect that it's a Post page and have that default image instead?
Tried everything I could find on search engines, but I can't figure out.
One solution (if you use the same template for posts and pages) is to use the function get_post_type() to check the post type:
if( 'post' == get_post_type() ) {
// load image for posts
} else {
// image for pages
}
Another solution is to use a template for posts (like single.php, or single-post.php if you have other custom post types) and one for pages (page.php), so you can use different code on different post types.

Featured Image Not Showing in Wordpress

I'm trying to show a featured image in Wordpress, I've uploaded the image (put it as the smallest image possible) but when I view the blog list view (category.php) I just get the blog post header, and no image.
Anybody know how to show the image? Checked the source, and doesn't show an image at all.
Hope someone can help, would be much appreciated.
In addition to setting the featured image in WP admin, you need rendering code somewhere in your template files. Something like this:
if ( has_post_thumbnail( get_the_ID() ) ) {
echo get_the_post_thumbnail( get_the_ID() );
}
I use this code in my header.php theme file, but you may prefer it within page.php or elsewhere (category.php seems appropriate in your case).
Check the documentation for help changing the dimensions, adding class attributes, etc. to the <img/> tag generated.
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

Get single Wordpress gallery image with given title

I am having users of a Wordpress blog upload a single image to different post's galleries with the name "banner". Basically, every post will have an image named "banner" uploaded to its gallery and this image needs to be displayed on the post's page outside of the article content. So, how can I get the URL to display an image in a post's single.php template?
Can I iterate through the images of a given post's gallery and find the one with the correct title somehow?
I've searched through the Wordpress codex docs and haven't found anything on a way to do this, just information on displaying galleries of photos. Note that I'm already using Wordpress's post thumbnail feature for something else.
Thanks!
I think I finally figured it out. The part I couldn't figure out was using the vaguely named get_children() function.
<?php
have_posts(); //must be in the loop
the_post(); //set the ID
$images =& get_children(array('post_mime_type' => 'image', 'post_parent' => get_the_ID()));
rewind_posts(); //rewind for the actual loop
$image_url = null;
foreach($images as $image) {
if($image->post_title == 'banner') {
$image_url = $image->guid;
break;
}
}
?>

Categories