php - Automatically prepending '#' to slug in child page permalinks -
problem
i'd have child page url http://www.foo.com/fooparent/foochild automatically turns http://www.foo.com/fooparent/#foochild
could done htaccess rewrite rules or kind of wordpress hook?
use case
i have parent pages loading child pages content in template. it's utilizing onepage layout anchor links scrolling down page each section navigate. there cases permalinks of child pages exposed , break onepage functionality. child permalink should load it's parent page , add use slug #anchor in url.
i looping through categories, getting link, , if subcategory use regular expression replace last / #.
# code display category links foreach(wp_list_categories() $category) { $name = $category->name; $link = get_category_link($category->term_id); if($category->parent) { // parent isn't 0, lets change link have anchor $link = preg_replace('~/([^/]+)/?$~', '#$1', $link); } // output $name/$link } the regex /([^/]+)/?$ matches /, followed non / characters in capture group (the anchor), followed optional trailing slash , end of string ($). can replace match pound , anchor saved in our first capture group (#$1).
update:
as prefix, can't tell documentation whether or not get_the_category() gets current category category template..but let assume does. can this:
# code redirect away subcategory pages $category = get_the_category(); // not sure if works // directly accessing child category, redirect if($category->parent) { $link = get_category_link($category->term_id); $link = preg_replace('~/([^/]+)/?$~', '#$1', $link); header("http/1.1 301 moved permanently"); header("location: $link"); exit; } links:
wp_list_categories()categoryobject's membersget_category_link()get_the_category()
Comments
Post a Comment