foreach all the post

时间:2016-04-21 作者:Mohamed Rihan

我如何获得下面的帖子标题。这就是我想要的

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);
}

2 个回复
最合适的回答,由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)

SO网友:Sumit

您的代码有许多错误:-

  • $thumbs 没有在任何地方使用
  • $img_url = $thumb[\'url\']; 未定义变量if(is_array($thumb) && $img_url) 总是false 因为第二点
  • $search_data = []; 应在回路外部
检查代码的正确部分

$search_data = [];
if($query->have_posts()):
    while($query->have_posts()):
        $query->the_post();

        $img_url = get_the_post_thumbnail_url();

        if(!empty($img_url)){
            $search_data[] = array(
                \'name\' => get_the_title(),
                \'thumb\' => $img_url,
                \'desc\' => get_the_content()
            );
        }       
    endwhile;
endif;
注:get_the_post_thumbnail_url() 仅在WordPress 4.4中提供

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果