最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Echo piece of code from file

programmeradmin1浏览0评论

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,

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论