Blog page featured image being overridden by blog post featured image - php

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.

Related

Wordpress - How can i have featured categorized post not mix with the main postings

Im using wordpress with twentyfourteen as theme. On the right sidebar, I already have widget that show top latest featured post. Therefore, I want to remove featured post from main posting to avoid duplicate.Here is the link to my website
Example above, you can see the "PROPERTY LAUNCH" post is duplicated on the main posting and on the featured posting on the right.
How can I resolve this so featured categorized post does not appear in main posting ?
After googling for few more hours I finally found solution to my question from this wp explorer website . This code will remove post from featured categories from appearing in main posting .
function exclude_category($query) {
if ( is_home() ) {
$query->set( 'cat', '-23' ); //23 refers to the my featured category id
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category' );

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.

set image from external url as featured image if its not present

i m trying to set a fallback image which is already uploaded to the site/present in media library as featured image if there's no featured image when i click on publish or update but it doesn't set the image. nothing happens
code in functions.php
function thumb_check($post_id){
$url="http://example.com/uploads/flower.png";
if (has_post_thumbnail( $post_id)){}
else { update_post_meta($post_id,'_thumbnail_id',$url);}
}
add_action( 'save_post', 'thumb_check' );
_thumbnail_id only works for WP photo id not URLs. Take a look at at this post https://wordpress.org/support/topic/_thumbnail_id-not-display-when-full-url-of-image
Here is my suggestion:
UPDATE: url_to_postid doesn't seem to work very well on my end. You should go to Media then look for your fallback image id and manually placing the image id in the code bellow. To see the image id look at the url it should look like http://example.com/wp-admin/upload.php?item=51 where 51 is the image id. Also, I replaced update_post_meta with set_post_thumbnail
functions.php
function thumb_check($post_id) {
if(!has_post_thumbnail($post_id)) {
set_post_thumbnail($post_id, 51)
}
}
add_action('save_post', 'thumb_check');

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

wordpress add thumbnail to post Excerpts plugin or method

I am looking for a way to add thumbnail images to post Excerpts.
I've installed: SuperSlider-Excerpt and Thumbnail For Excerpts plugins but I need to specifically assign the thumbnail that I want to use per post or automatically get the featured image to display as the thumbnail.
Anyone know a method or plugin that can do this please?
You can do this pretty easily with Wordpress. Like Francy said, the_post_thumbnail() will add the featured image you have picked on the post edit page in the admin section.
First you need to set up your thumbnails in your functions.php file. This is in the root directory of your theme (wp-content/themes/your-theme). Write this:
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 200, 200, false ); // Set your size, and clipping options
This would make thumbnails 200x200. You may want something else, so put whatever number you want in there.
Now that thumbnails are active, you can use the the_post_thumbnail() throughout your site, inside the loop, to display the thumbnail you've picked.
For example, if you want them to appear on the index page, head to index.php in your theme root directory, and write , right before . Experiment with where these appear, and get it in the right place for you.
Try within the loop:
the_post_thumbnail("thumbnail");
Or what also helped me was:
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail');
$theImageSource = $image[0];
?>

Categories