Set default image for all posts - php

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.

Related

Image size in single category post being overriden

I'm using this in a single post category template:
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(array( 200, 200 ));
}
?>
This is working except for the fact that the thumbnail, no matter what I put in the paramater, is showing at full size so it's huge on the page. When I inspect it, the 200x200 is marked out and it's using width:100% and height:auto. Obviously there's a global setting for single thumbnails somewhere, which is fine, but I need to control the images specifically for this category page to be 200x200.
Is there a better solution to force the thumbnail size here?

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.

Blog page featured image being overridden by blog post featured image

On my Wordpress blog page (home.php) template, I'm trying to display the featured image set for that page at the top, then the content archive grid underneath. However, instead of displaying the featured image I set for the page, the first blog post's featured image is displayed instead. Additionally (related), if I remove the first blog post's featured image, it uses another post image on the content archives page. I'm guessing the two problems are related, but I'm not sure. Any help would be appreciated.
Here's the code I put in my functions.php
//Add featured images
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
global $post;
wp_reset_postdata();
echo the_post_thumbnail($post->id); /*you can use medium, large or a custom size */
}
WordPress ignores anything you set on the "Page for Posts". Instead of treating it like a page in the general sense, it treats it more like an archive of posts and the "page" is merely a URL. That said, you can access the ID of the page and then use that to get the title, featured image, or other custom fields you assign to that page:
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
echo get_the_post_thumbnail( get_option( 'page_for_posts' ), 'large' ); // Or use whatever size you need
}
This says, "Get me the featured image on the page I set as the Blog page in Settings->Reading". The ID of that page is stored in your options table with the name page_for_posts.

using inserted media as background of a div in wordpress

I am trying to create a custom theme, where in one is supposed to use some custom post types in which he is supposed to insert one new media (Image) and then i have to use that attached image as the background of some div inside my post ? How do i make best use of wordpress in this scenario.
What currently i am doing is -
<div class="<?php echo get_post_meta($post->ID, $bgImageClass, true); ?>"></div>
And then i am defining this image class in my style sheet and uploading media seperately ,
Now i did google it up to find some nice solution and what i found till now is
wp_get_attachment_image_src( $attachment_id );
and explicitly giving attachment ID which i dont prefer, since d/w will have to work again. Now i read about the function -
<?php get_attached_media( $type, $post_id ) ?>
But i am not sure about its usage, i dont understand what and how it will return the media.
So, how can i use the power of wordpress in this scenario?

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

Categories