0

Building a WP site where two parent categories are styled to look like two distinct sections/sites.

I'm using the custom logo (set in Appearance/Customise) and using the function below I can successfully replace this with a custom logo on specific pages and categories. How can I also include a custom link rather than site home?

function change_logo_on_category($html) {

  if (is_page($page = '136'))

    $html = preg_replace('/<img(.*?)\/>/', '<img src="http://customlogo.png" class="custom-logo" alt="" itemprop="logo" />', $html);

  elseif ( in_category( 29 ) || post_is_in_descendant_category( 29 ))

    $html = preg_replace('/<img(.*?)\/>/', '<img src="http://customlogo.png" class="custom-logo" alt="" itemprop="logo" />', $html);

  return $html;   
}
add_filter('get_custom_logo','change_logo_on_category');
1
  • maybe you should think about a solution via css, each page have a single css class from the ID of the page and you can add the image via this selector.
    – bueltge
    Commented Jun 8, 2018 at 10:11

1 Answer 1

0

You are not using all possibilities of the get_custom_logo filter. As you can see from the source not only the $html is passed to it, but also the $blog_id. You can use the latter to do a preg_replace on the link like this:

add_filter ('get_custom_logo','wpse305624_change_logo_link',10,2);
function wpse305624_change_logo_link ($html,$blog_id) {
  $homelink = get_home_url ();
  $bloglink = get_permalink ($blog_id); // or anything else, for instance the blogs's parent
  $html = preg_replace ($homelink,$bloglink,$html);
  return $html;
  }

By the way, you could also use the filter on get_theme_mod (called at the beginning of get_custom_logo) to modify the returned image id in the first place, so you don't have to do a preg_replace afterwards. Unfortunately I don't see a similarly elegant way to manipulate the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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