2010-12-15 63 views
1

<?php echo do_shortcode('[gallery id="25"]'); ?>限制的库项目数量WordPress的输出

这是我使用的代码,我需要一种方法来把它限制在2项,而不是所有项目。我知道有没有本地的方式来做到这一点与画廊简码,但有一个插件或替代方法,我可以使用?

+0

尝试使用[下一代画廊(http://wordpress.org/extend/plugins/nextgen -gallery /),并使用[其API](http://nextgen-gallery.com/custom-fields/) – pootzko 2010-12-15 07:25:51

回答

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; 
}