Hello I've made some options for the customizer to change where the logo is displayed and the position of it. To avoid repeating the same piece of code I'm wondering if it's possible to put the logo code in a file (along with other pieces of code that I will be doing a similar process with) and echo it in different divs based on conditions.
Son in my header it would look like this:
<header id="header" class="clear" role="banner">
<div class="column column-one four-quarter">
<div class="container clear">
<?php if( get_theme_mod( 'mlwd_logo_layout', 'insideheader' ) == 'insideheader' ) && get_theme_mod('mlwd_logo_position_y', 'top') :
echo // Piece of code from code.php file
endif ?>
</div>
</div>
<div class="column column-two four-quarter">
<div class="container clear">
<?php if( get_theme_mod( 'mlwd_logo_layout', 'insideheader' ) == 'insideheader' ) && get_theme_mod('mlwd_logo_position_y', 'bottom') :
echo // Piece of code from code.php file
endif ?>
</div>
</div>
</header>
And the file would be this:
<div id="logo">
<?php if( get_theme_mod( 'mlwd_logo_image') ) : ?>
<img src="<?php echo esc_url(get_theme_mod('mlwd_logo_image')); ?>" alt="Logo"/>
<?php else: ?>
<h1>
<a href="<?php echo home_url(); ?>" title="<?php bloginfo('name'); ?>">
<?php bloginfo('name'); ?><?php echo $langblog;?>
</a>
</h1>
<p>
<?php bloginfo('description'); ?><?php echo $langblog;?>
</p>
<?php endif ?>
</div>
Is it possible to do something like this ?
Many thanks
Hello I've made some options for the customizer to change where the logo is displayed and the position of it. To avoid repeating the same piece of code I'm wondering if it's possible to put the logo code in a file (along with other pieces of code that I will be doing a similar process with) and echo it in different divs based on conditions.
Son in my header it would look like this:
<header id="header" class="clear" role="banner">
<div class="column column-one four-quarter">
<div class="container clear">
<?php if( get_theme_mod( 'mlwd_logo_layout', 'insideheader' ) == 'insideheader' ) && get_theme_mod('mlwd_logo_position_y', 'top') :
echo // Piece of code from code.php file
endif ?>
</div>
</div>
<div class="column column-two four-quarter">
<div class="container clear">
<?php if( get_theme_mod( 'mlwd_logo_layout', 'insideheader' ) == 'insideheader' ) && get_theme_mod('mlwd_logo_position_y', 'bottom') :
echo // Piece of code from code.php file
endif ?>
</div>
</div>
</header>
And the file would be this:
<div id="logo">
<?php if( get_theme_mod( 'mlwd_logo_image') ) : ?>
<img src="<?php echo esc_url(get_theme_mod('mlwd_logo_image')); ?>" alt="Logo"/>
<?php else: ?>
<h1>
<a href="<?php echo home_url(); ?>" title="<?php bloginfo('name'); ?>">
<?php bloginfo('name'); ?><?php echo $langblog;?>
</a>
</h1>
<p>
<?php bloginfo('description'); ?><?php echo $langblog;?>
</p>
<?php endif ?>
</div>
Is it possible to do something like this ?
Many thanks
Share Improve this question asked May 9, 2019 at 11:14 MarkMark 31 bronze badge 1- Welcome to WordPress Stack Exchange :-)! We love to help. Please have a thorough read through codex.wordpress/Theme_Development and ask a new question when you got something up and running and it still doesn't work. Of course you can put your code into template partials (aka template parts) or use one of the existing templates (what about header.php). Many thanks – norman.lol Commented May 9, 2019 at 11:53
1 Answer
Reset to default 1You can do this by putting your piece of code in a function and call it where required just like any template tag.
<?php
function the_logo(){ ?>
<div id="logo">
<?php if( get_theme_mod( 'mlwd_logo_image') ) : ?>
<img src="<?php echo esc_url(get_theme_mod('mlwd_logo_image')); ?>" alt="Logo"/>
<?php else: ?>
<h1>
<a href="<?php echo home_url(); ?>" title="<?php bloginfo('name'); ?>">
<?php bloginfo('name'); ?><?php echo $langblog;?>
</a>
</h1>
<p>
<?php bloginfo('description'); ?><?php echo $langblog;?>
</p>
<?php endif ?>
</div>
<?php } ?>
And the header would look like
<header id="header" class="clear" role="banner">
<div class="column column-one four-quarter">
<div class="container clear">
<?php if( get_theme_mod( 'mlwd_logo_layout', 'insideheader' ) == 'insideheader' ) && get_theme_mod('mlwd_logo_position_y', 'top') :
// Piece of code from code.php file
the_logo();
endif ?>
</div>
</div>
<div class="column column-two four-quarter">
<div class="container clear">
<?php if( get_theme_mod( 'mlwd_logo_layout', 'insideheader' ) == 'insideheader' ) && get_theme_mod('mlwd_logo_position_y', 'bottom') :
// Piece of code from code.php file
the_logo();
endif ?>
</div>
</div>
</header>
I hope this may be helpful,