header.php Wordpress Child Theme add a line of code

I want to create a header.php file in my child theme and add a line of code.  I know I can simple copy and paste the enitre header.php file and then add lines of code but that seems wrong to me.  Should I be writing hooks instead.  Here is the line of code and the line of code I want to add.

<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<img src="<?php echo esc_attr( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo" />
</a>

Open in new window


and

<p id="slogan"><?php bloginfo( 'description' ); ?></p>

Open in new window


All I want to do is insert the slogan line of code above the first line of code.  Can I write a hook to do this?  Please send the code as I don't know how to write hooks but want to learn.
livewirewebsolutionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Not sure how your code is hanging together but you can do something like this.

In your header.php file you do the following - add this to where the slogan should appear.

<?php echo empty($slogan) ? '' : $slogan;?>

Open in new window


Now in your child theme before you include header.php
$slogan = <<< HTML
<p id="slogan"><?php bloginfo( 'description' ); ?></p>

HTML;
include('header.php');

Open in new window

In other words you make provision for a $slogan in your header but it only output's it if it has been defined. You then define (set) it before you include the header. In pages that don't require a slogan it won't be defined and therefore nothing will be output in the slogan position.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
livewirewebsolutionsAuthor Commented:
yes, but what if the header.php file is updated in a theme update?  Won't the first code disappear?
0
Julian HansenCommented:
Not necessarily, word press child themes are by default meant to provide the facility to create theme changes that won't be overwritten.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.