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.
Related
I use ACF-plugin: https://wordpress.org/plugins/advanced-custom-fields/
There is a simple HTML file called sidebar.php.
This sidebar.php file has a place to display the image via ACF:
<figure class="Sidebar_Block">
<img class="ACF_Img" src=" <?php the_field('sidebar-latest') ?> "> // ****** place to display a picture ******
</figure>
In other files (home.php, category.php) i call sidebar.php via command
get_sidebar();
Image display only works on the home page (home.php)
And in the category.php file, displaying the image through ACF does not work.
The question is:
How to connect ACF to category.php and display image via WordPress admin?
The problem is that you saved sidebar-latest field only for your home page. I mean, it's attached to the home page. When you call the_field and you don't pass the page/post ID in the second argument, it will take the current one.
So for the homepage works because the sidebar image is saved for the home_page but not when you change page, in order to make it work, pass the home page's post ID to the second parameter:
<img class="ACF_Img" src="<?php the_field('sidebar-latest', $home_page_id); ?>">
So it will work on all page, also, remember to remove any extra space like my example.
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.
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.
I have created a navigation for my WordPress, the navigation gets the title of the pages and display them by title.
Now I want to change the contact title to an image(mail icon of course).
I have add a feature image in the contact page.
I can call the thumbnail with
<?php echo get_the_post_thumbnail(); ?>
The problem is that the thumbnail image only shows when I'm inside the contact page where the thumbnail is added.
I could add the image in every page that I create, but it must be another better way to do it.
This is my navigation code.
<ul id="nav">
<?php wp_list_pages('exclude=19&title_li=', 'sort_column=menu_order'); ?>
<?php echo get_the_post_thumbnail(); ?>
</ul>
As you see I am excluding page 19, so it wont show in the navigation.
But i can call the image with the get_the_post_thumbnail.
After some searching i found out that i wasn't that hard at all.
If i want to use the thumbnail from page 19 I just write
<?php echo get_the_post_thumbnail('19'); ?>
I'm using a template called 'expression' for Wordpress which works fine.
I'd just like to tweak it a little.
I have a page that shows all my blog posts, and ofcourse each blog post has an own overview if you click on it.
on each blog post you make, you can set a featured image. This image is shown on the overview page, and on each individual detailed view of 1 blog post.
I'm trying to find a way to only show this featured image on the overview page of all blogs, and not on the detailed view of a blog post.
Is there a way to do this?
I tried to edit the code in post-template.php by adding:
ispage('blog')
full code:
if(has_post_thumbnail() && !$hide_thumbnail && !is_page('blog')){ ?>
<div class="blog-post-img">
<?php if(!is_single()){?>
<a href="<?php the_permalink(); ?>">
<?php }
$img_id=$pex_page->layout=='full'?'post_box_img_full':'post_box_img';
the_post_thumbnail($img_id);
if(!is_single()){
?>
</a>
<?php } ?>
</div>
<?php
}
this works, but both featured images will be gone. which is not what I wanted.
Thanks for your help!
Got it eventually,
you have to use:
is_single();
instead of :
is_page();
Thanks anyway.