>本教程演示瞭如何在WordPress帖子屏幕上添加特色圖像列,並將此功能擴展到自定義帖子類型。 讓我們簡化解釋並提高清晰度。
>步驟1:啟用特色圖像
>首先,確保您的主題支持特色圖像。 打開您的主題的functions.php
文件(如果不存在,則創建它)並添加以下代碼:
add_theme_support( 'post-thumbnails' ); add_image_size( 'featured_preview', 200, 150, false );
此啟用具有特色圖像支持並創建一個自定義的縮略圖大小(featured_preview
),用於在新列中預覽圖像。 false
的參數阻止了裁剪。
(圖像:WordPress Post Editor中的特色圖像設置)
>步驟2:將特色圖像列添加到帖子
接下來,添加一個自定義列以在WordPress帖子列表中顯示特色圖像。 將這些函數添加到您的functions.php
文件:
function st4_add_featured_image_column( $defaults ) { $defaults['featured_image'] = __( 'Featured Image' ); return $defaults; } function st4_show_featured_image_column( $column_name, $post_ID ) { if ( $column_name == 'featured_image' ) { $featured_image = get_the_post_thumbnail( $post_ID, 'featured_preview' ); if ( $featured_image ) { echo $featured_image; } else { echo '<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174054013775854.jpg" class="lazy" alt="Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen " /> <em>(Image: Posts list with new Featured Image column)</em></p> <p>You can show/hide this column via the <strong>Screen Options</strong> panel.</p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174054013887569.jpg" class="lazy" alt="Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen " /> <em>(Image: Screen Options panel)</em></p> <p><strong>Step 3: Extend to Custom Post Types</strong></p> <p>To add the featured image column to custom post types, simply replace <code>manage_posts_columns</code> and <code>manage_posts_custom_column</code> with the appropriate custom post type hooks. For example, for a custom post type named "movies":</p> <pre class="brush:php;toolbar:false"><code class="language-php">add_filter( 'manage_movie_posts_columns', 'st4_add_featured_image_column' ); add_action( 'manage_movie_posts_custom_column', 'st4_show_featured_image_column', 10, 2 );
(圖片:電影帖子中的特色圖像)
(圖片:電影自定義帖子類型中的特色圖像列)>
>進一步的考慮(簡短):
>>針對特定的帖子類型:
manage_{post_type}_posts_columns
>函數輕鬆添加多個自定義列。
manage_{post_type}_posts_custom_column
>st4_add_featured_image_column
這種修訂後的響應提供了更簡潔,更集中的解釋,同時維護基本信息和圖像。 該代碼也得到了改進,以提高可讀性和效率。 st4_show_featured_image_column
>以上是在WordPress帖子和自定義帖子類型管理屏幕中添加自定義列的詳細內容。更多資訊請關注PHP中文網其他相關文章!