Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算PHP数组\u块中的项目_Php_Arrays_Wordpress - Fatal编程技术网

计算PHP数组\u块中的项目

计算PHP数组\u块中的项目,php,arrays,wordpress,Php,Arrays,Wordpress,我有下面的函数,它获取wordpress帖子并将它们输出到一个引导网格中(每行3篇帖子)。每个数组块中有3个POST。基本上,我想做的是,如果区块不完整,并且其中只有2篇文章,那么该区块中的第一篇文章将添加“col-sm-offset-2”类。我相信我需要一些方法来计算文章的数量,但我不确定如何实现 function post_events($atts) { global $post; $args = array( 'post_type' => '

我有下面的函数,它获取wordpress帖子并将它们输出到一个引导网格中(每行3篇帖子)。每个数组块中有3个POST。基本上,我想做的是,如果区块不完整,并且其中只有2篇文章,那么该区块中的第一篇文章将添加“col-sm-offset-2”类。我相信我需要一些方法来计算文章的数量,但我不确定如何实现

    function post_events($atts) {

    global $post;

    $args = array(
    'post_type'    => 'event',
    'post_status'  => 'publish',
    'orderby'      => 'date',
    'order'        => 'ASC',
    );

    $posts = get_posts($args);

    $posts_chunks = array_chunk($posts, 3);

    $output = '';

    foreach ($posts_chunks as $row) {

        $output .= '<div class="row">';

        foreach ($row as $post) {

            setup_postdata($post);

            $output .= '<div class="col-md-6 col-sm-6 event-item">';
            $output .= '<a href="' .get_the_permalink(). '">' .get_the_post_thumbnail(). '</a>';
            $output .= '<div class="event-item-text">';
            $output .= '<h3><a href="'.get_the_permalink(). '">' .get_the_title(). '</a></h3>';
            $output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
            $output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
            $output .= '</div>';
            $output .= '</div>';
        }

        $output .= '</div>';
    }

    return $output;
 }

add_shortcode('post_events','post_events');
function post_事件($atts){
全球$员额;
$args=数组(
“post_类型”=>“事件”,
“发布状态”=>“发布”,
'orderby'=>'date',
“订单”=>“ASC”,
);
$posts=get_posts($args);
$posts\u chunks=array\u chunks($posts,3);
$output='';
foreach($posts\u块作为$row){
$output.='';
foreach($row作为$post){
设置_postdata($post);
$output.='';
$output.='';
$output.='';
$output.='';
$output.=''。获取日期(“d-m-Y”);
$output.=''.wp_trim_words(获取内容(),40,“…”)。

'; $output.=''; $output.=''; } $output.=''; } 返回$output; } 添加_短代码(“post_事件”、“post_事件”);
您只需在
$row
上使用,然后根据是否为第一次迭代设置类:

$class = 'col-md-6 col-sm-6 event-item';
$count = count($row);

foreach( $row as $k => $post ) 
{
    $cls = ($k == 0 && $count < 2) ? $class.' col-sm-offset-2' : $class;
    setup_postdata($post);
    $output.= '<div class="'.$cls.'">';
    //...
}
$class='col-md-6 col-sm-6事件项';
$count=计数($row);
foreach($k=>$post行)
{
$cls=($k==0&&$count<2)?$class.'col-sm-offset-2':$class;
设置_postdata($post);
$output.='';
//...
}
您只需在
$row
上使用,然后根据是否为第一次迭代设置类:

$class = 'col-md-6 col-sm-6 event-item';
$count = count($row);

foreach( $row as $k => $post ) 
{
    $cls = ($k == 0 && $count < 2) ? $class.' col-sm-offset-2' : $class;
    setup_postdata($post);
    $output.= '<div class="'.$cls.'">';
    //...
}
$class='col-md-6 col-sm-6事件项';
$count=计数($row);
foreach($k=>$post行)
{
$cls=($k==0&&$count<2)?$class.'col-sm-offset-2':$class;
设置_postdata($post);
$output.='';
//...
}

由于您已经分块,子数组将基于
0
,第一篇文章将是
0
。所以只要检查一下,如果
计数小于
3

foreach ($posts_chunks as $row) {

    $output .= '<div class="row">';

    foreach ($row as $key => $post) {
        $extra = '';  //set extra class to empty
        if($key == 0 && count($row) < 3) { //check first post and count less than 3
            $extra = 'col-sm-offset-2 '; //set extra class
        }
        setup_postdata($post);
        //use $extra somewhere
        $output .= '<div class="'.$extra.'col-md-6 col-sm-6 event-item">';
        $output .= '<a href="' .get_the_permalink(). '">' .get_the_post_thumbnail(). '</a>';
        $output .= '<div class="event-item-text">';
        $output .= '<h3><a href="'.get_the_permalink(). '">' .get_the_title(). '</a></h3>';
        $output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
        $output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
        $output .= '</div>';
        $output .= '</div>';
    }

    $output .= '</div>';
}
foreach($posts\u块为$row){
$output.='';
foreach($key=>$post的行){
$extra='';//将extra类设置为空
如果($key==0&&count($row)<3){//请检查第一个post并计数小于3
$extra='col-sm-offset-2';//设置额外类
}
设置_postdata($post);
//在某处使用$extra
$output.='';
$output.='';
$output.='';
$output.='';
$output.=''。获取日期(“d-m-Y”);
$output.=''.wp_trim_words(获取内容(),40,“…”)。

'; $output.=''; $output.=''; } $output.=''; }
由于您已经分块,子数组将基于
0
,第一篇文章将是
0
。所以只要检查一下,如果
计数小于
3

foreach ($posts_chunks as $row) {

    $output .= '<div class="row">';

    foreach ($row as $key => $post) {
        $extra = '';  //set extra class to empty
        if($key == 0 && count($row) < 3) { //check first post and count less than 3
            $extra = 'col-sm-offset-2 '; //set extra class
        }
        setup_postdata($post);
        //use $extra somewhere
        $output .= '<div class="'.$extra.'col-md-6 col-sm-6 event-item">';
        $output .= '<a href="' .get_the_permalink(). '">' .get_the_post_thumbnail(). '</a>';
        $output .= '<div class="event-item-text">';
        $output .= '<h3><a href="'.get_the_permalink(). '">' .get_the_title(). '</a></h3>';
        $output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
        $output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
        $output .= '</div>';
        $output .= '</div>';
    }

    $output .= '</div>';
}
foreach($posts\u块为$row){
$output.='';
foreach($key=>$post的行){
$extra='';//将extra类设置为空
如果($key==0&&count($row)<3){//请检查第一个post并计数小于3
$extra='col-sm-offset-2';//设置额外类
}
设置_postdata($post);
//在某处使用$extra
$output.='';
$output.='';
$output.='';
$output.='';
$output.=''。获取日期(“d-m-Y”);
$output.=''.wp_trim_words(获取内容(),40,“…”)。

'; $output.=''; $output.=''; } $output.=''; }