我需要一些帮助,以获得一个横幅图像分配给一个页面自定义帖子类型。每页只有一个图像,每页都有不同的横幅图像,只需在img
标签
我在我的函数中尝试过这个。php:
function sixohthree_init() {
$labels = array(
\'name\' => \'Header Images\',
\'singular_name\' => \'Header Image\',
\'add_new_item\' => \'Add Header Image\',
\'edit_item\' => \'Edit Header Image\',
\'new_item\' => \'New Header Image\',
\'view_item\' => \'View Header Image\',
\'search_items\' => \'Search Header Images\',
\'not_found\' => \'No Header Images found\',
\'not_found_in_trash\' => \'No Header Images found in Trash\'
);
$args = array(
\'labels\' => $labels,
\'public\' => false,
\'show_ui\' => true,
\'supports\' => array(\'thumbnail\')
);
register_post_type( \'header-image\', $args );
}
add_action( \'init\', \'sixohthree_init\' );
/**
* Modify which columns display when the admin views a list of header-image posts.
*/
function sixohthree_headerimage_posts_columns( $posts_columns ) {
$tmp = array();
foreach( $posts_columns as $key => $value ) {
if( $key == \'title\' ) {
$tmp[\'header-image\'] = \'Header Image\';
} else {
$tmp[$key] = $value;
}
}
return $tmp;
}
add_filter( \'manage_header-image_posts_columns\', \'sixohthree_headerimage_posts_columns\' );
/**
* Custom column output when admin is view the header-image post list.
*/
function sixohthree_headerimage_custom_column( $column_name ) {
global $post;
if( $column_name == \'header-image\' ) {
echo "<a href=\'", get_edit_post_link( $post->ID ), "\'>", get_the_post_thumbnail( $post->ID ), "</a>";
}
}
add_action( \'manage_posts_custom_column\', \'sixohthree_headerimage_custom_column\' );
/**
* Make the "Featured Image" metabox front and center when editing a header-image post.
*/
function sixohthree_headerimage_metaboxes( $post ) {
global $wp_meta_boxes;
remove_meta_box(\'postimagediv\', \'header-image\', \'side\');
add_meta_box(\'postimagediv\', __(\'Featured Image\'), \'post_thumbnail_meta_box\', \'header-image\', \'normal\', \'high\');
}
add_action( \'add_meta_boxes_header-image\', \'sixohthree_headerimage_metaboxes\' );
/**
* Enable thumbnail support in the theme, and set the thumbnail size.
*/
function sixohthree_after_setup() {
add_theme_support( \'post-thumbnails\' );
set_post_thumbnail_size(150, 100, true);
}
add_action( \'after_setup_theme\', \'sixohthree_after_setup\' );
我的问题是,我需要能够为一个页面分配一个标题图像,或者至少为每个页面提取一个特定的图像
最合适的回答,由SO网友:Prasad Nevase 整理而成
这可以通过使用Advance Custom Field (ACF)插件。在您的标题图像帖子类型中,我建议您也添加对标题的支持,以便您可以轻松识别列表中的标题图像。为此,您只需更新
\'supports\' => array(\'title\',\'thumbnail\')
至于您的主要问题是为每页指定一个标题图像;安装ACF插件后,创建字段组和其中的字段。请参考下面的截图,这将给你确切的想法。
然后在“编辑页面”上,您将看到一个“页眉图像”元框,如以下屏幕截图所示:
如果您按照第一个ACF屏幕截图中的设置进行操作,那么上面的元框将只允许每页有一个图像。完成所有这些操作后,可以使用以下代码检索分配给页面的标题图像:
$page_header_img = get_post_meta(get_the_ID(), \'page_header_image\' ,true);
echo get_the_post_thumbnail($page_header_img[0]);
我希望这有帮助。