如何在wordpress画廊短代码中计算画廊的图像

时间:2014-12-05 21:14:49

标签: php wordpress

我想在每个帖子中显示wordpress画廊中的项目总数。 我编码这个,但它不起作用。帖子在图库中显示错误的项目数。

$args = array(
    'order' => 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_status' => null,
    'post_type' => 'attachment',
);

$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $post->post_content, $matches);
if (is_array($matches) && $matches[2] == 'gallery') {
    // do something
    preg_match('/\[ gallery ids=\"(.*?)\"]/',$matches[0],$ids);

    if (is_array($ids) && $ids[1] ) {
        $photos = explode(',',$ids[1]);
    } else {
        $photos = get_children( $args );
    }
    $total_images = count($photos);
}

0 个答案:

没有答案