Another way to go about it instead of substring, would be to use implode/explode & array filter.
echo implode(" » ", array_filter( explode(" » ", get_category_parents( $cat, true, " » ") ) ) );
Breaking that down:
Start with your original string.
$string = get_category_parents( $cat, true, " » ");
Explode the string using your divider to create an array of string parts.
$string_parts = explode(" » ", $string);
Filter out any empty array parts.
$string_parts = array_filter( $string_parts );
Recombine using your divider.
$string = implode(" » ", $string_parts);
Then combine them all together to create my one line solution at the top. The code you have using substring will get the job done, but it’s not the way I’d do it.
Related Posts:
- Display current category title on category page
- What does the token %1$s in WordPress represent [closed]
- Inject post (from specific category) between posts in Loop
- exclude multiple terms using get_terms() function
- Getting key value of WP_Term object in wordpress
- retrieve thumbnail from post ID of best selling product in category
- Custom HTML structure in wp_list_categories
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Allow authors to post only in categories they create in WordPress
- Archive listing of posts by publish year (multiple years)
- Add custom field to attachments in a specific category
- Multiple Tag Filtering
- Trying to show the category of a post in the post display
- Create category after theme setup and modify the default one
- Link to index.php from home.php?
- How does Permalink work with the AMP plugin after Removing Parent Permalink Catalog from Posts
- How to Link to category titles if it is in the single post content?
- Polylang and template files
- How can i show specific Category List?
- Notice: Undefined variable
- Hide a specific category title from displaying on site
- How to show category just on specific pages?
- Show subcategory name selected in specific category woocoommerce
- How to filter specific element of an array in wordpress/php?
- Return to the beginning of the results of a for loop
- Exclude category and post from loop in custom category.php
- Admin backend, show post from a category and exclude the posts from subcategories
- Populate Posts based on category selected using AJAX that means without page refresh
- All categories options or All categories not Populating
- random woocommerce categories are not showing when count enabled?
- Exclude a category slug on pagination
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- Custom content using in_category
- How to Retrieve All Category Images on Front-End
- Displaying 3 Category Posts differently
- PHP drop down menu for my get_category child of
- Short code into PHP
- PHP get_category() function redeclared
- Code works on page-example.php by not category-example.php
- Need Quick Help With Product Category Page
- List categories assigned to a post
- get_category display only 1
- Unable to get Post Category Name and URL
- How to list categories by page id in wordpress
- Need to adjust condition to say if I’m pulling categories from a post, to pull only the first one
- WordPress using get_term to retreive slug not working as expected
- How to add style in functions.php depending on conditions?
- Showing the project type in HREF
- How do I display Category titles on a WP Page?
- Rearrange get_categories array compared to another array
- Front end post submissions do not get submitted in the category
- problem with category menu, it doesn’t want to show link
- Make an array listing custom taxonomy
- Get category id for a custom category and display it in a class
- Saving category as insert post
- Trying to exclude posts from a category on the home page
- Search in particular category in wordpress
- Query seems to be duplicated
- Top menu disapears in Category Pages when filtering using ACF
- Is there a way to randomize and connect a background and header image?
- Trying to see if page is category or single and displaying title with appropriate heading tag
- wp_get_archive for category returning different URLs on different but similar sites
- Showing Subcategory’s Name and Link Instead of Parent Category’s
- Error trying import one category on page
- !in_array doesnt recognize category
- get_category only returning details for ‘uncategorized’
- dynamically filter by category via sub-menu
- How to change links in the 3 main categories, on widget category, to a javascript function call
- Remove the_content From Loop
- Category List in Theme Options Page
- Get unique post in parent category
- how to set category name for a post
- Code that displays images – images get shown multiple times
- Incomplete term slugs output from a foreach loop with get_the_terms
- category & sub category order issues in wordpress
- Hide empty categories from get_categories
- Display post of specific category on page
- Displaying categories
- on category page get name of category as a string
- WordPress categories not working throughout site
- Fix incorrect related posts code snippet
- Remove /category/ from category (archive) page URLs (without using a plugin)
- Display a list of posts corresponding to a category
- Product Category Page in Full Width
- How to create categories out of a list of words?
- Get Products within Current Product Category
- How to insert the first letter in uppercase [closed]
- Display all categories (with link) of custom post type – WordPress
- Is there a way of displaying related posts from lowest hyrachical order (lowest child category to parent category)
- Hide child of parent categories already hidden with “get_terms_args” – Woocommerce
- Get category name of current post (CPT UI)
- How to limit number of posts in the certain category and exclude the oldest post automatically
- Exclude a category ID from the following function
- Count post with tags within one category?
- child category under correct parent
- Display featured posts first, then display all others within a specific category in WordPress
- Help with get_categories code
- Is it possible to go to the next post in the same category?
- Can’t assign menu parent id or menu item breaks
- Prevent publishing of uncategorized posts