0

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

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

我从这里得到了这个代码:将特色图像缩略图添加到 WordPress 管理列

我的文件中的以下代码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;
  }
}
4

3 回答 3

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);`
于 2014-06-09T08:03:18.103 回答
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 );
}

感谢您访问我的网站并尝试我的片段:)

于 2011-11-25T06:32:07.530 回答
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() 参数中作为“支持”并在 add_theme_support() 调用中显式声明。

享受您的特色图片!

于 2013-09-04T20:21:30.890 回答