Answer the question
In order to leave comments, you need to log in
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
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');
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>';
}
// 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');
$thumb_disable = get_post_meta($post->ID, 'thum_disable', true);
if(!empty( $thumb_disable )) { the_post_thumbnail( ); }
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.
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 questionAsk a Question
731 491 924 answers to any question