php - Echo piece of code from file

Hello I've made some options for the customizer to change where the logo is displayed and the position of it. To av

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
Add a comment  | 

1 Answer 1

Reset to default 1

You 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,

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745509810a4630720.html

相关推荐

  • php - Echo piece of code from file

    Hello I've made some options for the customizer to change where the logo is displayed and the position of it. To av

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信