限制 WordPress 输出的图库项目数量

发布于 2024-10-07 19:38:28 字数 158 浏览 0 评论 0原文

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

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

That's the code I'm using, I need a way to limit it to 2 items instead of all items. I know there is no native way to do it with the gallery shortcode, but is there a plugin or alternative method I could use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2024-10-14 19:38:28

您可以在模板的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;
}

you can rewrite the gallery shortcode function in your template's functions.php and do something like this

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文