WordPress 高级自定义字段 Img Src(未知)
WordPress Advanced Custom Fields Img Src(Unknown)
我有一个标题为 "Row" 的自定义字段,其中包含图像和文本。
我确实从过去的主题中复制了相同的代码,但由于某种原因,图像在我的新主题中显示不正确。这是我在 page.php:
中的代码片段
$aboutPagePosts = new WP_Query(array(
'Posts_per_page' => 5,
'post_type' => 'row',
'category_name' => 'about'));
$image = get_field('image');
?>
<div class="page-section container">
<?php
while($aboutPagePosts->have_posts()) {
$aboutPagePosts->the_post(); ?>
<h2><?php echo $pageTitle ?></h2>
<div class="left-image-row">
<img src="<?php echo $image['url'] ?>" alt="">
<div class="section-text">
<?php the_field('text'); ?>
</div>
</div>
<?php } ?>
</div>
页面正在显示该行,因为我在应该显示的位置看到了字段文本。但是,当我检查页面时,我在 html.
中看到 img src(unknown)
有人知道会发生什么吗?
看来您可能需要将图像调用移动到 while
循环内部,如下所示:
$aboutPagePosts = new WP_Query(array(
'posts_per_page' => 5,
'post_type' => 'row',
'category_name' => 'about'
));
?>
<div class="page-section container">
<?php while($aboutPagePosts->have_posts()) : $aboutPagePosts->the_post(); ?>
<h2><?php echo $pageTitle ?></h2>
<div class="left-image-row">
<?php $image = get_field('image'); ?>
<img src="<?php echo $image; ?>" alt="">
<div class="section-text">
<?php the_field('text'); ?>
</div>
</div>
<?php endwhile; wp_reset_postdata(); /* make sure to reset post data */ ?>
</div>
另外Posts_per_page
应该全部小写posts_per_page
。
我有一个标题为 "Row" 的自定义字段,其中包含图像和文本。
我确实从过去的主题中复制了相同的代码,但由于某种原因,图像在我的新主题中显示不正确。这是我在 page.php:
中的代码片段$aboutPagePosts = new WP_Query(array(
'Posts_per_page' => 5,
'post_type' => 'row',
'category_name' => 'about'));
$image = get_field('image');
?>
<div class="page-section container">
<?php
while($aboutPagePosts->have_posts()) {
$aboutPagePosts->the_post(); ?>
<h2><?php echo $pageTitle ?></h2>
<div class="left-image-row">
<img src="<?php echo $image['url'] ?>" alt="">
<div class="section-text">
<?php the_field('text'); ?>
</div>
</div>
<?php } ?>
</div>
页面正在显示该行,因为我在应该显示的位置看到了字段文本。但是,当我检查页面时,我在 html.
中看到img src(unknown)
有人知道会发生什么吗?
看来您可能需要将图像调用移动到 while
循环内部,如下所示:
$aboutPagePosts = new WP_Query(array(
'posts_per_page' => 5,
'post_type' => 'row',
'category_name' => 'about'
));
?>
<div class="page-section container">
<?php while($aboutPagePosts->have_posts()) : $aboutPagePosts->the_post(); ?>
<h2><?php echo $pageTitle ?></h2>
<div class="left-image-row">
<?php $image = get_field('image'); ?>
<img src="<?php echo $image; ?>" alt="">
<div class="section-text">
<?php the_field('text'); ?>
</div>
</div>
<?php endwhile; wp_reset_postdata(); /* make sure to reset post data */ ?>
</div>
另外Posts_per_page
应该全部小写posts_per_page
。