我为一个主题创建了一个函数,该函数限制了画廊中显示的图像数量。这在Gallery post格式中。但是,我想限制所有页面上的图库图像,但单个帖子除外。
到目前为止,我的代码如下(用特定于所讨论的属性的部分进行了缩短):
function the_mx_limited_gallery( $attr ) {
$post = get_post();
$attachment_ids = the_mx_get_limited_gallery_ids();
$link_image_to = the_mx_medialink_switcher(); // Customizer controls
if( get_post_format() == \'gallery\' ) { // opens gallery post format check
...
// setup shortcode attributes
$atts = shortcode_atts( array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\',
\'id\' => $post ? $post->ID : 0,
\'itemtag\' => \'figure\',
\'icontag\' => \'div\',
\'captiontag\' => \'figcaption\',
\'columns\' => 3,
\'include\' => $attachment_ids,
\'size\' => \'gallery-thumb\',
\'link\' => $link_image_to,
), $atts );
$id = intval( $atts[\'id\'] );
if ( ! empty( $atts[\'include\'] ) ) {
$_attachments = get_posts( array(
\'include\' => $atts[\'include\'],
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => $atts[\'order\'],
\'orderby\' => $atts[\'orderby\'],
) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( ! empty( $atts[\'exclude\'] ) ) {
$attachments = get_children( array(
\'post_parent\' => $id,
\'exclude\' => $atts[\'exclude\'],
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => $atts[\'order\'],
\'orderby\' => $atts[\'orderby\']
) );
} else {
$attachments = get_children( array(
\'post_parent\' => $id,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => $atts[\'order\'],
\'orderby\' => $atts[\'orderby\']
) );
}
...
}
检索gallery ID(The\\u mx\\u get\\u limited\\u gallery\\u ID)的代码如下:
// Function for limiting Gallery ID length (in the Gallery post format).
// see http://www.webgurus.biz/how-to-limit-wordpress-gallery-thumbnails-in-the-loop/
function the_mx_get_limited_gallery_ids() {
global $wpdb, $post;
$ids = \'\';
$counter = 0;
$number_of_posts = 6;
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => 6,
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'post_status\' => null,
\'post_parent\' => $post->ID,
);
$attachments = get_posts($args);
if($attachments) {
foreach ($attachments as $attachment) {
if($counter != 0) {
$ids .= \',\'.$attachment->ID;
} else {
$ids .= $attachment->ID;
}
$counter++;
}
}
return $ids;
}
为了在前端显示,我尝试只使用筛选
shortcode_atts_gallery
并显示为
do_shortcode
, 但在我的测试环境中,这会干扰Jetpack的库添加。我想让我的画廊和Jetpack画廊一起展出。
任何帮助都将不胜感激。