2011-11-05 44 views
0

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

回答

1

是否啓用在後型縮略圖支持?

例如,我的WP-詞彙插件註冊了具有職位啓用功能(默認)和縮略圖的詞彙崗位類型,和它的作品開箱:

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); 
} 

感謝您訪問我的網站,並試圖我的片段:)

+0

我搜索了互聯網,但無法找到它 - 感謝這段代碼! – brendan

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()調用顯式聲明。

享受您的精選圖像!

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);`