管理员的特色图片列,用于自定义帖子类型无效

时间:2011-11-05 01:46:46

标签: wordpress image featured

我已将代码添加到我的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;
  }
}

3 个答案:

答案 0 :(得分:1)

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

例如,我的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 );
}

感谢您访问我的网站并尝试使用我的代码:)

答案 1 :(得分: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 :(得分:0)

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

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()参数中为“supports”,并在add_theme_support()调用中显式声明。

欣赏精选图片!