如何将带变量的div添加到wordpress函数中?

时间:2016-05-25 08:36:13

标签: php html wordpress variables wordpress-plugin

嘿所有,我正在尝试将一些带有变量的div添加到函数中。我知道html需要包装,最好的方法是什么?这是我试图添加到函数中的代码。

    function add_before_sidebar( ) { 

    <div class="acro-sidebar-preview">
        <div class="acro-sidebar">
            <div class="image-container">
                <div id="profile-picture-preview" class="profile-picture" style="background-image: url(<?php print $picture; ?>)">
        </div>
    </div>
        <h1 class="acro-username"><?php print $fullname; ?></h1>
        <h2 class="acro-description"><?php print $description; ?></h2>
    <div class="icons-wrapper">

    <?php print '<a href="' . $twitter . '"><img src="' . plugins_url( 'img/twitter.png', __FILE__ ) . '" ></a>' ?>
    <?php print '<a href="' . $facebook . '"><img src="' . plugins_url( 'img/facebook.png', __FILE__ ) . '" ></a>' ?>
    <?php print '<a href="' . $google . '"><img src="' . plugins_url( 'img/googleplus.png', __FILE__ ) . '" ></a>' ?> 

        </div>
    </div>
</div>
}
add_action( 'get_sidebar', 'add_before_siderbar' );

以上所有代码都与我设置的这组变量相关联

<?php
    $picture = esc_attr( get_option('profile_picture') ); 
    $firstname = esc_attr( get_option('first_name') );
    $lastname = esc_attr( get_option('last_name') );
    $fullname = $firstname . ' ' . $lastname; 
    $description = esc_attr( get_option('user_description') ); 
    $twitter = esc_attr( get_option('twitter_handler') );
    $facebook = esc_attr( get_option('facebook_handler') );
    $google = esc_attr( get_option('google_handler') );
    ?>

代码本身在函数之外工作得很好。我熟悉在函数中用变量写一个div,但这比我习惯的要复杂一点。任何帮助,将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:1)

您可以这样写:

function add_before_sidebar( ) { 
?>
    <div class="acro-sidebar-preview">
        <div class="acro-sidebar">
            <div class="image-container">
                <div id="profile-picture-preview" class="profile-picture" style="background-image: url(<?php print $picture; ?>)">
        </div>
    </div>
        <h1 class="acro-username"><?php print $fullname; ?></h1>
        <h2 class="acro-description"><?php print $description; ?></h2>
    <div class="icons-wrapper">

    <?php print '<a href="' . $twitter . '"><img src="' . plugins_url( 'img/twitter.png', __FILE__ ) . '" ></a>' ?>
    <?php print '<a href="' . $facebook . '"><img src="' . plugins_url( 'img/facebook.png', __FILE__ ) . '" ></a>' ?>
    <?php print '<a href="' . $google . '"><img src="' . plugins_url( 'img/googleplus.png', __FILE__ ) . '" ></a>' ?> 

        </div>
    </div>
</div>
<?php }
add_action( 'get_sidebar', 'add_before_siderbar' );