0

我正在尝试找出如何使用此代码来引用此处问题的答案:

`global $post;
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
    $atts = shortcode_parse_atts( $match[3] );
    $attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}` 

检索与 get_children() 相同的数据?目前,只检索 id,但我尝试过 get_post() 和 get_posts()。

get_children 数组中的序列化数据:

{i:229;O:7:"WP_Post":24:{s:2:"ID";i:229;s:11:"post_author";s:1:"1";
s:9:"post_date";s:19:"2012-12-27 21:01:49";s:13:"post_date_gmt";
s:19:"2012-12-27 21:01:49";s:12:"post_content";s:0:""
;s:10:"post_title";s:8:"DSCN0703";s:12:"post_excerpt";
s:0:"";s:11:"post_status";s:7:"inherit";
s:14:"comment_status";s:6:"closed";
s:11:"ping_status";s:4:"open";s:13:"post_password";s:0:"";
s:9:"post_name";s:10:"dscn0703-6";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";
s:13:"post_modified";s:19:"2012-12-27 21:01:49";s:17:"post_modified_gmt";
s:19:"2012-12-27 21:01:49";s:21:"post_content_filtered";s:0:"";
s:11:"post_parent";i:223;s:4:"guid";s:81:"http:/exampleurl.com/wp-content/uploads/2012/12/DSCN07031.jpg";s:10:"menu_order";i:1;s:9:"post_type";s:10:"attachment";s:14:"post_mime_type";s:10:"image/jpeg";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}

来自代码的序列化数据:a:7:{i:0;s:3:"229";i:1;s:3:"225";i:2;s:3:"228";i:3 ;s:3:"230";i:4;s:3:"226";i:5;s:3:"227";i:6;s:3:"232";}

有人可以指出 wp 钩子,它会给我与上面代码生成的 ids 中的 get_children 相同的数据吗?

4

1 回答 1

0

不是 100% 确定你在追求什么,但这是我使用的代码的副本(基于该答案并添加了一些内容)......它可能会有所帮助

 $post_subtitrare = get_post( $post->ID );
            $content = $post_subtitrare->post_content;
            $pattern = get_shortcode_regex();
            preg_match( "/$pattern/s", $content, $match );
            if ( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
                $atts = shortcode_parse_atts( $match[3] );
                $images = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
            }

            if ( $images ) :                
                $first_element = array_shift(array_values($images));
                if ( ! is_object($first_element)) {
                    $image_count = count( $images );
                    $first_image = $images[0];
                    $featured = wp_get_attachment_image( $first_image, 'blog-featured-image' );
                }
                else {
                    $image_count = count( $images );
                    $first_image = array_shift(array_values($images));
                    $imageID = $first_image->ID;
                    $featured = wp_get_attachment_image( $imageID, 'blog-featured-image' );
                }

...这给了我一些我可以使用的变量,包括 $featured。

于 2013-02-01T17:51:01.363 回答