以短代码的形式呈现默认WordPress[gallery]
短代码,包括给定类别的所有附件:
/**
* Usage: [catgallery cat="4,5"]
* Attribute: array of category IDs
*/
add_shortcode(\'catgallery\', \'wpse_70989_cat_gallery_shortcode\');
function wpse_70989_cat_gallery_shortcode($atts)
{
// $return = \'\'; // DEBUG: enable for debugging
$arr = array();
$cat_in = explode( \',\', $atts[\'cat\'] );
$catposts = new WP_Query( array(
\'posts_per_page\' => -1
, \'category__in\' => $cat_in
) );
foreach( $catposts->posts as $post)
{
// DEBUG: Enable the next line to print the post title
// $return .= \'<strong>\' . $post->post_title . \'</strong><br />\';
$args = array(
\'post_type\' => \'attachment\'
, \'numberposts\' => -1
, \'post_status\' => null
, \'post_parent\' => $post->ID
);
$attachments = get_posts($args);
if ($attachments)
{
foreach ( $attachments as $attachment )
{
// DEBUG: Enable the next line to debug the attachement object
// $return .= \'Attachment:<br /><pre>\' . print_r($attachment, true) . \'</pre><br />\';
$arr[] = $attachment->ID;
}
}
}
// DEBUG: Disable the next line if debugging
$return = do_shortcode( \'[gallery include="\' . implode( \',\', $arr ) . \'"]\' );
return $return;
}
阅读文档以根据您的规范微调代码。