使用图库获取最新帖子很简单:使用字符串搜索帖子\'[gallery\'
.
$posts = get_posts(
array (
\'s\' => \'[gallery\'
)
);
从每个库中获取第一个图像比较困难,因为您必须渲染
gallery
短代码。我不会使用默认的处理程序,它比我们这里需要的要多得多。这会浪费太多时间。
一步一步…
主要功能让我们从一个函数开始,该函数使用两个参数检索最新画廊帖子列表:帖子数量和格式帮助器:
function t5_list_galleries( $num = 10, $formatter = \'t5_gallery_list_formatter\' )
{
$posts = get_posts(
array (
\'s\' => \'[gallery\',
\'numberposts\' => (int) $num
)
);
if ( ! $posts )
return FALSE;
$list = new T5_Gallery_Image_Extractor( $posts );
$html = call_user_func( $formatter, $list->get_results() );
return $html;
}
正如你所见,我们需要一门课
T5_Gallery_Image_Extractor
构建列表和格式函数以创建标记。我没有将格式化程序放入gallery提取器以保持灵活性:您可以在小部件中或在具有不同格式化程序的短代码中使用该功能。
类T5\\u Gallery\\u Image\\u提取器类构造函数需要一个帖子列表。它不关心我们是如何得到这个列表的,所以我们可以用一个完全不同的主函数手动构建它。
在内部,它将替换gallery
使用简化版,只需从帖子中的第一个图库中获取第一幅图像。
公共方法get_results()
返回如下数组:
Array
(
[0] => Array
(
[url] => http://single.wp/blog/gallery-2/
[img] => <img width="150" height="150" src="http://content.wp/singlewp/uploads/2013/01/git-bash-150x150.png" class="attachment-thumbnail" alt="git-bash" />
[title] => Gallery 2
)
[1] => Array
(
[url] => http://single.wp/blog/gallery-1/
[img] => <img width="150" height="150" src="http://content.wp/singlewp/uploads/2013/01/hat-150x150.png" class="attachment-thumbnail" alt="hat" />
[title] => Gallery 1
)
)
因此,我们通过一个图库获取每篇文章的URL、图像标记和文章标题。
代码,抱歉,没有时间发表评论,maybelater.:)
class T5_Gallery_Image_Extractor
{
protected $results = array ();
protected $current_post = NULL;
protected $old_gallery_handler = FALSE;
public function __construct( $posts )
{
$this->replace_gallery_shortcode();
foreach ( $posts as $post )
$this->collect_first_image( $post );
}
public function get_results()
{
remove_shortcode( \'gallery\' );
if ( $this->old_gallery_handler )
add_shortcode( \'gallery\', $this->old_gallery_handler );
return $this->results;
}
protected function collect_first_image( $post )
{
$this->current_post = $post;
do_shortcode( $post->post_content );
$this->current_post = NULL;
}
protected function replace_gallery_shortcode()
{
// replace the old gallery shortcode
if ( isset( $GLOBALS[\'shortcode_tags\'][\'gallery\'] ) )
$this->old_gallery_handler = $GLOBALS[\'shortcode_tags\'][\'gallery\'];
remove_shortcode( \'gallery\' );
add_shortcode( \'gallery\', array ( $this, \'gallery_shortcode\' ) );
}
public function gallery_shortcode( $attr )
{
static $processed_posts = array ();
$post = $this->current_post;
if ( in_array( $post->ID, $processed_posts) )
return; // no need to run through all galleries of a post.
$processed_posts[] = $post->ID;
if ( ! empty ( $attr[\'ids\'] ) )
{
if ( empty ( $attr[\'orderby\'] ) )
$attr[\'orderby\'] = \'post__in\';
$attr[\'include\'] = $attr[\'ids\'];
}
if ( isset ( $attr[\'orderby\'] ) )
{
$attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
if ( ! $attr[\'orderby\'] )
unset( $attr[\'orderby\'] );
}
extract(shortcode_atts(array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\',
\'id\' => $post->ID,
\'include\' => \'\',
\'exclude\' => \'\'
), $attr));
$id = intval($id);
if ( \'RAND\' == $order )
$orderby = \'none\';
$att_args = array (
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => $order,
\'orderby\' => $orderby,
\'numberposts\' => 1
);
if ( ! empty ( $include ) )
{
$att_args[\'include\'] = $include;
$attachments = get_posts( $att_args );
}
else
{
$att_args[\'post_parent\'] = $post->ID;
if ( ! empty ( $exclude ) )
$att_args[\'exclude\'] = $exclude;
$attachments = get_children( $att_args );
}
$this->results[] = array (
\'url\' => get_permalink( $post->ID ),
\'img\' => wp_get_attachment_image( current( $attachments )->ID ),
\'title\' => $post_title = esc_attr( $post->post_title )
);
}
}
格式化程序非常简单:只需将上面的数组转换为HTML列表即可。
function t5_gallery_list_formatter( $list )
{
$output = \'<ul class="t5-gallery-list">\';
foreach ( $list as $item )
$output .= sprintf(
\'<li><a href="%1$s" title="%2$s">%3$s</a></li>\',
$item[\'url\'],
$item[\'title\'],
$item[\'img\']
);
return "$output</ul>";
}
现在,我们可以在任何需要的地方使用主功能:
print t5_list_galleries();
结果是一个很好的列表,其中包含了最新的十篇文章和一个图库,第一个图库中的第一幅图像被用作链接文本。
视觉格式由样式表决定:
.t5-gallery-list {
/* here be dragon rules */
}