代码之家  ›  专栏  ›  技术社区  ›  Uffo

获取附件,但不发布缩略图

  •  2
  • Uffo  · 技术社区  · 15 年前

    我有以下代码在我的 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') );
    
    }
    

    在我的 单一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; ?>
    

    我只想在那里显示一个图像,但它也显示了 后拇指图像,我不想要 ,是否有排除的选项?

    1 回复  |  直到 15 年前
        1
  •  7
  •   TheDeadMedic    15 年前

    那真是太疯狂了!接受函数中的大小是毫无意义的,因为它只返回post对象的数组。

    一旦你有了自己的职位, 然后 使用适当的附件功能获取所需附件大小的信息。

    我会提出一个这样的函数来代替它;

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

    并付诸实践;

    <?php if ($photo = get_images('numberposts=1', true)): ?>
    
        <img src="<?php echo wp_get_attachment_url($photo[0]->ID); ?>" alt="fullimg" />
    
    <?php endif; ?>
    

    更新:在函数中输入错误-已修复。