【问题标题】:Variable outside foreach loop phpforeach循环php外的变量
【发布时间】:2023-04-06 13:26:02
【问题描述】:

我遇到了问题。

在 Wordpress 上,我定义了一个 foreach 循环,以从附加图像中获取一些信息,例如标题、描述等...

在这个循环之外,我想调用各种变量的结果。如何在我的循环之外获得$attachment_description$attachment_caption$attachment_title

感谢您的帮助。代码下方:

if ( $attachments ) : 

echo $before;
    foreach( $attachments as $ID ){
        $attachment = get_post($ID);
        $attachment_description = $attachment->post_content; //Display description
        $attachment_caption = $attachment->post_excerpt; //Display caption
        $attachment_title = $attachment->post_title; //Display title

        //IF : Affichage sur certaines images
        //if($attachment_description != ''){
        //echo '<div class="item">' . wp_get_attachment_image( $attachment->ID, 'portfolioslider' ) . '<div class="captionportfolio"><h1 class="titrecaption">' . $attachment_title . '</h1>' . $attachment_description . '</div></div>';
        //}// else {
        echo '<div class="item">' . wp_get_attachment_image( $attachment->ID, 'portfolioslider' ) . '</div>';
        //}
    }
echo $after;

endif;

似乎没有任何效果,这是第一个带有循环的文件:

<?php 
/**
 * Make sure we can access post data easily.
 */
global $post;

/**
 * Setup variables needed for the gallery
 */
$attachments = get_post_meta( $post->ID, '_ebor_portfolio_gallery_list', true );
$type = get_post_meta( $post->ID, '_ebor_gallery_format', true );
$before = '<div class="owl-carousel portfolio-slider custom-controls">';
$after = '</div>';

if( $type == 'carousel' ){
$before = '<div class="carousel-gallery-wrapper"><div class="carousel-gallery custom-controls">';
$after = '</div></div>';
}

/**
 * If we found items, output the gallery.
  * $before and $after change depending on the gallery chosen.
 */
if ( $attachments ) : 

echo $before;

$arrayInformation = array();

    foreach( $attachments as $ID ){
        $attachment = get_post($ID);
        $attachment_description = $attachment->post_content; //Afficher la description
        $attachment_caption = $attachment->post_excerpt; //Afficher le caption
        $attachment_title = $attachment->post_title; //Afficher le titre

        $arrayInformation[$ID] = array("title" =>$attachment_title, "caption" =>$attachment_caption, "description" =>$attachment_description);

        //IF : Affichage sur certaines images
        //if($attachment_description != ''){
        //echo '<div class="item">' . wp_get_attachment_image( $attachment->ID, 'portfolioslider' ) . '<div class="captionportfolio"><h1 class="titrecaption">' . $attachment_title . '</h1>' . $attachment_description . '</div></div>';
        //}// else {
        echo '<div class="item">' . wp_get_attachment_image( $attachment->ID, 'portfolioslider' ) . '</div>';
        //}
    }
echo $after;

endif;

我想将变量的内容调用到这个文件中:

<div class="container inner">

<?php get_template_part('postformats/format', get_post_format()); ?>

<div class="divide30"></div>

<?php 

global $post;

$titrecat = get_the_title();
if( has_term( 'creation', 'portfolio-category' ) ) {
echo '<p><span class="portfolio-title">Création - </span>' . $titrecat . '</p>'; 
print $arrayInformation[$ID]["description"];
}
else if( has_term( 'realisation', 'portfolio-category' ) ) {
echo '<p><span class="portfolio-title">Réalisations - </span>' . $titrecat . '</p>';
print $arrayInformation[$ID]["description"]; 
}
else if( has_term( 'inspiration', 'portfolio-category' ) ) {
echo '<p><span class="portfolio-title">Inspiration - </span>' . $titrecat . '</p>'; 
print $arrayInformation[$ID]["description"];
}
?>  
</div>

get_template_part('postformats/format', get_post_format());当然参考第一个文件

我无法将 $ID 号放入第二个文件,原因是可变的,感谢您的宝贵帮助 :)

更新

<?php 
/**
* Make sure we can access post data easily.
*/
global $post;

/**
* Setup variables needed for the gallery
*/
$attachments = get_post_meta( $post->ID, '_ebor_portfolio_gallery_list', true );
$type = get_post_meta( $post->ID, '_ebor_gallery_format', true );
$before = '<div class="owl-carousel portfolio-slider custom-controls">';
$after = '</div>';

if( $type == 'carousel' ){
$before = '<div class="carousel-gallery-wrapper"><div class="carousel-gallery custom-controls">';
$after = '</div></div>';
}

/**
 * If we found items, output the gallery.
  * $before and $after change depending on the gallery chosen.
 */
if ( $attachments ) : 

echo $before;

$arrayInformation = array();

    foreach( $attachments as $ID ){
        $attachment = get_post($ID);
        $attachment_description = $attachment->post_content; //Afficher la description
        $attachment_caption = $attachment->post_excerpt; //Afficher le caption
        $attachment_title = $attachment->post_title; //Afficher le titre

        $arrayInformation[$ID] = array("title" =>$attachment_title, "caption"       
=>$attachment_caption, "description" =>$attachment_description);

        echo '<div class="item">' . wp_get_attachment_image( $attachment->ID, 'portfolioslider' )   
. '</div>';
    }
echo $after;

endif;
?>

<div class="divide30"></div>

<?php 

$idimage = $attachment->ID;

$titrecat = get_the_title();
if( has_term( 'creation', 'portfolio-category' ) )  {
echo '<p><span class="portfolio-title">Création - </span>' . $titrecat . '</p>'; 
print $arrayInformation[$idimage]["description"];
}
else if( has_term( 'realisation', 'portfolio-category' ) ) {
echo '<p><span class="portfolio-title">Réalisations - </span>' . $titrecat . '</p>'; 
}
else if( has_term( 'inspiration', 'portfolio-category' ) ) {
echo '<p><span class="portfolio-title">Inspiration - </span>' . $titrecat . '</p>'; 
}
?>


</div>

【问题讨论】:

  • 在循环内定义它们之后在循环外使用它们?你在问什么? echo $attachment_title 可以正常工作吗?
  • 不行,循环结束后变量不存在了
  • Not true。除非您使用函数或其他范围,否则变量将存在。
  • 不是其他作用域,没有函数,即使我在循环之后直接调用我的变量也没什么,当我将它调用到循环中时就可以了
  • 您的代码是使用include()get_template_part() 函数调用的,因此您需要将循环完全移到另一个文件中。

标签: php loops variables foreach


【解决方案1】:

你可以在循环中使用 array_push

array_push($new_arr, $attachment_description );

所以你也可以在循环外获得分配的数组

【讨论】:

    【解决方案2】:

    您可以创建一个数组,其中索引是您的 $ID,值是包含您想要的信息的数组。

    $arrayInformation = array();
    
    foreach( $attachments as $ID ){
            $attachment = get_post($ID);
            $attachment_description = $attachment->post_content; //Display description
            $attachment_caption = $attachment->post_excerpt; //Display caption
            $attachment_title = $attachment->post_title; //Display title
    
            $arrayInformation[$ID] = array("title" =>$attachment_title, "caption" =>$attachment_caption, "description" =>$attachment_description)
    }
    

    现在您可以获取值(例如,如果您的 ID 为 1)

    print $arrayInformation[1]["title"];
    print $arrayInformation[1]["caption"];
    print $arrayInformation[1]["description"];
    

    【讨论】:

    • 它不起作用,因为:这是一个轮播,带有一个 wordpress 画廊。每个轮播显示超过 5 张图片。我需要为每个图像调用标题和描述。我用这两个文件编辑了我的第一个答案。我将循环移动到第二个文件中。但我必须从 print $arrayInformation[1] 中删除 [1]。它是动态的。感谢您的回复。
    • 我知道它是动态的,id 1 只是如何访问值的一个示例......您可以进行查询以获取图像的所有 ID(或任何其他方式来获取) ) 然后在 arrayInformation 中循环每一个
    • 好的,谢谢,但我不知道我能做到,哈哈。我从一大早就开始尝试,但我感到很失望:/
    • 我将查询放入循环中?
    • 如果查询对获取 ID 有用,则需要在循环之前进行。
    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2014-03-15
    • 2012-12-20
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多