【问题标题】:WordPress: Remove Featured Image From Specific PageWordPress:从特定页面中删除特色图片
【发布时间】:2016-02-05 08:25:26
【问题描述】:

我能够从自定义帖子类型的页面中删除特色图像元框。以下是我使用的:

add_action('do_meta_boxes', 'remove_thumbnail_box');
function remove_thumbnail_box() {
  remove_meta_box( 'postimagediv','page','side' );
}

但是,我真正想做的是将其仅应用于特定的页面模板。这可能吗?

谢谢!

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案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】:

      对于希望以编程方式删除特色图像支持的其他人(cmets 应该是不言自明的):

      /**
      *   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' );
      

      调整要排除的页面数组后,您需要将此 sn-p 添加到您的主题 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' );
      

      【讨论】:

        【解决方案3】:

        还有另一种解决方案。通过编辑page.php

        如果您的特征图像 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}?>
        

        【讨论】:

        • 隐藏与删除不同,这是 OP 要求的。
        猜你喜欢
        • 2011-07-05
        • 2014-09-13
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 2017-11-27
        • 2023-03-17
        • 1970-01-01
        相关资源
        最近更新 更多