我能够从自定义帖子类型的网页中删除精选图片元数据。以下是我使用的内容:
add_action('do_meta_boxes', 'remove_thumbnail_box');
function remove_thumbnail_box() {
remove_meta_box( 'postimagediv','page','side' );
}
但是,我真正想要做的只是将其应用于特定的页面模板。这可能吗?
谢谢!
答案 0 :(得分:2)
啊,我找到了解决方案。
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file === 'page-template-name.php') {
add_action('do_meta_boxes', 'remove_thumbnail_box');
function remove_thumbnail_box() {
remove_meta_box( 'postimagediv','page','side' );
}
}
如果有更好的解决方案......请不要犹豫发布。
谢谢!
答案 1 :(得分:1)
对于任何想要以编程方式删除特色图片支持的人(评论应该是自我解释的):
/**
* Removes the featured image metabox from specific pages (by ID)
* @note: populate the $excluded_page_ids_array array with page IDs
*/
function eh_remove_featured_image_metabox_from_specific_pages() {
// populate with page IDs to exclude featured image metabox from
$excluded_page_ids_array = array( 38, 29 );
// store the current post ID
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
// check if the current page is in our excluded array
if( in_array( $post_id, $excluded_page_ids_array ) ) {
// remove featured image metabox
remove_meta_box( 'postimagediv','page','side' );
}
}
add_action( 'do_meta_boxes', 'remove_featured_image_metabox_from_specific_pages' );
调整要排除的网页数组后,您需要将此代码段添加到主题functions.php文件中。
或者,您可以从所有页面中删除精选图像,但指定的页面除外 - 特色图像元数据将保留在其中。
/**
* Removes the featured image metabox from all pages except specified ones
* @note: populate the $page_ids array with page IDs
*/
function eh_remove_featured_images_metabox_from_all_pages_except_specified() {
// populate with page IDs to keep the featured image metabox on
$page_ids = array( 25, 31 );
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
if( ! in_array( $post_id, $page_ids ) ) {
// remove featured image metabox
remove_meta_box( 'postimagediv','page','side' );
}
}
add_action( 'do_meta_boxes', 'eh_remove_featured_images_metabox_from_all_pages_except_specified' );
答案 2 :(得分:0)
还有另一种解决方案。通过编辑page.php
如果你的功能Image div如下所示
<div class="thumbnail">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
</div>
你可以使用
if(is_page(array(42,'about-me','Contact')); //当显示的页面是帖子ID:42或post_name_slug“about-me”或post_title“Contact”时返回true。
<?php
if (is_page( array( 42, 'about-me', 'Contact' ) ) ) {
?>
<div class="thumbnail" style="display:none;">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
</div>
<?php
}else{
?>
<div class="thumbnail">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
</div>
<?php}?>