限制WordPress输出的图库项目数

时间:2010-12-15 01:27:39

标签: wordpress wordpress-theming

<?php echo do_shortcode('[gallery id="25"]'); ?>

这是我正在使用的代码,我需要一种方法将其限制为2个项目而不是所有项目。我知道没有本地方法可以使用库短代码,但是我可以使用插件或替代方法吗?

1 个答案:

答案 0 :(得分:0)

您可以在模板的functions.php中重写图库短代码功能并执行类似的操作

remove_shortcode('gallery');
add_shortcode('gallery', 'parse_gallery_shortcode');
function parse_gallery_shortcode($atts) {

global $post;

extract(shortcode_atts(array(
'order'      => 'ASC',
'orderby'    => 'menu_order ID',
    'id' => $post->ID,
    'itemtag' => 'dl',
    'icontag' => 'dt',
    'captiontag' => 'dd',
    'columns' => 3,
    'ids' => '',
    'size' => 'medium',
    'link' => 'file'
), $atts));

$ids = explode(',', $atts[ids]);
$i = 0;
foreach( $ids as $id ) {
    $i++;
    if ( $i > 2 ) { break; } 
    // or replace 2 with how many images you want
$image  = get_post($id);
$img = wp_get_attachment_image_src($image->ID, 'post-onephoto');
$largeimg = wp_get_attachment_image_src($image->ID, 'large');
// this is where you output your images the way you want it
$return .= '<a href="'.$largeimg[0].'"><img width="400" height="400" src="'.$img[0].'" /></a>'; 
}

 return $return;
}
相关问题