Php 获取并显示WordPress上帖子的类别选项
我有一个博客索引页,它只显示分类法Php 获取并显示WordPress上帖子的类别选项,php,wordpress,Php,Wordpress,我有一个博客索引页,它只显示分类法参考资料下的所有文章 在资源下定义了几个类别。出于实验目的,标题为示例文章的博客文章将文章和eGuide分类 我试图在ul中显示类别(为每个类别生成一个新的li),但是get\u类别似乎没有呈现任何内容 我有以下标记: <?php global $post; while ( have_posts() ) : the_post(); ?> <div class="card"> <div class="categ
参考资料下的所有文章
在资源
下定义了几个类别
。出于实验目的,标题为示例文章
的博客文章将文章
和eGuide
分类
我试图在ul
中显示类别(为每个类别生成一个新的li
),但是get\u类别似乎没有呈现任何内容
我有以下标记:
<?php
global $post;
while ( have_posts() ) : the_post(); ?>
<div class="card">
<div class="categories">
<?php get_the_category($post->ID); ?>
<?php //the_tags('<ul><li>','</li><li>','</li></ul>'); ?>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
<ul>
<?php
foreach(get_the_category() as $category){
echo '
<li>
<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a>
</li>
';
}
?>
正如您所看到的,我以前的方法是通过标签(the_tags
),这很有效(为每个标签生成了一个新的li
)。但是,使用get_the_category
,我甚至无法显示类别。您可以尝试get_the_category_list()
函数,该函数默认情况下将在无序列表中显示帖子类别:
<?php
global $post;
while ( have_posts() ) : the_post(); ?>
<div class="card">
<div class="categories">
<?php echo get_the_category_list(); ?>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
您可以尝试获取_category_list()
函数,默认情况下,该函数将在无序列表中显示帖子类别:
<?php
global $post;
while ( have_posts() ) : the_post(); ?>
<div class="card">
<div class="categories">
<?php echo get_the_category_list(); ?>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
在misorude
的循环注释后成功解决了该问题
解决方案:
<?php
global $post;
while ( have_posts() ) : the_post(); ?>
<div class="card">
<div class="categories">
<?php get_the_category($post->ID); ?>
<?php //the_tags('<ul><li>','</li><li>','</li></ul>'); ?>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
<ul>
<?php
foreach(get_the_category() as $category){
echo '
<li>
<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a>
</li>
';
}
?>
在misorude
的循环注释后成功解决了该问题
解决方案:
<?php
global $post;
while ( have_posts() ) : the_post(); ?>
<div class="card">
<div class="categories">
<?php get_the_category($post->ID); ?>
<?php //the_tags('<ul><li>','</li><li>','</li></ul>'); ?>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
<ul>
<?php
foreach(get_the_category() as $category){
echo '
<li>
<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a>
</li>
';
}
?>
您使用的是默认类别还是自定义分类法?您的帖子令人困惑,您将分类法与类别混合在一起“但是get_类别似乎没有呈现任何内容”-此函数不生成任何输出,它返回一个WP_Term对象数组。您需要循环该数组,并自己从项输出相关数据。您使用的是默认类别还是自定义分类法?您的帖子令人困惑,您将分类法与类别混合在一起“但是get_类别似乎没有呈现任何内容”-此函数不生成任何输出,它返回一个WP_Term对象数组。您需要在该数组上循环,并自己从项目中输出相关数据。