使用短代码[gallery]显示所有帖子图库

时间:2015-07-27 12:22:53

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

我希望使用&#34; gallery&#34;显示帖子库中的图像。像那样:<?php echo do_shortcode('[gallery]'); ?>

经过一番搜索,我了解到我们需要使用&#34; preg_match&#34;功能来获取图像库的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"]' ); ?>

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

1 个答案:

答案 0 :(得分:0)

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

我修改了以下代码:

backend default {
  .host = "127.0.0.1";
  .port = "80";
}

acl purge {
        "localhost";
        "*loadbalancer-ip*";
}

sub vcl_recv {

    if (req.request == "PURGE") {
            if(!client.ip ~ purge) {
                    error 405 "Not allowed.";
            }
            return (lookup);
    } else if (req.url ~ "(?i)\.(jpeg|jpg|png|gif|ico|js|css|xml)$") {
            unset req.http.Cookie;
            return (lookup);
    } else {
            return (pass);
    }
}

sub vcl_hit {
    if (req.request == "PURGE") {
            set obj.ttl = 0s;
            error 200 "Purged";
    }
}

sub vcl_miss {
    if (req.request == "PURGE") {
            error 404 "Not in cache.";
    }
}


sub vcl_fetch {
    if (req.url ~ "(?i)\.(jpeg|jpg|png|gif|ico|js|css|xml)$") {
            unset beresp.http.set-cookie;
            return (deliver);
    }
}

sub vcl_deliver {
    if (obj.hits > 0) {
            set resp.http.X-Cache = "HIT";
    } else {
            set resp.http.X-Cache = "MISS";
    }
}

进入:

$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返回的ID数。

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