【问题标题】:I am trying to covert this normal code to foreach:我正在尝试将此普通代码转换为 foreach:
【发布时间】:2016-08-11 18:49:59
【问题描述】:

我使用自定义元框创建了 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>
}

并且需要输出多张上传的图片。

【问题讨论】:

  • @MarcB 他发布了他的原始代码,他发布了他的尝试(在 foreach 循环中)。虽然不是一个好问题,但我认为这有点粗鲁。

标签: php wordpress gallery meta


【解决方案1】:

这是最简单的方法:

<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>

【讨论】:

  • 感谢您的回答,应该可以,但是可以以不同的方式管理阵列。如果我们按顺序有这样的数组。 mytheme_image_1','mytheme_image_2','mytheme_image_3
  • 什么?在原始问题中准确说明您想要什么
  • 您的解决方案运行良好,但我们可以缩短数组吗?确实是_one、_tow、_three等。
  • 我已经更新了我的答案。如果它对您有用,请务必接受答案。
【解决方案2】:
<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 个数组,而不是使用多个数组。有可能做这样的事情吗? foreach (array('one', 'two', 'three', 'four', 'five' and more) as $theme ) foreach (array('one+1') as $theme) 还是这样的?

【讨论】:

    猜你喜欢
    • 2020-11-26
    • 2021-08-16
    • 2012-08-28
    • 1970-01-01
    • 2019-05-28
    • 2019-10-26
    • 2022-11-12
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多