Forums

[resolved] Referencing a child theme's variable in a parent theme (5 posts)

  1. brian.nicholson
    Member
    Posted 7 months ago #

    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="http://mydomain/mypic.png"/> into the header so Facebook grabs the right thumbnail. The code is available in several places, but I grabbed it from http://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="http://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?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    Try using a function instead of a definition:

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

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

  3. brian.nicholson
    Member
    Posted 7 months ago #

    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).

  4. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    I ended up using

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

  5. brian.nicholson
    Member
    Posted 7 months ago #

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

Reply

You must log in to post.

About this Topic