如何获取同一帖子类型的第一个孩子的帖子id?

时间:2011-10-03 作者:m-torin

是否有一个函数可以让我获取第一个子级的帖子id(属于相同的帖子类型,而不是附件)?我总是可以通过wp\\u查询运行一个循环,但这似乎有些过头了/

这里的目标是构建一个显示第一个附加图像的函数My function会遍历父贴子的post\\u缩略图和附加图像,但当只有图像位于子贴子中时会停止工作

Edit: Here is my current working function. It works, but its not pretty. How can I clean it up?

/****************************************/
/* Grab first image of a post
/****************************************/  
function surfbird_grab_album_image() {
    global $post, $posts;

    $albumtitle =  get_the_title();

    if(has_post_thumbnail()) :
        return the_post_thumbnail(\'small\');

    else :

        //Grab image if parent post has one
        $args = array(
            \'order\'          => \'ASC\',
            \'orderby\'        => \'menu_order\',
            \'post_type\'      => \'attachment\',
            \'post_parent\'    => $post->ID,
            \'post_mime_type\' => \'image\',
            \'numberposts\'    => 1,
        );

        $attachments = get_posts($args);
        if ($attachments) :
            foreach ($attachments as $attachment) {
                echo wp_get_attachment_image($attachment->ID, \'small\', false, false);
                echo apply_filters( \'the_title\', $albumtitle );
            }
        else :

            //Search the child posts and grab the first image we find
             $albumgroups = get_posts( array( \'post_type\' => \'album\', \'post_parent\' => get_the_ID(), \'orderby\' => \'menu_order\', \'numberposts\' => -1 ) );
            foreach( $albumgroups as $post ) :  setup_postdata($post); 

            $postID = $post->ID;
            $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) {
                    return wp_get_attachment_image($attachment->ID, \'small\', false, false);
                }
            }
        endforeach;
        wp_reset_query();   

        endif;

    endif;
wp_reset_postdata();
}

1 个回复
SO网友:Tom J Nowell

而不是:

    $args = array(
        \'order\'          => \'ASC\',
        \'orderby\'        => \'menu_order\',
        \'post_type\'      => \'attachment\',
        \'post_parent\'    => $post->ID,
        \'post_mime_type\' => \'image\',
        \'numberposts\'    => 1,
    );
使用:

    $args = array(
        \'order\'          => \'ASC\',
        \'orderby\'        => \'menu_order\',
        \'post_type\'      => get_post_type($post),
        \'post_parent\'    => $post->ID,
        \'numberposts\'    => 1,
    );
您也可以使用post/page/etc或“any”

结束

相关推荐

Images don't show up

我刚刚安装了一个新的多站点安装。一切都像我一样工作,只是图像没有显示在任何子网站上。Any idea why the images don\'t show up?我正在共享Windows服务器(由Arvixe.com托管)中的IIS6上运行Wordpress 3.1.1。辅助站点位于子目录而不是子域中。主站点的图像与标题图像一样有效,但子站点中的图像不会显示。我知道这些图片在博客中出现时,正在正确上传。服务器上的dir文件夹。如果我这样导航它们,它们也会出现:http://www.example.com/