【问题标题】:Display all post image gallery using the shortcode [gallery]使用简码 [gallery] 显示所有帖子图片库
【发布时间】:2023-04-04 00:28:01
【问题描述】:

我想用“图库”显示帖子图库中的图像。像这样:<?php echo do_shortcode('[gallery]'); ?>

经过一番搜索,我了解到我们需要使用“preg_match”函数来获取图片库的 ID。类似的东西:

$post_content = $post->post_content;
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);

但我不知道如何使用它......我是新手,我很难使用它。 我应该把这段代码放在我的函数文件中吗?如果是,请问我该怎么做?

其目的是放置这样的最终代码:<?php echo do_shortcode( '[gallery ids="$array_id"]' ); ?>

谢谢你,对不起我的英语!!!

【问题讨论】:

    标签: wordpress preg-match gallery image-gallery shortcode


    【解决方案1】:

    我找到了解决方案,所以我与您分享。也许有人会对此感兴趣。

    我修改了以下代码:

    $post_content = $post->post_content;
    preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
    $array_id = explode(",", $ids[1]);
    

    进入:

    <?php
    global $post;
    
        $post_content = $post->post_content;
        preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
        $images_id = explode(",", $ids[1]);
    echo do_shortcode('[gallery type="slideshow" ids="'. implode(',', array_slice($images_id, 0, 3)).' ,"]');
    ?>
    

    我将它插入到我的自定义 format-gallery.php 中,它工作得很好。请注意,我已自动限制使用 array_slice() 返回的 id 数量。

    我希望这会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多