Wordpress - 在下面添加描述特色图片

时间:2015-06-13 11:01:03

标签: wordpress

我想在帖子页面的特色图片框下面添加一个注释,说

'推荐图像尺寸:1300像素(宽)x 340像素(高)

是否可以在wordpress 4.2.2。

该文本供管理员用户使用,以获得更好的指导。

非常感谢任何帮助。提前谢谢。

3 个答案:

答案 0 :(得分:1)

这里的旧帖子,但我也尝试这样做。自WordPress 2.9.0以来的过滤器就提供了此功能,例如:

/**
 * Filters the admin post thumbnail HTML markup to return.
 *
 * @since 2.9.0
 * @since 3.5.0 Added the `$post_id` parameter.
 * @since 4.6.0 Added the `$thumbnail_id` parameter.
 *
 * @param string $content      Admin post thumbnail HTML markup.
 * @param int    $post_id      Post ID.
 * @param int    $thumbnail_id Thumbnail ID.
 */
return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );

所以像这样使用它:

function stackoverflow_30817906( $content, $post_id, $thumbnail_id ) {
    if ( 'post' !== get_post_type( $post_id ) ) {
        return $content;
    }
    $caption = '<p>' . esc_html__( 'Recommended image size: 1300px (width) x 340px (height)', 'i18n-tag' ) . '</p>';
    return $content . $caption;
}
add_filter( 'admin_post_thumbnail_html', 'stackoverflow_30817906', 10, 3 );

答案 1 :(得分:0)

你应该去wp-admin->includes->post.php 在第1295行(取决于版本),你会发现:

if ( !empty( $thumbnail_html ) ) {
    $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
    $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
    $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
}

将其更改为:

if ( !empty( $thumbnail_html ) ) {
            $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
            $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
            $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a><span style="display:block;">Recommended image size: 1300px (width) x 340px (height)</span></p>';
        }

不建议更改核心文件,因为更新过程中您的更改可能会丢失,但我不建议仅为此次更改创建插件。

答案 2 :(得分:0)

如果你想在不编辑核心文件的情况下这样做,你可以使用jQuery来添加文本。

add_action('admin_footer', function() {
    global $pagenow;
    if('post.php' == $pagenow || 'post-new.php' == $pagenow) : ?>
    <script>
        jQuery(document).ready(function($) {
            $('#postimagediv .inside').after('<div style="padding: 0 12px 12px;">Recommended image size: 1300px (width) x 340px (height)</div>');
        });
    </script>
<?php endif;
});

此解决方案并非“清洁”。直接在PHP文件中进行更改,但我认为编辑核心更可取。使用此解决方案,您不必担心在WordPress更新后重新更改更改。

相关问题