Timeline for How to get current post category details inside "loop"?
Current License: CC BY-SA 4.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 7, 2019 at 4:12 | vote | accept | Mark20 | ||
Feb 4, 2019 at 16:20 | comment | added | Milo |
You misunderstood my last comment, WordPress sets $post for you, you don't need to, and you don't need to supply a post ID. Did you try just copying and pasting the code I gave you? I edited your original code and added the example code from above. Tested, working.
|
|
Feb 4, 2019 at 16:17 | history | edited | Milo | CC BY-SA 4.0 |
added 1078 characters in body
|
Feb 4, 2019 at 8:45 | comment | added | Mark20 | does a solution to this problem? | |
Feb 4, 2019 at 7:02 | comment | added | Mark20 | $post = get_post(); $category = get_the_category( $post->ID ); $category_url = esc_url( get_category_link( $category->term_id ) ); $category_name = esc_html( $category->name ); Not working, this is the only stuff I change. | |
Feb 4, 2019 at 6:47 | comment | added | Milo |
It shows the first (or only) category for each post in the loop. Calling the_post() populates a global variable, $post . Functions that can/have to be used in the loop use the contents of $post to get data for the current post.
|
|
Feb 4, 2019 at 6:08 | comment | added | Mark20 | Thanks for that, but you mentioned that the example shows "how to output the url and name of the first (or only) category" what about the current item of the loop? It doesn't seem to be given or maybe I missed it. | |
Feb 4, 2019 at 5:53 | comment | added | Milo |
the code in your comment doesn't work because you don't echo the value returned from esc_html . that function doesn't produce any output on its own.
|
|
Feb 4, 2019 at 5:42 | comment | added | Mark20 | <?php $categories = get_the_category(); ?> <div><p class="banner-article-topic"><?php esc_html( $categories[0]->name ); ?></p></div> Tried this, tried with an anchor, tried to introduce local $count variable, tried to make $categories global (my content is in a separate file) and access the array elements using a $count variable, all of these solutions did not work. | |
Feb 4, 2019 at 5:15 | history | answered | Milo | CC BY-SA 4.0 |