我正在尝试了解如何使用此代码referencing the answer to the question here:
`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";}
代码中的序列化数据: 一个: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钩子,它会从上面代码生成的ID中获取与get_children相同的数据吗?
答案 0 :(得分: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。