Php 如何在任何页面上的任何位置打印某个wordpress类别?

Php 如何在任何页面上的任何位置打印某个wordpress类别?,php,wordpress,categories,Php,Wordpress,Categories,有人能告诉我,为了从Wordpress获得一个类别,然后在我想要的地方打印它,PHP会是什么样子吗 我猜我必须构建一个PHP函数。比如: 函数get_a_category(){ $category=get_the_category();您不必编写自己的函数;您必须使用和新查询来保持原始WP循环。在页面模板中运行此新查询以从“mycategory”获取第一篇文章。将showposts更改为您想要的文章数 <?php $my_query = new WP_Query('category_nam

有人能告诉我,为了从Wordpress获得一个类别,然后在我想要的地方打印它,PHP会是什么样子吗

我猜我必须构建一个PHP函数。比如:

函数get_a_category(){


$category=get_the_category();您不必编写自己的函数;您必须使用和新查询来保持原始WP循环。在页面模板中运行此新查询以从“mycategory”获取第一篇文章。将showposts更改为您想要的文章数

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>

您不必编写自己的函数;您只需使用和一个新查询来保持原始WP循环。在页面模板中运行此新查询以从“mycategory”获取第一篇文章。将showposts更改为您想要的文章数

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>

试试这个

function get_a_category() {
    $category = get_the_category();
    foreach ($category AS $key => $value) {
        $category_name[] = $value->cat_name;
    }
    $categories = implode(', ',$category_name);
    echo $categories;
}
试试这个

function get_a_category() {
    $category = get_the_category();
    foreach ($category AS $key => $value) {
        $category_name[] = $value->cat_name;
    }
    $categories = implode(', ',$category_name);
    echo $categories;
}