K
K
Kostya2014-02-11 14:15:05
CMS
Kostya, 2014-02-11 14:15:05

How to add a thumbnail inside a post in Wordpress?

Hello everyone, I'm interested in the question of how you can optionally enable or disable the image thumbnail inside the post?
That is, a function is used for output, <?php the_post_thumbnail( ); ?>and it displays entries in my announcement and inside the post. It is necessary to make it so that when necessary, inside a certain post, you can turn off the thumbnail even if it is set in order to be displayed in the announcement.
I saw such an implementation on the Thenextweb Wodpress site. Example: an article with a thumbnail ; article without thumbnail .
Who knows how to implement it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikolai Antal, 2014-02-12
@kapnod

Regarding code.tutsplus.com/tutorials/reusable-custom-meta-b...
From this article you need the code:

// Вставляете его в файл function.php
// Add the Meta Box
function add_custom_meta_box() {
    add_meta_box(
        'custom_meta_box', // $id
        'Custom Meta Box', // $title
        'show_custom_meta_box', // $callback
        'post', // $page
        'normal', // $context
        'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');

and (somewhat modified):
function show_custom_meta_box( $post) {
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
   
// get value of this field if it exists for this post  
 $meta = get_post_meta($post->ID, 'thum_disable', true);

echo '<input type="checkbox" name="thum_disable" id="thum_disable" ' . checked($meta, "on"); . ' value="on" />
        <label for="thum_disable">Отключить миниатюру</label>';
}

State saving:
// Save the Data
function save_custom_meta($post_id) {
        
    // verify nonce
    if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
        return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id))
            return $post_id;
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;
    }
     
     $old = get_post_meta($post_id, 'thum_disable', true);
     $new = $_POST['thum_disable'];
        if ($new && $new != $old) {
            update_post_meta($post_id, 'thum_disable', $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, 'thum_disable', $old);
        }
}
add_action('save_post', 'save_custom_meta');

Next in the template where you want to manage the thumbnail:
$thumb_disable = get_post_meta($post->ID, 'thum_disable', true);
if(!empty( $thumb_disable )) { the_post_thumbnail( ); }

A
Alexander Zelenin, 2014-02-11
@zelenin

create a custom field as a checkbox for posts with a plugin (ACF?) or built-in WP tools. In the template, check whether the checkbox is checked or not.

N
Nikolai Antal, 2014-02-11
@hermit931

With the help of a shortcode :).
Write a function for a shortcode that will display a thumbnail.
In the post output file, you will probably have to use the get_the_content function and check for the presence of this very shorcode, if there is, disable the thumbnail output with the_post_thumbnail function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question