2013-11-21 80 views
0

我正在尝试将woocommerce集成到非woocommerce主题。除了一个问题以外,情况很好。Woocommerce wordpress functions.php - 将div添加到div

我的主题有一个叫做块的div,它隐藏内容然后淡入/添加填充。我希望这也是在woocommerce页面中。

DIV的是这样的

<div id="block" <?php 
    if ($detect->isMobile()) {if($detect->isTablet()){ 
    /* Tab */ 
    if (get_field("search_override_header_position", "options") && get_field("search_tablet_header_position", "options")) {echo 'data-header-position="'.get_field("search_tablet_header_position", "options").'"';} else {echo 'data-header-position="'.$tablet_header_position.'"';}; 
} else { 
    /* Mob */ } 
    if (get_field("search_override_header_position", "options") && get_field("search_mobile_header_position", "options")) {echo 'data-header-position="'.get_field("search_mobile_header_position", "options").'"';} else {echo 'data-header-position="'.$mobile_header_position.'"';}; 
} else { 
    /* Desc */ 
    if (get_field("search_override_header_position", "options") && get_field("search_header_position", "options")) {echo 'data-header-position="'.get_field("search_header_position", "options").'"';} else {echo 'data-header-position="'.$header_position.'"';}; 
} 
?>>&nbsp;</div> 

使用这种http://docs.woothemes.com/document/third-party-custom-theme-compatibility/

我试图把这个在我的功能PHP和我得到的是一个白色的页面,很多语法错误的

remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10); 
remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10); 

add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10); 
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10); 
' 
function my_theme_wrapper_start() { 
    echo ' 

    <div id="block" <?php 
     if ($detect->isMobile()) {if($detect->isTablet()){ 
     /* Tab */ 
     if (get_field("search_override_header_position", "options") && get_field("search_tablet_header_position", "options")) {echo 'data-header-position="'.get_field("search_tablet_header_position", "options").'"';} else {echo 'data-header-position="'.$tablet_header_position.'"';}; 
    } else { 
     /* Mob */ } 
     if (get_field("search_override_header_position", "options") && get_field("search_mobile_header_position", "options")) {echo 'data-header-position="'.get_field("search_mobile_header_position", "options").'"';} else {echo 'data-header-position="'.$mobile_header_position.'"';}; 
    } else { 
     /* Desc */ 
     if (get_field("search_override_header_position", "options") && get_field("search_header_position", "options")) {echo 'data-header-position="'.get_field("search_header_position", "options").'"';} else {echo 'data-header-position="'.$header_position.'"';}; 
    } 
    ?>>&nbsp;</div><section id="main">'; 
} 

function my_theme_wrapper_end() { 
    echo '</section>'; 
} 

回答

0

您发布的代码存在语法错误,因为您在字符串中使用单引号而不是转义。该代码也试图注入PHP,这只有在输出代码稍后将由PHP处理器解释时才可能实现。

有两种方法可以解决这个问题。您可以创建一个输出缓冲区,然后通过管道发送输出并使用include语句处理,或者将其发送到php eval函数。由于安全风险,这些选项都不应该使用。

以下代码可能会提供您希望的结果,而不会造成PHP注入安全风险。

remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10); 
remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10); 

add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10); 
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10); 
//' Not sure why this quote was here.... 
function my_theme_wrapper_start() { 
    //echo ' 

// ending PHP processor instruction here will cause code to execute when current function is called 

?> 

<div id="block" <?php 
    if ($detect->isMobile()) {if($detect->isTablet()){ 
    /* Tab */ 
    if (get_field("search_override_header_position", "options") && get_field("search_tablet_header_position", "options")) {echo 'data-header-position="'.get_field("search_tablet_header_position", "options").'"';} else {echo 'data-header-position="'.$tablet_header_position.'"';}; 
} else { 
    /* Mob */ } 
    if (get_field("search_override_header_position", "options") && get_field("search_mobile_header_position", "options")) {echo 'data-header-position="'.get_field("search_mobile_header_position", "options").'"';} else {echo 'data-header-position="'.$mobile_header_position.'"';}; 
} else { 
    /* Desc */ 
    if (get_field("search_override_header_position", "options") && get_field("search_header_position", "options")) {echo 'data-header-position="'.get_field("search_header_position", "options").'"';} else {echo 'data-header-position="'.$header_position.'"';}; 
} 
?>>&nbsp;</div><section id="main"><?php 

// '; // no need for ending quote here but we do need to reopen the PHP processor instruction that we closed above instead of using an echo 
} 

function my_theme_wrapper_end() { 
    echo '</section>'; 
}