如何在PHP变量中存储一块html(带有嵌入式PHP)?

时间:2013-01-26 05:23:05

标签: php html

我是wordpress主题和PHP的新手。 (我不相信这是WP特有的)我想知道在PHP变量中存储HTML块(带有嵌入的PHP代码)的最佳方法。

<?php
function rfttc_insert_nav_menu ($position){
$poswanted = 'above';
$nav_code = 
    '<nav id="site-navigation" class="main-navigation" role="navigation">
        <h3 class="menu-toggle"><?php _e( \'Menu\', \'twentytwelve\' ); ?></h3>
        <a class="assistive-text" href="#content" title="<?php esc_attr_e( \'Skip to content\', \'twentytwelve\' ); ?>"><?php _e( \'Skip to content\', \'twentytwelve\' ); ?></a>
        <?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'menu_class\' => \'nav-menu\' ) ); ?>
    </nav><!-- #site-navigation -->'

if($position == $poswanted)
    return $nav_code;
}
?>

在这里阅读许多问题让我相信nowdoc和heredoc不是好的选择。我试过单引号并转义里面的单引号。与双引号相同。两次尝试都导致错误消息。

任何帮助都将不胜感激。

修改* 以下代码最终起作用。

<?php
function rfttc_insert_nav_menu ($position){
    $poswanted = 'below';
    if($position == $poswanted){?>
        <nav id="site-navigation" class="main-navigation" role="navigation">
        <h3 class="menu-toggle"><?php __( 'Menu', 'twentytwelve' )?> </h3>
        <a class="assistive-text" href="#content" title="<?php
        esc_attr_e( 'Skip to content', 'twentytwelve' );
        ?>"><?php  _e( 'Skip to content', 'twentytwelve' );?>"</a>
        <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );?>
        </nav><!-- #site-navigation -->
        <?php
    }
}
?>

感谢TJohnW指出我正确的方向:处理内联PHP而不是尝试插入它。

1 个答案:

答案 0 :(得分:1)

我想我知道你在这里要做什么。试一试。

<?php
function rfttc_insert_nav_menu ($position){
   $poswanted = 'above';
   $nav_code = 
  '<nav id="site-navigation" class="main-navigation" role="navigation">
   <h3 class="menu-toggle">'. _e( 'Menu', 'twentytwelve' ) . '</h3>
   <a class="assistive-text" href="#content" title="' . esc_attr_e( 'Skip to content', 'twentytwelve' ) . '">' . _e( 'Skip to content', 'twentytwelve' ) . '</a>' . wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ) . '</nav><!-- #site-navigation -->';

  if($position == $poswanted) return $nav_code;
}
?>