如何从Wordpress中的帖子类型获取所有帖子

时间:2016-01-27 21:22:41

标签: php mysql wordpress woocommerce

我尝试使用函数get_post_types从数据库获取所有产品,但我得到一个空数组:

 <?php

   $list=mysql_query("SELECT * FROM wp_posts where post_type='product'");

   if (empty($list)) {
     echo "NONE";
   }
?>

2 个答案:

答案 0 :(得分:1)

您可以使用标准的WP_Query方法:

$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$product_query = new WP_Query( $args );

if( $product_query->have_posts() ){
    while( $product_query->have_posts() ){
        $product_query->the_post();
        if( has_post_thumbnail() ){
            the_post_thumbnail();
        }
    } 
}

答案 1 :(得分:1)

尝试:

<h2 style="text-align:center;" id="yui_3_17_2_1_1452876314695_505"> And we’re only just beginning to see<br>
the tip of what that looks like: <br><br>
 It’s the sharing economy, internet of things,  the <br>
 quantified self,  the possibilities of virtual reality&nbsp;and robotics. It’s even shaping ideas about global
  leadership.And underneath all of this, there’s a more fundamental shift taking place, a rise
   in a new way of doing things.</h2>

检查 post_type 名称是否正确。

此查询将获取所有类型 product 的帖子,仅获取已发布的帖子,添加:

$products = get_posts(array(
                            'numberposts'   => -1,
                            'post_type'     => 'product',
                        ));

到数组参数。

从帖子中你可以获得很多信息: 获取发布图片

'post_status' => 'publish'

获取帖子的类别

$img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

$ cats 是一系列术语。