E
E
Evgeniy2020-06-25 21:22:15
WordPress
Evgeniy, 2020-06-25 21:22:15

How to link category to posts?

Good afternoon. I ran into a problem that I can't seem to solve. The idea is this. Each category must have its own unique number and it must be divisible by the number of posts in this category and also recorded in each post of the category.
My tests:
Decided to do everything through custom fields. I set an arbitrary field for the category, but it is not displayed in the post card. Used the the_field() and get_field() functions.
After that I decided to go the other way. Write a number in the description of the category itself and display it in the post card. But then on the wp-kama website I found out that the category description is applied only on the page of the category itself.

Completely confused. Write who knows how it can be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-06-25
@azerphoenix

Decided to do everything through arbitrary fields. I set an arbitrary field for the category, but it is not displayed in the post card. Used the the_field() and get_field() functions.

To get started, I recommend reading the documentation for the ACF plugin.
To display a custom field created for a category in a post, you need to pass the ID of the current category as the second argument to the_field() and get_field() . Roughly speaking, in the template you need to get the id of the category that the current post belongs to and pass it as the second argument.
the_field('custom_field', 13 )
After that I decided to go the other way. Write a number in the description of the category itself and display it in the post card.

Not a very flexible solution, because if you need to reuse description for other purposes, then you will need to choose between functionality.
Each category must have its own unique number and it must be divisible by the number of posts in this category and also recorded in each post of the category.

1) did not quite understand the idea, or rather the final goal that you want to achieve.
2) In the case of using a custom field in the above way, you do not write this value to each post, but only display this value from the category in the template. If you need to record and store this value, then create another custom field for the post, update the value of this field based on the value of the custom category field and display it in the template

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question