我正在尝试添加一个条件,如果未填充“image”字段,该条件将显示默认图像。。。以下是我想要解决的问题:
$args = array(
\'posts_per_page\' => 3,
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'is_featured\',
\'value\' => \'1\',
\'compare\' => \'!=\', // NOT Featured
)
)
);
$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
while( $second_query->have_posts() ) : $second_query->the_post();
$attachment_id = get_field(\'image\');
$size = "customfeatins"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
echo \'<article>
<img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="136" height="90" />
<h3>\' . get_the_title() .\'</h3>
<p class="date">\' . get_the_date() .\'</p>
</article>
\';
endwhile;
endif;
wp_reset_postdata();
我尝试过这个,但它打破了这一页:
$args = array(
\'posts_per_page\' => 3,
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'is_featured\',
\'value\' => \'1\',
\'compare\' => \'!=\', // NOT Featured
)
)
);
$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
while( $second_query->have_posts() ) : $second_query->the_post();
if( get_field(\'image\') ){
$attachment_id = get_field(\'image\');
$size = "customfeatins"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
} else {
$image = wp_get_attachment_image_src( \'http://placehold.it/136x90\' );
}
endif;
echo \'<article>
<img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="136" height="90" />
<h3>\' . get_the_title() .\'</h3>
<p class="date">\' . get_the_date() .\'</p>
</article>
\';
endwhile;
endif;
wp_reset_postdata();
最合适的回答,由SO网友:ByteMyPixel 整理而成
我终于做到了这一点-感谢大家的反馈,我能够重新思考一些事情,并找到一个解决方案,这正是我想要的:)
这里是(也可以通过过滤标签获得相关帖子):
<?php
$tags = wp_get_post_tags($post->ID);
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args = array(
\'tag__in\' => $tag_ids,
\'post_type\' => \'post\',
\'post__not_in\' => array($post->ID),
\'posts_per_page\' => 3, // Number of related posts that will be shown.
\'caller_get_posts\'=> 1
);
$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
echo \'<div class="title fs-60 salmon">Related Posts</div>\';
while( $second_query->have_posts() ) : $second_query->the_post();
$titlechars = 45; // Character Limit
$posttitle = get_the_title();
$modtitle = substr($posttitle, 0, $titlechars);
$contentchars = 120; // Character Limit
$postcontent = get_the_excerpt();
$modcontent = substr($postcontent, 0, $contentchars);
echo \'<article>\';
?>
<?php
if( get_field(\'image\') ):
$attachment_id = get_field(\'image\');
$size = \'customfeatins\'; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
echo \'<img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="136" height="90" />\';
?>
<?php else : ?>
<img src="http://placehold.it/136x90" alt="image" />
<?php endif; ?>
<?php
echo \'
<h3 class="purple">\' . $modtitle .\'</h3>
<p class="date">\' . get_the_date() .\'</p>
<p>\' . $modcontent . \'… <a href="\' . get_permalink() . \'">More ›</a></p>
</article>\';
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
如果帖子的“自定义图像”字段中没有图像,则会使用占位符图像(暂时设置为placehold.it)。
SO网友:Chip Bennett
我很确定这行不通:
$image = wp_get_attachment_image_src( \'http://placehold.it/136x90\' );
作为
wp_get_attachment_image_src( $id )
需要作为整数传递的附件ID
$id
.
如果主题中有回退默认图像,可以这样调用它:
$image = get_template_directory_uri() . \'/images/fallback.png\';
或者,如果希望用户从媒体库中选择要使用的图像,则需要通过以下方式查询该图像
$id
. 如何做到这一点将取决于存储用户选择的方式。
编辑(Edit)
后退功能几乎可以正常工作,但它只输出源中的第一个字符,例如:
src="/"
这是因为在一种情况下,您在$image
, 在回退中,您将在$image
. 但在输出中,您引用$image[0]
, 这意味着$image
是一个数组。
我建议你通过$image[0]
可能是一个单独的变量$image_src
. 然后,您可以参考$image_src
在回退情况下,然后也是输出$image_src
.
对于example:
if( get_field(\'image\') ){
// stuff
$image = wp_get_attachment_image_src( $attachment_id, $size );
// ADD THIS
$image_src = $image[0];
} else {
// USE $image_src HERE AS WELL
$image_src = get_template_directory_uri() . \'/images/fallback.png\';
}
// more stuff
// ...skipping ahead
// ...
// USE $image_src HERE AS WELL
<img src="\' . $image_src . \'" alt="\' . get_the_title() .\'" width="136" height="90" />