我正在使用ACF插件创建静态(非电子商务)产品页面。
我现在正在制作一个页面,其中列出了所有带有产品名称的子页面,以及添加到product_徽标自定义字段中的图像。我发现一些代码几乎可以实现我想要的一切:
<?php
//get children of page 241 and display with custom fields
$args=array(
\'post_parent\' => 825,
\'post_type\' => \'page\',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<!-- here I get the \'product_short_description\' custom field\'s data, assign it to the $meta_one variable and echo it out !-->
<?php $meta_one = get_post_meta($post->ID, \'product_short_description\', true); ?>
<span><?php echo $meta_one; ?></span>
<!-- here I get the \'product_logo\' custom field\'s data, assign it to the $meta_two variable and echo it out !-->
<?php $meta_two = get_post_meta($post->ID, \'product_logo\', true); ?>
<span><img src="<?php echo $meta_two; ?>" /></span>
</a>
</div>
<?php endwhile; } ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post().?>
第二个字段$meta\\u two获取包含图像ID的product\\u徽标字段。我想使用以下方法将其转换为URL:
<?php wp_get_attachment_image_src($meta_two, null, true); ?>
但这只是给了我一个空白的URL。
完整代码如下:
$args=array(
\'post_parent\' => 825,
\'post_type\' => \'page\',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<!-- here I get the \'product_short_description\' custom field\'s data, assign it to the $meta_one variable and echo it out !-->
<?php $meta_one = get_post_meta($post->ID, \'product_short_description\', true); ?>
<span><?php echo $meta_one; ?></span>
<!-- here I get the \'product_logo\' custom field\'s data, assign it to the $meta_two variable and echo it out !-->
<?php $meta_two = get_post_meta($post->ID, \'product_logo\', true); ?>
<span><img src="<?php wp_get_attachment_image_src($meta_two, null, true); ?> " /></span>
</a>
</div>
<?php endwhile; } ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post().?>
有人能指出我做错了什么吗?