12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Sep 14, 2019

[Function] Output comma-separated top-level categories of the current post

Sridhar Katakam

This members-only tutorial provides the steps to define a custom function in WordPress that outputs categories of the current post that are not children of other categories - separated by commas.

Ex.:

For a post that belong to these categories:

the function will output:

Notice how "Child Category 1" does not get included since it is a child category.

If using Oxygen, add a text component like Text or Heading. Double click the text, click Insert Data button in the top toolbar, then PHP Function Return value and put in the Function Name, wpdd_post_categories_toplevel and then click INSERT.

Step 1

Install and activate Code Snippets plugin.

Go to Snippets > Add New.

Title: [Function] Output comma-separated top-level categories of the current post

Code:

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-down