Option to display Featured Image, or not - wordpress

Wordpress 4.3.1
Hi,
this is my first post at stackoverflow and I hope I did everything right.
I need a function that allows to output the Featured Image, or not. Just not uploading an image is no option, because the thumbnail next the excerpts is needed in any case.
The following combinations are needed:
Thumbnail and Featured Image are identical
Thumbnail and first image in the article are different
Thumbnail and no Featured Image
I need the function for case 2 and 3. In case 2, the Featured Image will be integrated via the editor.
I have already found this plugin: https://wordpress.org/plugins/hide-featured-image/
However, that solves the task only with CSS:
.has-post-thumbnail img.wp-post-image{ display: none; }
But I would like that the picture is not even putted out and loaded in the Browser.
I have the following solution found on the net. It is working so far that the checkbox is shown inside the Featured Image box and it also saves the state if it is checked, or not, after updating the article. However, the Featured Image continues to be output and I don't understand what is going wrong.
Here is the code that I copied to the functions.php:
/**
 * Adds a checkbox to the featured image metabox.
 *
 * #param string $content
 */
  
add_filter( 'admin_post_thumbnail_html', 'optional_featured_image', 10, 2 );
/**
 * Add a 'hide the featured image' checkbox to the Featured Image area
 */
function optional_featured_image( $content, $post_ID ) {
    $content .= '<div class="hide-feat-image">';
    // add our nonce field
    $content .= wp_nonce_field( 'display_featured_image_nonce', 'display_featured_image_nonce', true, false );
    // create our checkbox
    $content .= '
    <input style="margin: 5px;" type="checkbox" id="display-featured-image" name="display_featured_image" '. checked( get_post_meta( $post_ID, 'display_featured_image', true ), true, false ) .' value="1"/>
    <label for="display-featured-image">Hide on post page?</label>
    ';
    $content .= '</div><!-- hide-feat-image -->';
    // return our appended $content
    return $content;
}
add_action( 'save_post', 'save_optional_featured_image' );
/**
 * Save our 'hide the featured image' checkbox to the Featured Image area
 */
function save_optional_featured_image( $post_id ) {
    if (
        // check nonce
        !isset( $_POST['display_featured_image_nonce'] )
        || !wp_verify_nonce( $_POST['display_featured_image_nonce'], 'display_featured_image_nonce')
        // make sure user is allowed to edit posts
        || !current_user_can( 'edit_post', $post_id )
    )
        return;
    // sanitize our input to yes or no
    $display = isset( $_POST["display_featured_image"] ) && $_POST["display_featured_image"] ? true : false;
    // update our post
    update_post_meta( $post_id, 'display_featured_image', $display );
} 
And here's the WP snippet, I use at the template for the output:
<?php the_post_thumbnail('header-image'); ?>
I'm grateful for any advice! Thank you in advance for your effort!

Firstly I'd check in the database that this 'optional_featured_image' parameter is actually being set by your code. Check the prefix_postmeta table and lookup for the post you're editing.
When the template is output you need to have a conditional to check for this 'display_featured_image' parameter you've setup. Otherwise the_post_thumbnail() will always display.
Try this code in your template:
<?php
$optional_featured_image = get_post_meta( get_the_ID(), 'display_featured_image', true);
if( !$optional_featured_image )
the_post_thumbnail('header-image');
?>

Related

remove and disable featured image in posts

I write a post in new post editor and then add media. I can see the images in the editor but when i publish them they aren't loading up and a frame with little square in the middle comes up. This is the link to one of my posts: http://getprogramcode.com/2013/03/c-program-for-bit-stuffing/ . For some people only link to the image comes up and it opens up with 404 error. See the line after OUTPUT: bit stuffing.
Also i want to remove the featured image from appearing in my posts. I have a option in my theme on a new post: "disable featured image" - but that doesnt work . Also if i dont put the image or i remove the featured image the empty image appears: see the home page: http://getprogramcode.com Please help me to solve this problem
You should not use relative paths on WordPress, only Absolute paths.
The problem is that you are using ../wp-content/... as your URL's for image paths.
When adding images, you should have the option to give it a path, you should opt to link to the media file.
For the disable feature image, if you go into the page.php or single.php code, it should have a line of code in it for calling in the featured image.
It should look something like this:
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
You just need to remove or comment out this code and it should stop showing up on the pages.

The URL added while adding featured image in wordpress

I am working with wordpress latest version.
As in wordrpess, we can add URL while adding a featured image in a post as there are text fields like caption, title, alt text, description, URL etc.
I've added a link like http://example.com/abc-news
I am fine to show the featured image in the blog post but I want that link as
{featured-image}
So that, if anyone clicks on the featured image he/she will go to that link.
Any help would be appreciated.
If you are using the default theme with the latest version of WordPress, you can edit content.php in the 2014 theme directory.
You need to wrap <?php twentyfourteen_post_thumbnail(); ?> with the link code pointing to your URL.
This code should be around line 13 in content.php.
<?php twentyfourteen_post_thumbnail(); ?>
To link to the current blog post, you could add this code:
<?php twentyfourteen_post_thumbnail(); ?>
If you are using a different theme than the default, open the file which contains The Loop in your theme, search for the_post_thumbnail or a similarly named function, and change the code as above.
The exact answer will vary depending on theme, but 98% of the time the fix should be similar to the above. Hope this helps.
Edit: Here's some more information: http://codex.wordpress.org/Function_Reference/the_post_thumbnail#Post_Thumbnail_Linking_to_the_Post_Permalink
Thanks for your prompt reply.
You are right that it's 98% depending on the theme.
The theme is using a meta_value to add a custom URL for the featured image.
So, I need to get that URL through get_post_meta
I've got from here.
And I am fine with it now. :-)
Thanks

Wordpress: How to pass additional Content to the blog-preview page?

For each blog-post on my wordpress-blog I'd like to have Teaxtarea where i can pass additional content for that post.
In my case that would be an unordered list which contains a quick overview of the content.
That additional content should be displayed in the preview of the post on the blog-preview-page.
My problem:
I am actually not sure on how to best add this additional content and then pass it to the preview.
Do I use wordpress' custom fields for something like this?
I'm gratefull for a push in the right direction.
Thank you,
Nils
If I understand you right, I'd take a look at "custom meta boxes" functionality - it allows you to add any type of additional input into your blog post admin area, and than display its content on front-end however you like.
There's a nice tutorial series on that topic, with example code snippets:
http://wp.tutsplus.com/series/reusable-custom-meta-boxes/
And if you'd like to display the textarea content only in preview mode, you can use appropriate conditional tag in you template file:
http://codex.wordpress.org/Conditional_Tags#A_Preview
The conditional tag is_preview returns true when a single post is viewed in Draft mode. The following will append post meta to the content when a post is being previewed:
function so16799607_preview( $content )
{
if ( ! is_preview() )
return $content;
return $content . get_post_meta( get_the_ID(), 'my_post_meta', true );
}
add_filter( 'the_content', 'so16799607_preview', 10, 1 );
You should check out Advanced Custom Fields. That's a really stable plugin that lets you create custom meta boxes in posts, pages and custom post types. That plugin does exactly what your question states. Need al little PHP to get stuff from your database, but that is as easy as:
<?php the_field(field_name);?>
And the documentation is pretty good. And if you don't like a plugin, it exports the PHP as well.
Anther tool that does the same is Pods Framework. Both powerfull extensions to any WP install in my opinion.
Hope this helps.

Setting thumbnail featured image size not working properly

Hi I just started today on creating my first Wordpress Theme and I am trying to create a featured Image for each post.Aldo I have managed to acomplish that it seems that the sizes I am giving it are not taking effect.Here is my code:
if(function_exists('add_theme_support')){
add_theme_support('post-thumbnails' , array('post'));
set_post_thumbnail_size(200,120);
}
if(function_exists('has_post_thumbnail') && has_post_thumbnail()){
the_post_thumbnail();
}
It seems that my featured images are not always set to the same size.For example if the image is smaller then what size I set it will remain the same , for big images the width is the same but for some the height is different.
What am I doing wrong here?
Are you setting the thumbnail size in functions.php? It will not work properly if it's just in index.php or another theme file.
From the codex:
This feature must be called before the init hook is fired. That means
it needs to be placed directly into functions.php or within a function
attached to the 'after_setup_theme' hook. For custom post types, you
can also add post thumbnails using the register_post_type function as
well.
the_post_thumbnail() displays the thumbnail and should be placed where you want the thumbnail to display.

How to change default post attributes?

My site needs to have image based posts, meaning the post is only an image.
Now i tried implementing it with Custom Post Types, but have encoutred problems,
like categories didn't show the right posts, pagination caused problems etc.
Now i thought to myself that i don't need the regular posts and if i could just edit them
to have only the featured image option enabled, life would be much easier.
But i failed to find any information regarding this.
Anyone can help me please?
To add featured image support/option simply add the following code snippet to your functions.php file located inside your theme's root folder
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
and to show the featured image inside your template you can do
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(); // will show the featured image if you have set any for the post
}
Applicable to Wordpress version-2.9 and higher.
When you add new post just find the featured image meta box and set an featured image from there.
You can also setup image sizes (depending on that images will be displayed) at the front end, just take a look at here.

Resources