我使用自定义元数据库创建了wordpress库,我的代码根据需要提供了正确的输出。 但试图以标准方式制作它。
原始代码
$mytheme_gallery_one = get_post_meta( get_the_ID(), 'mytheme_image_one', true );
$mytheme_image_one_image = wp_get_attachment_image( $mytheme_gallery_one, 'large' );
$mytheme_image_one_url = wp_get_attachment_url( $mytheme_gallery_one );
$mytheme_gallery_two = get_post_meta( get_the_ID(), 'mytheme_image_two', true );
$mytheme_image_two_image = wp_get_attachment_image( $mytheme_gallery_two, 'large' );
$mytheme_image_two_url = wp_get_attachment_url( $mytheme_gallery_two );
$mytheme_gallery_three = get_post_meta( get_the_ID(), 'mytheme_image_three', true );
$mytheme_image_three_image = wp_get_attachment_image( $mytheme_gallery_three, 'large' );
$mytheme_image_three_url = wp_get_attachment_url( $mytheme_gallery_three );
?>
<figure class="fit-vid ed-youtube">
<a href="<?php echo $mytheme_image_one_url; ?>" ><?php echo $mytheme_image_one_image; ?></a>
<a href="<?php echo $mytheme_image_two_url; ?>" ><?php echo $mytheme_image_two_image; ?></a>
<a href="<?php echo $mytheme_image_two_url; ?>" ><?php echo $mytheme_image_two_image; ?></a>
</fiture>
我正在尝试使用 foreach :
缩短版本foreach {
my_theme_gallery;
my_theme_image;
my_theme_url
<figure class="fit-vid ed-youtube">
<a href="<?php echo $mytheme_image_one_url; ?>" ><?php echo $mytheme_image_one_image; ?></a>
</fiture>
}
并需要输出上传的多张图片。
答案 0 :(得分:0)
这是最简单的方法:
<figure class="fit-vid ed-youtube">
<?php
foreach ( array('one', 'two', 'three') as $theme )
{
$mytheme_gallery = get_post_meta( get_the_ID(), 'mytheme_image_' . $theme, true );
$mytheme_image = wp_get_attachment_image( $mytheme_gallery, 'large' );
$mytheme_image_url = wp_get_attachment_url( $mytheme_gallery );
echo '<a href="', $mytheme_image_url, '" >', $mytheme_image, '</a>';
}
?>
</figure>
答案 1 :(得分:0)
<figure class="fit-vid ed-youtube">
<?php
**foreach ( array('one', 'two', 'three') as $theme )**
{
$mytheme_gallery = get_post_meta( get_the_ID(), 'mytheme_image_' . $theme, true );
$mytheme_image = wp_get_attachment_image( $mytheme_gallery, 'large' );
$mytheme_image_url = wp_get_attachment_url( $mytheme_gallery );
echo '<a href="', $mytheme_image_url, '" >', $mytheme_image, '</a>';
}
?>
</figure>
如果我们有超过20个阵列,那么而不是使用多个arryas。有可能做出这样的事吗? foreach(数组(&#39;一个&#39;,&#39;两个&#39;,&#39;三个&#39;,&#39;四个&#39;,&#39;五&#39; ;以及更多)作为$ theme) foreach(数组(&#39;一个+ 1&#39;)作为$ theme) 还是活着的?