听起来您只需要使用快捷码来收集文件夹的内容并显示这些项目的网格。短代码很容易设置。
$gallery = do_shortcode(\'[folder_gallery title="XYZ" folder="wp-content/uploads/2015/11"]\' );
echo $gallery;
那你只需要
glob 一个目录,并从文件中渲染您的库。我没有包括一个画廊,但这将工作以外的上传。只需给它一个相对于根文件夹的路径。
// [folder_gallery folder="wp-content/uploads/2015/11" ]
add_shortcode( \'folder_gallery\', \'folder_gallery__shortcode\' );
function folder_gallery__shortcode( $atts ) {
$a = shortcode_atts(
array (
\'folder\' => \'\',
\'title\' => \'\',
), $atts );
$folder = $a [ \'folder\' ];
// bad folder
if ( empty( $folder ) || ! is_readable(ABSPATH . $folder) ) {
return \'No Valid Folder Selected\';
}
// allow filtering of the filetypes
$filetypes = apply_filters( \'folder_gallery_shortcode__filetypes\', array ( \'png\', \'jpg\', \'jpeg\', \'gif\' ) );
// glob
$filetypes = empty( $filetypes ) ? \'png,jpg,jpeg,gif\' : implode( \',\', $filetypes );
$files = glob( untrailingslashit( ABSPATH . $folder ) . "/*.{" . $filetypes . "}", GLOB_BRACE );
$gallery_images = array ();
foreach ( $files as $file ) {
if ( $file === __FILE__ ) {
continue;
}
/*
$filetype = wp_check_filetype( $file );
$ext = $filetype[ \'ext\' ];
$type = $filetype[ \'type\' ];
$proper_filename = $filetype[ \'proper_filename\' ];
*/
// replace the filepath with url path for front-end gallery
$gallery_images[] = str_replace( trailingslashit( ABSPATH ), trailingslashit( WP_SITEURL ), $file );
}
// TODO: IMPLEMENT YOUR GALLERY HERE
// construct the gallery
$output = empty( $a[ \'title\' ] ) ? \'\' : \'<h2>\' . $a[ \'title\' ] . \'</h2>\';
$output .= \'<ul>\';
// Loop through each image in each gallery
foreach ( $gallery_images as $image_url ) {
$output .= \'<li>\' . \'<img src="\' . $image_url . \'">\' . \'</li>\';
}
$output .= \'</ul>\';
// allow filtering of the output
$gallery = apply_filters( \'folder_gallery_shortcode__gallery\', $output, $gallery_images );
return $gallery;
}