2016-05-11 54 views
1

我有帖子说是由AJAX更新的列表,并使用下面这个函数:显示类别名称通过的functions.php

function prefix_load_cat_posts() { 
    $cat_id = $_POST[ 'cat' ]; 
    $args = array (
     'cat' => $cat_id, 
     'posts_per_page' => -1, 
     'order' => 'DESC' 

     ); 

    $posts = get_posts($args); 

    ob_start(); ?> 

    <table class="table"> 
     <tbody> 
      <tr cellpadding="20"> 
       <th valign="bottom">Date</th> 
       <th valign="middle">Posted By</th> 
       <th valign="middle">Name</th> 
       <th valign="middle">Category</th> 
       <th valign="middle">Description</th> 
       <th valign="middle">Property/ Project</th> 
       <th valign="middle">File</th> 
      </tr> 

    <?php $category = get_the_category(); ?> 

    <?php foreach ($posts as $post) { 
     setup_postdata($post); ?> 

     <tr> 
      <td><?php echo get_the_date('j M Y',$post->ID) ; ?></td> 
      <td><?php the_author(); ?></td> 
      <td><?php echo get_field('attachment_name',$post->ID); ?></td> 

      <td><?php echo $category[0]->cat_name ; ?></td> 

      <td><?php echo get_field('attachment_desc',$post->ID); ?></td> 
      <td><?php echo get_field('property/_project',$post->ID); ?></td> 
      <td><a href="<?php echo get_field('attachment_document',$post->ID); ?>">Download PDF</a></td> 
     </tr> 

     <?php } wp_reset_postdata(); ?> 

     </tbody> 
    </table> 

     <?php $response = ob_get_contents(); 
     ob_end_clean(); 

     echo $response; 
     die(1); 
    } 
    ?> 

它的工作像一个享受,但是我不能得到的类别名称通过正确的栏目,你会看到我指的是在foreach语句之间的列。

我觉得我差不多没有完全相同,如果有人能告诉我我做错了什么,那会很好。

+0

它是什么显示vs你期待它显示什么? –

+0

@LucasBonner目前不显示任何东西 –

+0

如果使用'print_r($ category);'而不是'echo ...'会发生什么? –

回答

0

变化

<?php $category = get_the_category(); ?> 

<?php foreach ($posts as $post) { 
    setup_postdata($post); ?> 

<?php foreach ($posts as $post) { 
    setup_postdata($post); 
    $category = get_the_category(); ?> 
+0

这不起作用,仍然得到Array() –

+0

我怀疑这会有帮助,但如果你建立在我的答案上并使用'get_the_category($ post-> ID)'会发生什么? –

1

声明全局变量后

global $post; 

,并使用

get_the_category($post->ID); 

在设置发布数据之后的foreach内。它应该工作。

相关问题