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
- Inject post (from specific category) between posts in Loop
- Getting key value of WP_Term object in wordpress
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- How to Remove Parents Category Permalink from Posts
- Looking to exclude blog posts from category Previous/Next buttons
- List all categories but exclude current post category on single post page
- Edit category output
- Custom Loop through category menu to include sub categories
- Different background-image by category
- Get posts with at least one category in common with current post?
- How to exclude specific category from the get_the_category(); array
- Add HTML Attributes To Anchor Tags In `wp_list_categories()` Function
- Where do posts get the sidebar from?
- combine Code 1 with Code 2
- get post based on category chosen in drop down – The ajax method
- Proper syntax or method for keeping url in modified isotope / category links
- how do I get a specific post from a post with a subcategory in WP
- WP grandchild categories in nested ul li
- How to show single category archive
- Get page that displays all children of taxonomy parent
- selected option if current category is the value
- get_template_part based upon post’s category
- Alert Bar section within WP loop is displaying even though there are no posts
- Different post styles depending on category
- Dilemma of Populating all the categories in a drop down list
- Define specific category name in PHP
- JSON REST API WordPress only showing first 10 categories
- Category title output before opening title tag
- Include Parent Term in wp_list_categories
- I’m trying to create an array with a foreach loop, but the array only stores the last item [closed]
- Get all categories post is not in
- Rewrite with pagination /foo/page/2/ to posts of a given category, page 2
- How to get category name or ID by post?
- How to get current post category details inside “loop”?
- How to hide posts of a specific custom category in WordPress?
- Post not populating for custom post type based on category selection
- Add new post in existing categories using wp_insert_post
- How to change logo by category
- Problem with custom loop and wp_list_pluck [closed]
- Listing titles and custom fields of entries within subcategories
- random display categories – change url
- Output category list inside array
- Do not duplicate posts with multiple categories in multiple loops
- Removing sidebar on category pages
- Define category ID using get_post_meta
- WordPress custom taxonomy
- go to home page when i select default in select-box
- Adding wp_list_categories to a specific div that was appended using jQuery
- Compare current post Category in select menu
- Remove Custom Post Category In WordPress Slug Using .Htaccess
- Woocommerce. Get a list of products in the current category on the archive page and product page
- Counting number of posts in multiple (sub)categories using shortcode
- Wrong Number of Products When Hiding Subcategory Products From Showing In Parent Category
- How do I edit the terms output args or array data?
- Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
- Change category thumbnail size
- Allow two posts (from different categories) to have the same slug
- Get main parent category for post (WordPress/Advanced Custom Fields)
- Display Visual Composer shortcode if a post belongs in specific categories
- admin panel – How to remove “delete” button from category editing page
- Category Page Displaying all Pages
- Get parent category name when only child category is applied to a product
- Multiple Tag Filtering
- Link to index.php from home.php?
- How does Permalink work with the AMP plugin after Removing Parent Permalink Catalog from Posts
- Polylang and template files
- Notice: Undefined variable
- Hide a specific category title from displaying on site
- Show subcategory name selected in specific category woocoommerce
- 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
- 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
- PHP drop down menu for my get_category child of
- Short code into PHP
- Need Quick Help With Product Category Page
- Need to adjust condition to say if I’m pulling categories from a post, to pull only the first one
- Rearrange get_categories array compared to another array
- 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?
- Error trying import one category on page
- dynamically filter by category via sub-menu
- Remove the_content From Loop
- Category List in Theme Options Page
- Get unique post in parent category
- Code that displays images – images get shown multiple times
- Incomplete term slugs output from a foreach loop with get_the_terms
- Display post of specific category on page
- Displaying categories
- Product Category Page in Full Width
- Display all categories (with link) of custom post type – WordPress
- How to limit number of posts in the certain category and exclude the oldest post automatically