1

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;
    }

}
  • You could just use JS to change the links on page load? – Ashtmdu Oct 13 '17 at 14:22
  • Are you using template files for this USA CPT? – Ashtmdu Oct 13 '17 at 14:24
  • No at the moment I'm just using a plugin to implement and then working off the standard template files. – user102297 Oct 13 '17 at 15:57
  • See your problem is that you can't really ascertain what does and doesn't use 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 the is_singular() to check if you're on the right page to swap the link. Does that make sense? – Ashtmdu Oct 13 '17 at 16:06
  • Yes that does make sense. Thanks for your header example below, I will test it. – user102297 Oct 14 '17 at 8:18
0

Your filter generally seems fine.

I'd probably filter option_home instead of home_url, but it results in the same thing for most use cases (bloginfo("home") will work and so will get_home_url(), but get_option("home") will only work correctly with option_home).

  • Using option_home does have better results but some URLs are returning mysite.com/usa/usa/example-post/ – user102297 Oct 13 '17 at 15:58
  • Are they using custom permalinks by any change? If they have a permalink set to usa/example-post/, they'll add that to the home-url, therefore making it /usa/usa/example-post/. Otherwise, maybe those links are written in the template in a special way? – janh Oct 13 '17 at 16:27
  • The slug is rewritten for the custom post type, otherwise the name of the custom post type gets used, which is the same anyway. Without rewriting the slug I still get /usa/usa/example-post/ – user102297 Oct 14 '17 at 8:17
  • Maybe fix it the dirty & easy way and use output buffers, then replace /usa/usa/ with /usa/? – janh Oct 14 '17 at 8:21
0

I would suggest putting this in the file that contains your menu code, the header file usually. You can just check the page on load to see if the page your on needs the USA appending to the link.

Here is an example that could be used for any element within a page.

HEADER.PHP

<?php
if( is_singular('us') ) { 
  $url = site_url( '/usa/' );
} else { 
  $url = site_url();
}
?>
<nav>
  <a href="<?php echo $url; ?>" class="logo">MY LOGO</a>
</nav>

site_url() by default will just give you your website URL but it can also take an extra $path parameter to append to the end of the URL. This is why I have put '/usa/' inside site_url() above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.