首页 > 解决方案 > 简码在帖子和页面中无法正常工作

问题描述

我注册了一个短代码,但它不能正常工作。当我将短代码放入我的帖子或页面时,它不起作用。但是当我直接使用 do_shortcode 函数时工作正常。

这就是我在我的函数文件中注册的内容

function world_news_section_shortcode($atts, $content ) {
    $atts = shortcode_atts(
        array(
            'section-heading'       => '',
            'section-more'      => '',
            'section-link'      => '',

        ), $atts, 'world_news'
    );
    ob_start(); 


    $world_news = new WP_Query(array(
        'post_type'         => 'post',
        'posts_per_page'    => 3,
        'category_name'     => 'world',
    ));?>

    <div class="lattest-post">
    <h2 class="lattest-post-head"><?php echo $atts['section-heading']; ?></h2>
     <a href="<?php echo $atts['section-link']; ?>" class="lattest-more"><?php echo $atts['section-more']; ?></a>


    <?php while ($world_news->have_posts()) : $world_news->the_post(); ?>
        <div class="lattest-post-contant">
               <?php the_post_thumbnail( 'image_post' ); ?>
                <h4><?php the_title(); ?></h4>
                <?php the_excerpt(); ?>
                <a href="<?php the_permalink(); ?>">Read More</a>

            </div>

    <?php endwhile;?>   
    </div>

    <?php $world_news = ob_get_clean(); 

    return $world_news; 
}
add_shortcode( 'world_news', 'world_news_section_shortcode' );

我在我的帖子和页面中使用了这个短代码。但不工作。

[world_news section-heading="World news new" section-more="More+" section-link=""]

但是当我将此代码直接使用到我的索引页面时,它工作得很好。

<?php echo do_shortcode('[world_news section-heading="World news new" section-more="More+" section-link=""]'); ?>

为什么我的短代码不能用于帖子和页面?

标签: phpwordpressshortcodewordpress-shortcode

解决方案


推荐阅读