Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “$wpdb->get_results”帖子显示在单独的ccs表格中-需要一个表格_Php_Wordpress_Css Tables_Meta Query - Fatal编程技术网

Php “$wpdb->get_results”帖子显示在单独的ccs表格中-需要一个表格

Php “$wpdb->get_results”帖子显示在单独的ccs表格中-需要一个表格,php,wordpress,css-tables,meta-query,Php,Wordpress,Css Tables,Meta Query,注意:我已经编辑了下面的代码,以反映现在的工作方式,感谢您的帮助 我的查询从我的自定义帖子和自定义字段中提取我需要的所有数据。字段填充,行和列看起来都很好。但是css表正在崩溃,正如Nathan在下面指出的那样,每个帖子都显示在一个单独的表中,我得到一行的标题,一行的标题,等等 我尝试了许多方法来对表进行编码,但每次显示的行都与此完全相同 我承认这很混乱,但我没有在表结构上采取任何快捷方式,以便准确地显示我获取它的方式,至少在正确的列中显示所有数据 <?php $querystr = "

注意:我已经编辑了下面的代码,以反映现在的工作方式,感谢您的帮助

我的查询从我的自定义帖子和自定义字段中提取我需要的所有数据。字段填充,行和列看起来都很好。但是css表正在崩溃,正如Nathan在下面指出的那样,每个帖子都显示在一个单独的表中,我得到一行的标题,一行的标题,等等

我尝试了许多方法来对表进行编码,但每次显示的行都与此完全相同

我承认这很混乱,但我没有在表结构上采取任何快捷方式,以便准确地显示我获取它的方式,至少在正确的列中显示所有数据

<?php
 $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'event'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT_K);

 ?>
 <div class="post" id="post-<?php the_ID(); ?>">
   <?php if ($pageposts): ?>
 <?php global $post; ?>
<table class="maee-table"><col width="20%"><col width="30%"><col width="20%"><col width="30%">
<tr><th class="column-head">Date</th>
<th class="column-head">Name</th>
<th class="column-head"> Audience</th>
<th class="column-head">Submitting partner</th>
</tr>
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

   <tr>
     <td class="maee-row"><?php rwmb_the_value( 'prefix-event_date1');?></td> 
 <td class="maee-row"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
      <td class="maee-row" ><?php rwmb_the_value( 'prefix-event_audience1');?></td>
<td class="maee-row"> <?php the_category(', ') ?></td>
   </tr>
  <?php endforeach; ?>
  </table>
 </div>
 <?php endif; ?>
</section>

您可以尝试以下方法:

    global $wpdb;

    $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'event'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";



    $content = '<table class="maee-table"><col width="20%"><col width="30%"> 
<col width="20%"><col width="30%">';

    $content .= '<tr><th>Date</th><th>Name</th><th>Audience</th><th>Submitting Partner</th></tr>';

    $results = $wpdb->get_results($querystr);

    global $post; // left this out before.  Try this.    

    foreach ($results as $post) {

        setup_postdata($post);

        $content .= '<tr>';
        $content .= '<td class="maee-row">' . rwmb_the_value( 'prefix- 
event_date1') . '</td>';
        $content .= '<td class="maee-row">' . the_title() . '</td>';
        $content .= '<td class="maee-row">' . rwmb_the_value( 'prefix- 
event_audience1') . '</td>';
        $content .= '<td class="maee-row">' . the_category(', ') . '</td>';
        $content .= '</tr>';
    }
    $content .= '</table>';

    // return the table
    echo $content;

您没有在单个表中显示结果。您正在为每个帖子创建一个新表。是的,这正是问题所在。也许我应该重新表述我的标题问题:如何解决这个问题,使之成为一个表?将echo作为表的行移到foreach循环之前,将echo作为结束表标记的行移到endforeach之后……感谢您的回答。我试了一下,但结果是一样的。例如,我尝试了一些其他配置,将整个标题分组移动到该位置。仍然是相同的结果。请用您对代码所做的修改更新问题,以便我们可以看到更改了什么。这显然更优雅,所以谢谢您!它只创建了一个表,但没有拉入任何数据。我用的代码把一切都拉了进去。但我会在这附近玩。再次感谢!在我的示例中,我省略了global$post,将其添加到了中。这对于设置postdata很重要,所以不妨尝试一下。这可能就是为什么你的例子得到了数据,而这一个没有。