Uff*_*ffo 2 wordpress wordpress-theming
我在functions.php中有以下代码:
function get_images($size = 'thumbnail') {
global $post;
return get_children( array('post_parent' => get_the_ID(), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
}
Run Code Online (Sandbox Code Playgroud)
在我的single.php中
<?php $photos = get_images('full'); ?>
<?php $x = 0; ?>
<?php foreach($photos as $photo): ?>
<?php if($x < 1): ?>
<img src="<?=wp_get_attachment_url($photo->ID)?>" alt="fullImg" />
<?php endif; ?>
<?php $x++;
?>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)
我想在那里只展示一个图像,但它也显示了我不想要的后拇指图像,是否有一个选项可以排除它?
那是一些疯狂的编码!在函数中接受一个大小毫无意义,因为它只返回一个post对象数组.
一旦你得到了你的帖子,然后使用适当的附加功能(S),以获得对附件大小你后的信息.
我会建议这样的功能;
function get_images($overrides = '', $exclude_thumbnail = false)
{
return get_posts(wp_parse_args($overrides, array(
'numberposts' => -1,
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'exclude' => $exclude_thumbnail ? array(get_post_thumbnail_id()) : array(),
'orderby' => 'menu_order ID'
)));
}
Run Code Online (Sandbox Code Playgroud)
并将其付诸实践;
<?php if ($photo = get_images('numberposts=1', true)): ?>
<img src="<?php echo wp_get_attachment_url($photo[0]->ID); ?>" alt="fullimg" />
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
更新:错字功能 - 修复.
| 归档时间: |
|
| 查看次数: |
2444 次 |
| 最近记录: |