我创建了以下短代码:
function newsletter_signup_shortcode( $atts ) {
$post_type = get_post_type();
// Attributes
extract( shortcode_atts(
array(
\'location\' => get_permalink(),
\'show_title\' => \'yes\',
), $atts )
);
// Return the unfiltered content if we\'re not on a post.
if ( $post_type != \'post\' ) {
return $content;
}
ob_start();
?>
<!-- MailChimp Signup Form outputs here - code omitted for clarity -->
<?php
// Add the opt-in form after the post content.
return $content . ob_get_clean();
}
add_shortcode( \'newsletter_signup\', \'newsletter_signup_shortcode\' );
这在主页和博客帖子页面、帖子和边栏小部件中都非常有效。
但是,当我查看页面时,侧栏小部件不会显示短代码内容:
我错过了什么?
最合适的回答,由SO网友:TheDeadMedic 整理而成
如果当前“视图”不是一篇文章,则函数中有一个条件将跳过(返回)。还有一个未定义的变量$content
- 以下条件已删除,未定义的变量已修复(always, always have debugging 使用WordPress构建时打开):
function newsletter_signup_shortcode( $atts ) {
$post_type = get_post_type(); // Not sure if you still need this for your MailChimp template?
// Attributes
extract( shortcode_atts(
array(
\'location\' => get_permalink(),
\'show_title\' => \'yes\',
), $atts )
);
ob_start();
?>
<!-- MailChimp Signup Form outputs here - code omitted for clarity -->
<?php
return ob_get_clean();
}