建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress 为特色图片添加选项

GG网络技术分享 2025-03-18 16:04 0


部分主题的精选图片将显示在文章的内页。 在大多数情况下,我们不希望它显示在内页中,因此我们可以添加一个选项来控制显示。 代码显示如下:

function prefix_featured_image_meta( $content ) {

global $post;

$text = __( 'Don't display image in post.', 'prefix' );

$id = 'hide_featured_image';

$value = esc_attr( get_post_meta( $post->ID, $id, true ) );

$label="<label for="" . $id . '" class="selectit"><input name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $value . ' "'. checked( $value, 1, false) .'> ' . $text .'</label>';

return $content .= $label;

}

add_filter( 'admin_post_thumbnail_html', 'prefix_featured_image_meta' );

function prefix_save_featured_image_meta( $post_id, $post, $update ) {

$value = 0;

if ( isset( $_REQUEST['hide_featured_image'] ) ) {

$value = 1;

}

// Set meta value to either 1 or 0

update_post_meta( $post_id, 'hide_featured_image', $value );

}

add_action( 'save_post', 'prefix_save_featured_image_meta', 10, 3 );

只需将上述代码添加到functions.php 中即可。

然后在single.php中需要判断文章的hide_featured_image字段是否为1(隐藏),剩下的功能代码可以根据自己的主题修改。

标签: WordPress 教程

提交需求或反馈

Demand feedback