Get Category Name from Wordpress Search Results Pa

2019-05-19 23:15发布

I stuck again :)

After a search query in a specific Blog Category (with redirect to the main WP BLOG page) my search url looks like

.../online-shop/?s=&category=new-posts-category&post_type=post

Unfortunately, i'm not able to echo the category Name (new-posts-category) in the search Results page. I guess i tried all possible ways i found on the Wordpress Codex (like Codex - wich works fine by the way in my woocommerce search results) page so far. Any idea how to solve this issue?

EDIT:

The closest i came so far is (Source)

$categories = get_the_category(); 
    // if ( ! empty( $categories ) ) {
    echo esc_html( $categories[0]->name );   
// }

But this one echoes the name of the first array element ([0]) of $categories. And i have no idea how to change this so it echos the current category name - it is by the way the only version wich outputs any result.

1条回答
孤傲高冷的网名
2楼-- · 2019-05-19 23:49

Try this within the post loop:

$postcat = get_the_category_list( '|','',$post->ID );

echo $postcat;

查看更多
登录 后发表回答