我能够从页面的自定义帖子类型中删除精选图片元框。下面是我使用的:
add_action('do_meta_boxes', 'remove_thumbnail_box'); function remove_thumbnail_box() { remove_meta_box( 'postimagediv', 'page', 'side' ); } 但是, 我真正想做的只是将其应用于特定的页面模板。这可能吗?
谢谢!
#1
啊, 我找到了解决方案。
$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' ); } } 如果有更好的解决方案…请不要犹豫。
谢谢!
#2
对于希望以编程方式删除特色图像支持的其他任何人(注释应为自我解释):
/** * 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' ); 调整要排除的页面数组后, 你需要将此代码段添加到themes.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' ); #3
还有另一种解决方案。通过编辑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’)); //当显示的页面是post 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}?>
srcmini
评论前必须登录!
注册