首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可变前驱循环php

可变前驱循环php
EN

Stack Overflow用户
提问于 2014-11-17 10:21:20
回答 2查看 1.9K关注 0票数 1

我有麻烦了。

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

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

谢谢你的帮助。守则如下:

代码语言:javascript
运行
复制
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;

似乎没有什么是可行的,这是循环的第一个文件:

代码语言:javascript
运行
复制
<?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;

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

代码语言:javascript
运行
复制
<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编号放入第二个文件中,原因是可变的,谢谢您宝贵的帮助:)

更新

代码语言:javascript
运行
复制
<?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>
EN

Stack Overflow用户

发布于 2014-11-17 10:28:36

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

代码语言:javascript
运行
复制
$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)

代码语言:javascript
运行
复制
print $arrayInformation[1]["title"];
print $arrayInformation[1]["caption"];
print $arrayInformation[1]["description"];
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26970430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档