【问题标题】:Featured Image column of admin for custom post types not working自定义帖子类型的管理员特色图片列不起作用
【发布时间】:2011-12-22 11:41:31
【问题描述】:

我已将代码添加到我的 function.php 文件中,以将帖子的特色图片添加到我的管理列。它适用于帖子和页面,但对于我的两种自定义帖子类型(汽车、车轮),它对管理布局没有任何作用。

有人可以帮我解决这个问题吗?我需要为每个自定义添加过滤器吗?

我从这里得到这个代码:Add featured image thumbnail to WordPress admin columns

以下代码在我的function.php 文件中:

// Add the posts and pages columns filter. They can both use the same function.
add_filter('manage_posts_columns', 'tcb_add_post_thumbnail_column', 5);
add_filter('manage_pages_columns', 'tcb_add_post_thumbnail_column', 5);
add_filter('manage_custom_post_columns', 'tcb_add_post_thumbnail_column', 5);

// Add the column
function tcb_add_post_thumbnail_column($cols){
  $cols['tcb_post_thumb'] = __('FeaTured');
  return $cols;
}

// Hook into the posts an pages column managing. Sharing function callback again.
add_action('manage_posts_custom_column', 'tcb_display_post_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'tcb_display_post_thumbnail_column', 5, 2);
    add_action('manage_custom_post_column', 'tcb_display_post_thumbnail_column', 5, 2);

// Grab featured-thumbnail size post thumbnail and display it.
function tcb_display_post_thumbnail_column($col, $id){
  switch($col){
    case 'tcb_post_thumb':
      if( function_exists('the_post_thumbnail') )
        echo the_post_thumbnail( 'admin-list-thumb' );
      else
        echo 'Not supported in theme';
      break;
  }
}

【问题讨论】:

    标签: wordpress image featured


    【解决方案1】:

    我为我的画廊帖子类型创建的以下代码可以 100% 工作,您可以将画廊更改为您的帖子类型名称。

    首先添加缩略图支持。和预览的图像大小

    add_theme_support( 'post-thumbnails' ); add_image_size( 'gallery-post-prev', 50, 50, true );

    然后设置缩略图。

    现在在你的functions.php中创建一个函数来获得特色的imgae

    /** * get featured image function */ function gallery_featured_image($post_ID) { $post_thumbnail_id = get_post_thumbnail_id($post_ID); if ($post_thumbnail_id) { $post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'gallery-post-prev'); return $post_thumbnail_img[0]; } }

    现在创建一个列标题,这是列的标题,在我们的例子中是“特色图片”

    /** * add column heading */ function gallery_columns_head($defaults) { $defaults['featured_image'] = 'Featured Image'; return $defaults; }

    现在创建栏目内容,在我们的例子中,我们将在栏目中显示特色图片。

    /** * show featured image in column */ function gallery_columns_content($column_name, $post_ID) { if ($column_name == 'featured_image') { $post_featured_image = gallery_featured_image($post_ID); if ($post_featured_image) { echo '<img src="' . $post_featured_image . '" />'; } } }

    现在添加过滤器以显示我们创建的列标题

    add_filter('manage_gallery_posts_columns', 'gallery_columns_head', 10); 并添加动作挂钩以在列内容中显示特色图像。

    `add_action('manage_gallery_posts_custom_column', 'gallery_columns_content', 10, 2);`
    

    【讨论】:

      【解决方案2】:

      我正在使用自定义帖子类型实现自定义主题,发现我需要添加对帖子类型的支持并在主题中声明它。如下:

      register_post_type( 'team',
          array(
              'labels' => array(
                  'name' => __( 'Team Members' ),
                  'singular_name' => __( 'Team Member' )
              ),
              'public' => true,
              'has_archive' => true,
              'supports'    => array( 'title', 'editor', 'thumbnail' ),
          )
      );
      add_theme_support( 'post-thumbnails', array( 'team' ) );
      

      请注意,它在 register_post_type() 参数中作为“支持”并在 add_theme_support() 调用中明确声明。

      欣赏您的精选图片!

      【讨论】:

        【解决方案3】:

        您是否在帖子类型中启用了缩略图支持?

        例如,我的 wp-glossary 插件注册了一个具有帖子功能(默认)和启用缩略图的词汇表帖子类型,它开箱即用:

        add_action('init', 'tcb_glossary_register_posttype_glossary');
        function tcb_glossary_register_posttype_glossary() {
          register_post_type( 'glossary',
            array(
              'labels' => array(
                'name'               => __( 'Glossary Terms' ),
                'singular_name'      => __( 'Glossary Term' ),
                'add_new'            => __( 'Add New Term' ),
                'add_new_item'       => __( 'Add New Glossary Term' ),
                'edit_item'          => __( 'Edit Glossary Term' ),
                'new_item'           => __( 'Add New Glossary Term' ),
                'view_item'          => __( 'View Glossary Term' ),
                'search_items'       => __( 'Search Glossary Terms' ),
                'not_found'          => __( 'No Glossary Terms found' ),
                'not_found_in_trash' => __( 'No Glossary Terms found in trash' )
              ),
              'public'               => true,
              'menu_position'        => 105,
              'supports'             => array( 'title', 'editor', 'thumbnail' ),
              'has_archive'          => true,
            )
          );
          flush_rewrite_rules( false );
        }
        

        感谢您访问我的网站并尝试我的 sn-p :)

        【讨论】:

        • 我在互联网上搜索并找不到它-感谢这个sn-p!
        猜你喜欢
        • 1970-01-01
        • 2014-04-20
        • 2016-01-10
        • 2015-04-03
        • 2021-12-26
        • 2013-10-27
        • 1970-01-01
        • 2018-07-18
        • 1970-01-01
        相关资源
        最近更新 更多