0

Original title - Noob here. Wondering how to make use of this Astra theme function / filter to have different site identity logos on each page

The function is at line 1471 here (astra_replace_header_logo).

function astra_replace_header_logo( $image, $attachment_id, $size, $icon ) {
        $custom_logo_id = get_theme_mod( 'custom_logo' );
        if ( ! is_customize_preview() && $custom_logo_id == $attachment_id && 'full' == $size ) {
            $data = wp_get_attachment_image_src( $attachment_id, 'ast-logo-size' );
            if ( false != $data ) {
                $image = $data;
            }
        }
        return apply_filters( 'astra_replace_header_logo', $image );
    }

I know I should be writing functions with conditional statements in the functions.php file of my child theme, but beyond that I need a bit of guidance. I have used astra_has_custom_logo before to disable the logo for certain pages, and I made use of is_page() in that case (for if elses), which worked.

1

Here's couple examples on how to determine which page you're on within a filter and how to return different data based on it.

On these examples I'm using the apply_filter() part of the code you linked to to hook the custom code to the theme code. The custom function recieves the variabels from apply_filter() as parameters.

The custom function should return a value with a matching type and format as the first parameter.

In my example the option 1 uses a hard-coded array of pages and logos (with a image id and image source array examples). Option 2 assumes the page specific logo is saved as an integer in the page post_meta. The custom function returns custom image data, if there's a page match and the data is found. The function returns the first parameter as a fallback / default, meaning nothing was changed.

function my_prefix_filter_astra_replace_header_logo( $image ) {

  // option 1 - logo from hard coded value
  global $post;

  $logos_for_pages = array(
    'page-a' => 123,
    'page-b' => array( // using wp_get_attachment_image_src() return format
      '/wp-content/themes/my-theme/logos/logo-b.png', // url
      100, // width
      30, // height
      false // is_intermediate
    ),
  );

  if ( isset( $logos_for_pages[$post->post_name] ) ) {
    if ( is_int( $logos_for_pages[$post->post_name] ) ) {
      $replace_logo = wp_get_attachment_image_src( $logos_for_pages[$post->post_name], 'ast-logo-size' );
      if ( false !== $replace_logo ) {
        return $replace_logo;
      }
    } else {
      return $logos_for_pages[$post->post_name];
    }    
  }

  // option 2 - logo from post meta
  global $post;

  $logos_for_pages = array(
    'page-a',
    'page-b'
  );

  foreach ( $logos_for_pages as $page_name ) {
    if ( is_page( $page_name ) ) {
      $logo_id = get_post_meta( $post->ID, 'meta_key_for_page_specific_logo', true );      
      if ( $logo_id ) {
        $replace_logo = wp_get_attachment_image_src( $logo_id, 'ast-logo-size' );
        if ( false !== $replace_logo ) {
          return $replace_logo;
        }
      }
    }
  }

  return $image;
}
add_filter( 'astra_replace_header_logo', 'my_prefix_filter_astra_replace_header_logo' );
| improve this answer | |
  • Only seeing this now. Thank you very much. I have a few questions though. 1. Should return $image be at the end of both options or just the second one? 2. when in the code is a value being assigned to $post, and how does it have properties when none seem to have been defined in the code?(specifically the "post_name" property) Sorry if these are stupid questions. – ClubShrimp Aug 8 '19 at 14:49
  • @ClubShrimp 1) The $image at the end is the default value the filter returns, if no custom image is returned earlier from either option 1 or 2. So including it once at the end is enough. And you should either use option 1 or 2, and remove or comment out the other. 2) The values in $post ( a global variable) are set automatically to the current page/post by WP. No, not at all, we're all here to learn new things. – Antti Koskinen Aug 8 '19 at 20:10

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