1

I am new to wordpress themes and PHP. (I don't believe this is specific to WP) I would like to know the best way to store a block of HTML (with embeded PHP code) in a PHP variable.

<?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;
}
?>

Reading through many questions here leads me to believe that nowdoc and heredoc are not good choices. I have tried single quotes and escaping the single quotes inside. Same with double quotes. Both attempts resulted in error messages.

Any help here would be appreciated.

EDIT* The following code is what finally worked.

<?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
    }
}
?>

Thanks to TJohnW for pointing me in the right direction: processing the inline PHP instead of trying to insert it.

4

1 回答 1

1

I think I know what you are trying to do here. Give this a shot.

<?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;
}
?>
于 2013-01-26T05:41:22.030 回答