Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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 获取并显示WordPress上帖子的类别选项_Php_Wordpress - Fatal编程技术网

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对象数组。您需要在该数组上循环,并自己从项目中输出相关数据。