To get the categories of a post in WordPress and echo only the first category, you can use the following code:

<?php
$category = get_the_category();
if ($category) {
    echo $category[0]->cat_name;
}
?>

This code retrieves the categories of the current post by using the 'get_the_category()' function. It then checks if there are any categories for the post. If there are, it echoes the name of the first category using '$category[0]->cat_name'.

How to Display Only the First Category of a WordPress Post

原文地址: https://www.cveoy.top/t/topic/qvzp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录