检索主题中自定义字段的图像 URL
Retrive Image URL of Custom Fields in Theme
我已经为自定义 Post 类型创建了自定义字段。当我检索返回 22 的主题模板图像值中的值时。
如何获取图像 URL.?
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'projects',
'post_status' => 'publish',
);
$projects = get_posts( $args );
foreach ($projects as $project) {
echo $project->project_title; // Title Text
echo $project->project_description; // Description Content
echo $project->project_image; // 22
} ?>
当我打印 $project
WP_Post Object
(
[ID] => 35
[post_author] => 1
[post_date] => 2017-04-07 05:50:29
[post_date_gmt] => 2017-04-07 05:50:29
[post_content] => Tekzenit
[post_title] => Tekzenit
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => tekzenit
[to_ping] =>
[pinged] =>
[post_modified] => 2017-04-07 06:12:54
[post_modified_gmt] => 2017-04-07 06:12:54
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://10.180.82.8/fareed/?post_type=projects&p=35
[menu_order] => 0
[post_type] => projects
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
如果您使用高级自定义字段或自己编写代码,有关如何创建自定义字段的更多信息会有所帮助。
但如果您有图像 ID,则可以使用:
$img_id = $project->project_image;
$img_array = wp_get_attachment_image_src($img_id, 'full' );
$img_url = $img_array[0];
echo $img_url;
$project->project_image
returns id
您可以使用 wp_get_attachment_image_url
函数来获取图像 URL
if($project->project_image){
$imagePath = wp_get_attachment_image_url( $project->project_image, '' );
echo imagePath
}
我已经为自定义 Post 类型创建了自定义字段。当我检索返回 22 的主题模板图像值中的值时。 如何获取图像 URL.?
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'projects',
'post_status' => 'publish',
);
$projects = get_posts( $args );
foreach ($projects as $project) {
echo $project->project_title; // Title Text
echo $project->project_description; // Description Content
echo $project->project_image; // 22
} ?>
当我打印 $project
WP_Post Object
(
[ID] => 35
[post_author] => 1
[post_date] => 2017-04-07 05:50:29
[post_date_gmt] => 2017-04-07 05:50:29
[post_content] => Tekzenit
[post_title] => Tekzenit
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => tekzenit
[to_ping] =>
[pinged] =>
[post_modified] => 2017-04-07 06:12:54
[post_modified_gmt] => 2017-04-07 06:12:54
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://10.180.82.8/fareed/?post_type=projects&p=35
[menu_order] => 0
[post_type] => projects
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
如果您使用高级自定义字段或自己编写代码,有关如何创建自定义字段的更多信息会有所帮助。
但如果您有图像 ID,则可以使用:
$img_id = $project->project_image;
$img_array = wp_get_attachment_image_src($img_id, 'full' );
$img_url = $img_array[0];
echo $img_url;
$project->project_image
returns id
您可以使用 wp_get_attachment_image_url
函数来获取图像 URL
if($project->project_image){
$imagePath = wp_get_attachment_image_url( $project->project_image, '' );
echo imagePath
}