WordPress条件不起作用

时间:2012-05-07 作者:urok93

嗨,我正在构建一个jquery手风琴,它在我的网站上运行得很好。

然而,我现在试图引入条件,只在帖子和页面上启用它,但是它不起作用,似乎条件没有检测到它是一个页面。

代码如下:http://pastebin.com/9anYAKa1

function faq_start_shortcode( $atts, $content = null ) {
   return \'<div id="accordion">\';
}

function faq_question_shortcode( $atts, $content = null ) {
   return \'<div class="accordionButton">\' . $content . \'</div>\';
}

function faq_answer_shortcode( $atts, $content = null ) {
   return \'<div class="accordionContent">\' . $content . \'</div>\';
}

function faq_end_shortcode( $atts, $content = null ) {
   return \'</div>\';
}


if ( is_single() || is_page() ) {
add_shortcode(\'faqstart\', \'faq_start_shortcode\');
add_shortcode(\'question\', \'faq_question_shortcode\');
add_shortcode(\'answer\', \'faq_answer_shortcode\');
add_shortcode(\'faqend\', \'faq_end_shortcode\');
}

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

问题是你正在包装你的add_action() 在条件内部调用,但在add_action() 执行调用时,查询和查询条件都不可用。

解决方案:将条件置于回调中:

function faq_start_shortcode( $atts, $content = null ) {
    if ( is_single() || is_page() ) {
        return \'<div id="accordion">\';
    }
}

function faq_question_shortcode( $atts, $content = null ) {
    if ( is_single() || is_page() ) {
        return \'<div class="accordionButton">\' . $content . \'</div>\';
    }
}

function faq_answer_shortcode( $atts, $content = null ) {
    if ( is_single() || is_page() ) {
        return \'<div class="accordionContent">\' . $content . \'</div>\';
    }
}

function faq_end_shortcode( $atts, $content = null ) {
    if ( is_single() || is_page() ) {
        return \'</div>\';
    }
}


add_shortcode(\'faqstart\', \'faq_start_shortcode\');
add_shortcode(\'question\', \'faq_question_shortcode\');
add_shortcode(\'answer\', \'faq_answer_shortcode\');
add_shortcode(\'faqend\', \'faq_end_shortcode\');
(请注意,您可以使用is_singular() 作为( is_single() || is_page() ).

结束

相关推荐