我想有一个画廊在一篇文章或一个页面(代码在模板或通过快捷码)。此图库的图像应自动从我选择的一个或多个类别的帖子中提取。
因此,在保存新帖子后,所有附加到此帖子的图像(一个或多个)都应自动添加到库中。
如果这个图库可以放在一个单独的页面上,那就太好了。如果一个插件成功了:也很好。
我想有一个画廊在一篇文章或一个页面(代码在模板或通过快捷码)。此图库的图像应自动从我选择的一个或多个类别的帖子中提取。
因此,在保存新帖子后,所有附加到此帖子的图像(一个或多个)都应自动添加到库中。
如果这个图库可以放在一个单独的页面上,那就太好了。如果一个插件成功了:也很好。
以短代码的形式呈现默认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;
}
阅读文档以根据您的规范微调代码。编辑:我终于得到了正确的答案;请在这篇帖子下面为所有感兴趣的人查看我自己的答案。经过几天的搜寻和反复尝试,我真的被卡住了。我有一个客户,他通过使用评论表在自己的网站上收集客户体验;所有评论都显示为客户评论。到目前为止还不错。但由于这家公司在旅游方面做得很多,他需要让他的客户上传多达五张图片,并附上评论。所以我需要做的是找到一种方法,让客户上传媒体,并填写评论表。我知道这有点冒险,因为可能会上传损坏的图像等等。但我仍然希望实现这一目标。尝试了很多东西,但我一次又一次陷入困境的地方是文件上传处理,对于一个未登