我如何获得下面的帖子标题。这就是我想要的
function get_all_post_for_search() {
return array (
array(
\'name\' => "Hello Project",
\'thumb\' => \'/wp-content/uploads/2016/03/photo-1442473483905-95eb436675f1.jpeg\',
\'desc\' => \'Lorem ipsum dolor sit amet, consectetur adipisicing elit..\'
),
array(
\'name\' => "Another Project",
\'thumb\' => \'/wp-content/uploads/2016/03/photo-1449024540548-94f5d5a59230-1.jpeg\',
\'desc\' => \'Lorem ipsum dolor sit amet, consectetur adipisicing elit..\'
)
);
}
这是我的密码
function all_post_data(){
$args = array (
\'post_type\' => \'post\',
\'pagination\' => FALSE,
\'suppress_filters\' => FALSE
);
$query = new WP_Query( $args );
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
$post_id = get_the_id();
$the_title = get_the_title();
$thumbs = get_post_thumbnail_id();
$des = get_the_content();
$search_data = [];
$img_url = $thumb[\'url\'];
if(is_array($thumb) && $img_url){
$search_data[] = array(
\'name\' => $the_title,
\'thumb\' => $img_url,
\'desc\' => $des
);
}
endwhile;
endif;
wp_reset_postdata();
var_dump($search_data);
}
最合适的回答,由SO网友:Mohamed Rihan 整理而成
工作代码
function list_post_data(){
$args="SELECT * FROM wp_posts WHERE wp_posts.`post_type` = \'post\' AND wp_posts.`post_status` = \'publish\'";
$args = array(
\'post_type\' => \'post\'
);
$loop = new WP_Query( $args );
$array = array();
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$array[] = array(
//\'id\' => get_the_ID(),
\'name\' => get_the_title(),
\'thumb\' => $url,
\'desc\' => get_the_content()
);
endwhile;
// wp_reset_query();
ob_clean();
//echo json_encode($array);
var_dump($array);
exit();
}
结果是
array (size=2)
0 =>
array (size=3)
\'name\' => string \'test post\' (length=9)
\'thumb\' => boolean false
\'desc\' => string \'dfgrgsrga\' (length=9)
1 =>
array (size=3)
\'name\' => string \'Hello world!\' (length=12)
\'thumb\' => string \'http://lfigp-new.dev/wp-content/uploads/2016/03/valentine-cake_1x.png\' (length=69)
\'desc\' => string \'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!\' (length=85)