WP程序函数:get_children()使用介绍

WP程序函数:get_children()使用介绍


     从Wordpress2.0开始,get_children()这个函数就存在了,随着Wordpress版本的升级,其返回值和参数都有了变化,本文介绍下这个函数以及与之相关的函数。


get_children()

Tips:在Wordpress中,post至少有两个含义:被称为文章的文章类型;全部文章类型,在本文中,我们分别称之为狭义上的 隔膜计量泵文章和广义上的文章。

get_children()可以返回一篇文章(广义上的文章,可以是狭义上的文章/页面/或者自定义文章类型)的附件、子页面或者回滚版本,如果有的话,如果没有的话,将会返回false(从Wordpress2.9开始)。

get_children()的工作原理和参数与 get_posts()类似。

这个函数有两个隔膜计量泵参数/数组参数:

    <?php $children_array = get_children( $args, $output ); ?>   

第一个参数是一个数组参数,第二个参数确定返回值的类型,说明如下:

(看到许多Wordpress爱好者介绍get_children()这个函数的时候,仅列出了默认的参数,但是不是很全面,这里我就多列一些)

    <?php $args = array(   
        'numberposts' => -1,//返回数目,-1为全部返回   
        'orderby'          => 'date',//排序:'none' ,'ID' ,'author' ,'title','date','modified' ,'parent' ,'rand' ,'comment_count' ,'menu_order' ,'meta_value' ,'meta_value_num'   
        'order' => 'ASC',   
        'post_parent'      => '0',//要查询的父文章的ID,默认为0   
        'post_type'        => '0',//任意文章类型,常用的是attachment, page, or revision   
        'post_status'      => 'publish',//状态:已发布   
        'post_mime_type'   => '',//mime类型,例如:image, video, video/mp4,默认为空   
         ); ?>  

第一个数组参数(该数组与get_children()的参数数组相比,大致相同):

该参数数组常用的参数(默认的使用参数):

    <?php $args = array(   
        'post_parent' => 0,   
        'post_type'   => 'any',    
        'numberposts' => -1,   
        'post_status' => 'any' ); ?>  

 

第二个参数:

可以是对象或者数组: OBJECT, ARRAY_A, ARRAY_N,默认为对象。

 

来自官方文档的用例:

    $images =& get_children( 'post_type=attachment&post_mime_type=image' );   
      
    $videos =& get_children( 'post_type=attachment&post_mime_type=video/mp4' );   
      
    if ( emptyempty($images) ) {   
        // no attachments here   
    } else {    http://www.tj.lss.gov.cn/
        foreach ( $images as $attachment_id => $attachment ) {   
            echo wp_get_attachment_image( $attachment_id, 'full' );   
        }   
    }   
      
    //  If you don't need to handle an empty result:   
      
    foreach ( (array) $videos as $attachment_id => $attachment ) {   
        echo wp_get_attachment_link( $attachment_id );   
    }  

官方文档的另外一个用例:

    <?php   
    function echo_first_image( $postID ) {   
        $args = array(   
            'numberposts' => 1,   
            'order' => 'ASC',   
            'post_mime_type' => 'image',   
            'post_parent' => $postID,   
            'post_status' => null,   
            'post_type' => 'attachment',   
        );   
      
        $attachments = get_children( $args );   
      
        if ( $attachments ) {   
            foreach ( $attachments as $attachment ) {   
                $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );   
      
                echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">';   
            }   
        }   
    }  

 

从Wordpress2.5开始,有了另外一个函数:

    <?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>  

如果有图像附件,就返回图像附件,否则会返回一个空字符串。

参数说明:

        $attachment_id,//附件的ID   
        $size,//附件的尺寸数组或尺寸变量关键字:thumbnail, medium, large 或者 full,数组的话,例如array(100,100),默认为 'thumbnail',从WP2.5开始,这个参数并不影响图像的图标了,它总是以原始尺寸返回。   
        $icon,//布尔值,是否使用图标来表示该图片。默认为false   
        $attr,//查询字符串或者是属性数组,如下:   
    $default_attr = array(   
        'src'   => $src,   
        'class' => "attachment-$size",   
        'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )),   
    );  

 

WordPress官方用例:

        <ul>   

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post();       

          

         $args = array(   

           'post_type' => 'attachment',   

           'numberposts' => -1,   

           'post_status' => null,   

           'post_parent' => $post->ID   

          );   

          

          $attachments = get_posts( $args );   

             if ( $attachments ) {   

                foreach ( $attachments as $attachment ) {   

                   echo '<li>';   

                   echo wp_get_attachment_image( $attachment->ID, 'full' );   

                   echo '<p>';   

                   echo apply_filters( 'the_title', $attachment->post_title );   

                   echo '</p></li>';   

                  }   

             }   

          

         endwhile; endif; ?>   

    </ul> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值