本机图库自定义html输出

时间:2018-09-14 作者:qwaz

我已通过将图像库添加到我的页面Add Media > Create Gallery. 我不想输出gallery的默认html,也没有足够的选项来更改gallery sortcode的属性:itemtag, icontag, captiontag 因为我想要一个完全定制的html。

我试过过滤post_gallery, 抓取作为图库包含在该页面中的图像的URL并将其输出:

add_filter( \'post_gallery\', \'my_name_page_gallery\', 10, 2 );
function my_name_page_gallery($output, $attr) {
    $output = get_post_gallery_images();
    return $output;
}
但我得到以下错误:

已达到最大函数嵌套级别“512”,正在中止!in/wp包括/短代码。php第234行

我做错了什么?如何获取库图像的URL并输出自定义html?

1 个回复
SO网友:RiddleMeThis

这就是你要找的,here? get_post_gallery_images().

这是该页面的示例。

add_filter( \'the_content\', \'wpdocs_show_gallery_image_urls\' );

/**
* Show image URLs below the content
*/
function wpdocs_show_gallery_image_urls( $content ) {

    global $post;

    // Only do this on singular items
    if( ! is_singular() )
        return $content;

    // Make sure the post has a gallery in it
    if( ! has_shortcode( $post->post_content, \'gallery\' ) )
        return $content;

    // Retrieve the first gallery in the post
    $gallery = get_post_gallery_images( $post );
    $image_list = \'<ul>\';

    // Loop through each image in each gallery
    foreach( $gallery as $image_url ) {
        $image_list .= \'<li>\' . \'<img src="\' . $image_url . \'">\' . \'</li>\';
    }
    $image_list .= \'</ul>\';

    // Append our image list to the content of our post
    $content .= $image_list;

    return $content;
}

结束

相关推荐

Shortcode is not working

通过阅读教程,我创建了一个简单的快捷代码:<?php /* Plugin Name: GT NoTranslate Shortcode Description: Shortcode to wrap a span with the \"notranslate\" span around text. Version: 0.1 BETA Author: Andrew Truckle Author URI: http://www.trucklesoft.co.