I recently moved a parent category and its children from the default categories taxonomy to a new custom taxonomy. The data moved over just fine and posts are properly associated with the company name in the the new company taxonomy.
Before
- Taxonomy: Category
- Term: Company
- Child Term: Mozilla
After
- Taxonomy: Company
- Term: Mozilla
The problem I just ran into is that when I display the terms associated with the
post using the_terms
is builds the url as follows:
//mozilla
I knew I could fake the correct results by manually building the URL, but I wanted to understand why it was happening. What I was hoping to figure out was why the double slash was happening and how to fix it.
I recently moved a parent category and its children from the default categories taxonomy to a new custom taxonomy. The data moved over just fine and posts are properly associated with the company name in the the new company taxonomy.
Before
- Taxonomy: Category
- Term: Company
- Child Term: Mozilla
After
- Taxonomy: Company
- Term: Mozilla
The problem I just ran into is that when I display the terms associated with the
post using the_terms
is builds the url as follows:
http://example/company//mozilla
I knew I could fake the correct results by manually building the URL, but I wanted to understand why it was happening. What I was hoping to figure out was why the double slash was happening and how to fix it.
Share Improve this question asked Jan 30, 2015 at 18:42 Stephen S.Stephen S. 1,0292 gold badges13 silver badges21 bronze badges2 Answers
Reset to default 1First Assumption: That an extra slash was being added for some reason
To see what was going on, I wanted to see what was going on in the array. When I print_r()
the array I get the following:
Array (
[10] => stdClass Object (
[term_id] => 10
[name] => Mozilla
[slug] => mozilla
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => company
[description] =>
[parent] => 504
[count] => 8
[object_id] => 37085
[filter] => raw
)
)
Everything looked pretty normal until I noticed the [parent] => 504
value, which I knew was moved over when the terms were moved from Category taxonomy to the Company taxonomy (I thought it wouldn't matter at the time).
This new knowledge helped me to come to a new assumption.
Second & Correct Assumption: That there was something missing between the two slashes.
Instead of seeing the second slash as being extra, my new idea was that the non-existent parent was missing from url:
- Before: example/category/company/mozilla
- After: example/company/(missing parent)/mozilla
To test if this was the problem, I changed the database value [parent] => 504
to [parent] => 0
to remove any parent association and it worked as expected to create the correct url:
http://example/company/mozilla
Just if anynone else will be searching for the same. To fix the issue check your register_taxonomy() rewrite syntax. There should be no trailing slash. Note the '/product-category' without ending slash in the code sample below:
register_taxonomy(
'product-category',
array('product', 'post'), array(
'label' => __('Product Categories'),
'rewrite' => array('slug' => '/product-category'),
'hierarchical' => true,
'show_admin_column' => true,
)
);