I would like to change the home_url for a custom post type that I have created.
The reason I would like to change the home_url is so that the site logo then links to a different URL when someone is viewing a custom post.
The custom post type is named 'usa', therefore I want to change the logo / home url to link to mysite.com/usa/
I have used the code below to achieve this, however I get errors because other links that make use of home_url, such as menu links, then have 'usa' appended to them, e.g mysite.com/usa/example-post/usa/
Does someone know a better implementation?
add_filter( 'home_url', 'custom_home_url' );
function custom_home_url( $url )
{
if( is_singular('us') )
return $url .'/usa';
else {
return $url;
}
}
home_url
in your functions.php and therefore, you can't pick which one should use the new URL instead of the original. What I would suggest is putting your code in the default header template file (where the menu code is). You can use theis_singular()
to check if you're on the right page to swap the link. Does that make sense?