Foreach不显示多个内容

时间:2015-01-12 12:14:01

标签: php wordpress foreach

我的Shortcode中有一点问题,我正在为WordPress创建。 foreach循环每次都不显示多个帖子。我不确定为什么会这样做,因为如果我var_dump $post变量显示这两个帖子都可用于该变量,那么有人可以帮助我吗?

代码:

function notes_shortcode($atts) {
global $post;

$atts = shortcode_atts( array( 'category' => $args["category"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"]);
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );

    foreach( $posts as $post ) {
        setup_postdata($post);
        $imgURL = getpostImage( $post->ID );
        $title = get_the_title( $post->ID );
        $content = substr(get_the_content() , 0, 125); 
        $post = '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
        $post .= '<div class="col-md-4 bloglist">';
        $post .= '<div class="post-content">';
        $post .= '<div class="post-image">';
        $post .= '<div class="flexslider blog-slider">';
        $post .= '<div class="overlay" style="opacity: 0;"></div>';
        $post .= '<div class="flex-viewport" style="overflow: hidden; position: relative;">';
        $post .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
        $post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $post .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $post .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"></li>';
        $post .= '</ul></div></div></div>';
        $post .= '<div class="date-box"><span class="day">' . $date . '</span>';
        $post .= '<span class="month">' . $month . '</span> </div>';
        $post .= '<div class="post-text">';
        $post .= '<h3><a href="css/#">' . $title . '</a></h3>';
        $post .= '<p> ' . $content . '<br>';
        $post .= ' <a href="#" class="btn-text">Read More</a></p></div></div></div></div>';
        return $post;
    }
}
add_shortcode( 'notes', 'notes_shortcode' );

function getpostImage($postid) {
    if (has_post_thumbnail($post->ID)){
            $imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
            $imgURL = $imgArray[0];
            return $imgURL;
    }
}

谢谢..

2 个答案:

答案 0 :(得分:1)

好的,我解决了这个问题。我使用WordPress过滤器挂钩$buff变量并将其返回到循环外部,如果有人需要,下面是解决方案。

function notes_shortcode($atts) {
global $post;
global $buf;

$atts = shortcode_atts( array( 'category' => $args["category"], 'posts_per_page' => $args["posts_per_page"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"], 'posts_per_page' => $atts["posts_per_page"] );
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
$buf = '';
$postHolder = array(); 

foreach( $posts as $post ) {
        setup_postdata($post);
        $imgURL = getpostImage( $post->ID );
        $title = get_the_title( $post->ID );
        $content = substr(get_the_content() , 0, 125); 

        $buf .= '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
        $buf .= '<div class="col-md-4 bloglist">';
        $buf .= '<div class="post-content">';
        $buf .= '<div class="post-image">';
        $buf .= '<div class="flexslider blog-slider">';
        $buf .= '<div class="overlay" style="opacity: 0;"></div>';
        $buf .= '<div class="flex-viewport" style="overflow: hidden; position: relative; max-width: 350px; max-height: 175px; padding-bottom: 15px; margin-bottom: 15px;">';
        $buf .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
        $buf .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $buf .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $buf .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $buf .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"></li>';
        $buf .= '</ul></div></div></div>';
        $buf .= '<div class="date-box"><span class="day">' . $date . '</span>';
        $buf .= '<span class="month">' . $month . '</span> </div>';
        $buf .= '<div class="post-text">';
        $buf .= '<h3><a href="css/#">' . $title . '</a></h3>';
        $buf .= '<p> ' . $content . '<br>';
        $buf .= ' <a href="#" class="btn-text">Read More</a></p></div></div></div>';
        $buf .= apply_filters( 'post_class', '</div>', $atts );
    } 
    $buf .= apply_filters( 'post_class', '', $atts );
    return $buf;
}

add_shortcode( 'notes', 'notes_shortcode' );

function getpostImage($postid) {
    if (has_post_thumbnail($post->ID)){
            $imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
            $imgURL = $imgArray[0];
            return $imgURL;
    }
}

谢谢大家的帮助..

答案 1 :(得分:0)

您正在返回该功能,这意味着您正在使功能结束。你可以做的是发送数组,然后再循环它。使用下面的代码

function notes_shortcode($atts) {
global $post;

$atts = shortcode_atts( array( 'category' => $args["category"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"]);
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
$array = array();
    foreach( $posts as $post ) {
        setup_postdata($post);
        $imgURL = getpostImage( $post->ID );
        $title = get_the_title( $post->ID );
        $content = substr(get_the_content() , 0, 125); 
        $post = '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
        $post .= '<div class="col-md-4 bloglist">';
        $post .= '<div class="post-content">';
        $post .= '<div class="post-image">';
        $post .= '<div class="flexslider blog-slider">';
        $post .= '<div class="overlay" style="opacity: 0;"></div>';
        $post .= '<div class="flex-viewport" style="overflow: hidden; position: relative;">';
        $post .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
        $post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $post .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $post .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"> </li>';
        $post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL  . '" alt="" draggable="false"></li>';
        $post .= '</ul></div></div></div>';
        $post .= '<div class="date-box"><span class="day">' . $date . '</span>';
        $post .= '<span class="month">' . $month . '</span> </div>';
        $post .= '<div class="post-text">';
        $post .= '<h3><a href="css/#">' . $title . '</a></h3>';
        $post .= '<p> ' . $content . '<br>';
        $post .= ' <a href="#" class="btn-text">Read More</a></p></div></div></div></div>';
        $array[] = $post;
    }
return $array;
}
add_shortcode( 'notes', 'notes_shortcode' );

function getpostImage($postid) {
    if (has_post_thumbnail($post->ID)){
            $imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
            $imgURL = $imgArray[0];
            return $imgURL;
    }
}

希望这有助于你