>本教程演示了如何在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中文网其他相关文章!