通过functions.php显示类别名称
我有一个由AJAX更新并使用以下功能的帖子列表:通过functions.php显示类别名称,php,html,wordpress,categories,Php,Html,Wordpress,Categories,我有一个由AJAX更新并使用以下功能的帖子列表: function prefix_load_cat_posts () { $cat_id = $_POST[ 'cat' ]; $args = array ( 'cat' => $cat_id, 'posts_per_page' => -1, 'order' => 'DESC' ); $posts = get_posts( $args );
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);
}
?>
功能前缀\u加载\u目录柱(){
$cat_id=$_POST['cat'];
$args=数组(
“猫”=>$cat\u id,
“每页帖子数”=>-1,
“订单”=>“描述”
);
$posts=get_posts($args);
ob_start();?>
日期
邮寄人
名称
类别
描述
物业/工程
文件
它的工作就像一个对待,但我不能得到类别名称拉通过在正确的列,你会看到我所指的列之间的foreach语句
我觉得我就快到了,但还不完全到,如果有人能告诉我我做错了什么,那就太好了。改变
<?php $category = get_the_category(); ?>
<?php foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
到
声明全局post变量
global $post;
和使用
get_the_category( $post->ID );
设置后在foreach内部发布数据。它应该可以工作。它显示的是什么与您希望它显示的是什么?@LucasBonner它现在没有显示任何内容如果您使用
print\r($category);
而不是echo…
?@LucasBonner我得到这个输出-数组()我怀疑这是否有帮助,但是如果你在我的答案的基础上使用获取类别($post->ID)
,会发生什么呢?
get_the_category( $post->ID );