我的短码出现了两次

时间:2017-01-09 作者:brothman01

The Problem:我制作了一个自定义的帖子类型和一个快捷码来显示它的信息。我以前做过,这不是问题。当我显示这个短代码时,短代码的内容在页面上重复两次。

The Code:我将这些操作和函数添加到插件主文件的底部(以plugin-name.php 在样板中)

function custom_post_type() {

$labels = array(
    \'name\'                  => _x( \'Carousel \', \'Post Type General Name\', \'text_domain\' ),
    \'singular_name\'         => _x( \'Carousel\', \'Post Type Singular Name\', \'text_domain\' ),
    \'menu_name\'             => __( \'Carousel\', \'text_domain\' ),
    \'name_admin_bar\'        => __( \'Carousel\', \'text_domain\' ),
    \'archives\'              => __( \'Carousel Archives\', \'text_domain\' ),
    \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
    \'all_items\'             => __( \'All Posts\', \'text_domain\' ),
    \'add_new_item\'          => __( \'Add New\', \'text_domain\' ),
    \'add_new\'               => __( \'Add New\', \'text_domain\' ),
    \'new_item\'              => __( \'New Carousel\', \'text_domain\' ),
    \'edit_item\'             => __( \'Edit Carousel\', \'text_domain\' ),
    \'update_item\'           => __( \'Update Carousel\', \'text_domain\' ),
    \'view_item\'             => __( \'View Carousel\', \'text_domain\' ),
    \'search_items\'          => __( \'Search Featured Blogs\', \'text_domain\' ),
    \'not_found\'             => __( \'Not found\', \'text_domain\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
    \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
    \'insert_into_item\'      => __( \'Insert into item\', \'text_domain\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
    \'items_list\'            => __( \'Items list\', \'text_domain\' ),
    \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
    \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
);
$args = array(
    \'label\'                 => __( \'Post Type\', \'text_domain\' ),
    \'description\'           => __( \'Post Type Description\', \'text_domain\' ),
    \'labels\'                => $labels,
    \'supports\'              => array( ),
    \'taxonomies\'            => array( \'category\', \'post_tag\' ),
    \'hierarchical\'          => false,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 5,
    \'show_in_admin_bar\'     => true,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => true,
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'capability_type\'       => \'page\',
    \'menu_icon\'                => \'dashicons-welcome-view-site\',
);
register_post_type( \'zeebo-carousel\', $args );

}

add_action( \'init\', \'custom_post_type\', 0 );


function carousel_shortcode_func( $atts ) {

$a = shortcode_atts( array(
\'id\' =>  false,
\'foo\' => \'something\',
\'bar\' => \'something else\',
), $atts );

ob_start();

?>

shortcode content here<br />

<?php

return ob_get_contents();

}
add_shortcode( \'carousel\', \'carousel_shortcode_func\' );
Actions I have Already Taken to Fix This:
1。更改主题2。停用所有其他插件。重写代码3。谷歌搜索如何使内容在每个短代码中只显示一次?

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

这更多的是关于php而不是wp,但是ob_get_contents 返回缓冲区而不清除它。因此,您可以获得两次缓冲区内容。使用ob_get_clean 返回缓冲区时擦除缓冲区。

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请