• Resolved brian.nicholson

    (@briannicholson)


    I have a parent theme that is used on about 6 child themes. Each child theme represents a different company.

    I’m adding code in my parent theme’s functions.php to insert <meta property="og:image" content="https://mydomain/mypic.png"/> into the header so Facebook grabs the right thumbnail. The code is available in several places, but I grabbed it from https://bit.ly/uUuIYZ.

    The code is working fine when my post has a thumbnail. But the code also allows me to specify a default image so that if a post doesn’t have a thumbnail, it grabs the default image. That’s where my problem is happening.

    $default_image="https://example.com/image.jpg";

    Instead of specifying a single static image for all child themes, as shown in the code, I want to use a variable from each child theme. So in my Company A child theme’s functions.php, I have added

    define ( 'LOGO', '/company_A_Logo.png');

    I was hoping to then just use this in the parent theme:

    $default_image= $LOGO;

    But it’s not working; in my meta tag, it’s generating content=””. I think this is either a basic syntax error or perhaps an issue with the accessibility of the variable. Thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try using a function instead of a definition:

    function theme_logo() {
    return 'https://domain.com/company_A_Logo.png'; // Note the full url
    }

    then in the appropriate template file, use <?php echo theme_logo();?>.

    Thread Starter brian.nicholson

    (@briannicholson)

    Thanks, esmi. Worked great. I can now recover from the repeated banging my head against the wall. I should have turned to the forums earlier.

    I ended up using

    return get_option('siteurl') . '/remainder_of_path/company_A_logo.png';

    in case I ever change the site URLs (which we recently did, and hopefully won’t do again soon).

    I ended up using

    Try return esc_url( home_url() ). '/remainder_of_path/company_A_logo.png'; instead. That’s better practice ™ than using get_option.

    Thread Starter brian.nicholson

    (@briannicholson)

    Thanks. I went back and updated the code on all the child themes.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Referencing a child theme's variable in a parent theme’ is closed to new replies.