我最近在整理我们家的所有旧电脑,13年前的联想,8年前的惠普,7年前的神舟:加内存,换/加SSD,换CPU(i3-330M换成了i7-640M),换CMOS电池(联想没成功,他是用电焊焊死针脚的,我用的450°C锡焊枪结果把接触铜丝一起扯掉了),换电源适配器,换电池。
总之一通折腾,然后从联想拆下来的一个320G的机械硬盘套了个硬盘盒当移动硬盘了,把所有电脑上的视频图片整理了一下,有将近70G多,排除掉电影剧集也还剩20G多。话说我的账户是Bluehost的Plus版,以前是无限存储空间的,现在如果看他的宣传页,上面说只有20G了。但是我进入cpanel里面看统计信息,又是写的21.38 GB / ∞,不清楚什么情况。先不管了,暂时先把东西都传上去再说,如果后面有问题再删,等下买个Office365家庭版传OneDrive上去。
我把这20G的图片都添加到了这个WordPress的NextGen Gallery (NGG)里面,又分类建了几十个相册。我以前的相册页面是还是用老的shortcode(简码)写的: [[album id=1,2,3, compact]] 。即便是现在新的古腾堡块编辑器(Gutenberg),在编辑Gallery的时候,也只能一个个相册添加。我需要一个能自动更新的页面来显示所有的相册,每当我新建或删除相册之后,只要刷新一下这个页面就能看到所有NGG里面的相册,这样就不用每次都过来添加删除了。
OK,先理清一下思路,简单来说我需要一段嵌入页面执行的脚本,既然WordPress是基于PHP的,那肯定是他了。这段脚本先去取NGG里面所有的Album或Gallery,然后再想办法调用shortcode或者什么function转换成页面的显示。NGG本身在MySQL里面应该是有独立的database或者table的,所以理论上直接从db里面query肯定能弄出来。但是本着有API就不直接访问db的原则,先去NGG网站上看一下文档再说:Browse Documentation for Imagely Products。
除了shortcode example其他的好像没什么用,不过他既然是WordPress插件,那么代码应该可以从plugins里面看得到。去cpanel的文件管理器,果然能看到wp-content/plugins/nextgen-gallery目录,打包下到本地,用vscode全文搜索,在products\photocrati_nextgen\modules\ngglegacy\lib\ngg-db.php找到了下面一段代码:
products\photocrati_nextgen\modules\ngglegacy\lib\ngg-db.php
/**
* Finds all galleries
* @deprecated
* @return array
*/
static function find_all_galleries()
{
$mapper = C_Gallery_Mapper::get_instance();
return $mapper->find_all();
}
/**
* Finds all albums
*
* @deprecated
* @param string $order_by
* @param string $order_dir
* @param int $limit number of albums, 0 shows all albums
* @param int $start the start index for paged albums
* @return array $album
*/
function find_all_album($order_by = 'id', $order_dir = 'ASC', $limit = '0', $start = '0')
{
$mapper = C_Album_Mapper::get_instance();
$mapper->select();
$mapper->where_and(array());
$mapper->order_by($order_by, $order_dir);
if ($limit > 0)
$mapper->limit($limit, $start);
return $mapper->run_query();
}Code language: PHP (php)瞧他挂着@deprecated,那就不直接调用了,看了下C_Gallery_Mapper和C_Album_Mapper,应该可以按照get_instance()->find_all()这样用。继续找showAlbum和showGallery:
products\photocrati_nextgen\modules\nextgen_basic_album\module.nextgen_basic_album.php
function nggShowAlbum($albumID, $template = 'extend', $gallery_template = '')
{
$renderer = C_Displayed_Gallery_Renderer::get_instance();
$retval = $renderer->display_images(array(
'album_ids' => array($albumID),
'display_type' => 'photocrati-nextgen_basic_extended_album',
'template' => $template,
'gallery_display_template' => $gallery_template
));
return apply_filters('ngg_show_album_content', $retval, $albumID);
}
Code language: PHP (php)products\photocrati_nextgen\modules\nextgen_basic_gallery\module.nextgen_basic_gallery.php
/**
* Wrapper to I_Displayed_Gallery_Renderer->display_images(); this will display
* a basic thumbnails gallery
*
* @param int $galleryID Gallery ID
* @param string $template Path to template file
* @param bool $images_per_page Basic thumbnails setting
*/
function nggShowGallery($galleryID, $template = '', $images_per_page = FALSE)
{
$args = array(
'source' => 'galleries',
'container_ids' => $galleryID
);
if (apply_filters('ngg_show_imagebrowser_first', FALSE, $galleryID))
$args['display_type'] = NGG_BASIC_IMAGEBROWSER;
else
$args['display_type'] = NGG_BASIC_THUMBNAILS;
if (!empty($template))
$args['template'] = $template;
if (!empty($images_per_page))
$args['images_per_page'] = $images_per_page;
echo C_Displayed_Gallery_Renderer::get_instance()->display_images($args);
}
Code language: PHP (php)好了,基本思路有了,再找个插件“Code Snippets”用于在页面插入脚本顺便调试,开始干活。
行了不废话了,总结一下,NGG Gallery看起来是他们比较上心重点维护的东西,按照他的代码直接调用就行;NGG Album好像差点意思,直接调用的话连分页都没有。总之,写好的代码如下:
Show all NGG albums.
<?php
$albums = C_Album_Mapper::get_instance()->find_all();
$album_ids = array();
foreach ($albums as $album) {
array_push($album_ids, $album->id);
}
//echo nggShowAlbum(join(',', $album_ids), 'extend', 'basic_compact_album');
$renderer = C_Displayed_Gallery_Renderer::get_instance();
$retval = $renderer->display_images(array(
'album_ids' => $album_ids,
'display_type' => 'photocrati-nextgen_basic_extended_album',
'template' => 'extend',
'galleries_per_page' => 5
));
echo apply_filters('ngg_show_album_content', $retval, join(',', $album_ids));
?>Code language: PHP (php)Show all NGG galleries.
<?php
$galleries = C_Gallery_Mapper::get_instance()->find_all();
$gallery_ids = array();
foreach ($galleries as $gallery) {
array_push($gallery_ids, $gallery->gid);
}
nggShowGallery($gallery_ids, '', 10);
?>Code language: PHP (php)把这俩加到Snippets插件里面(HTML Content),经典编辑器里可以在编辑排版栏插入Content Snippet,勾选Run PHP Code,可以看到他会生成一段简码:
[code_snippet id=xx php=true]Code language: JSON / JSON with Comments (json)
OK,这样一来这个页面就会一直显示所有的Gallery或Album,你可以随便维护你的NGG而不用管这个页面了。

发表回复