CPT in a shortcode

时间:2019-01-16 作者:Damir

我在将自定义帖子类型的内容放入快捷码时遇到了问题,想知道有人能帮我解决这个问题吗。

我需要这个短代码:

   // List judges shortcode
 function judges_shortcode($atts){
extract(shortcode_atts(array(

    ),$atts ) );

$value=\'<?php 
                            the_content(); ?>

                            <?php

                            $judges = get_posts(array(
                                \'numberposts\' => -1,
                                \'post_type\' => \'judges\',
                                \'meta_key\' => \'\',
                                \'meta_value\' => \'\'
                            ));


                                if ($judges) { ?>

                                    <div class="container">
                                        <div class="row">
                                    <?php foreach ($judges as $post) { setup_postdata( $post ); ?>
                                    <?php 
                                    $image = get_field(\'judge_image\');
                                    $link = get_field(\'judge_link\');
                                    $socialicon = get_field(\'judge_social_icon\');

                                     ?>
                                            <div class="col">
                                            <ul class="judges d-flex">
                                                <li class="judge">
                                                    <h3 class="text-center"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                                                    <img src="<?php echo $image[\'url\']; ?>" alt="" />
                                                    <p><?php the_content(); ?></p>
                                                    <a class="judge-link" target="_blank" href="<?php echo $link ?>"><?php echo $socialicon ?></a>
                                                </li>
                                            </ul>    
                                            </div>
                                    <?php } wp_reset_postdata(); ?>
                                        </div> <!-- end row -->
                                    </div> <!-- end container -->

                                 <?php } ?>\';


return $value;
}
add_shortcode(\'judges\',\'judges_shortcode\');

1 个回复
SO网友:mrben522

您应该使用ob_start()ob_get_clean() 为此。此外,您似乎没有使用任何属性,因此可以放弃对的调用shortcode_atts()

<?php
// List judges shortcode
function judges_shortcode($atts){

    ob_start();

    the_content();
    $judges = get_posts(array(
        \'numberposts\' => -1,
        \'post_type\' => \'judges\',
        \'meta_key\' => \'\',
        \'meta_value\' => \'\'
    ));

    if ($judges) { ?>

        <div class="container">
            <div class="row">
                <?php foreach ($judges as $post) { setup_postdata( $post ); ?>
                    <?php
                    $image = get_field(\'judge_image\');
                    $link = get_field(\'judge_link\');
                    $socialicon = get_field(\'judge_social_icon\');

                    ?>
                    <div class="col">
                        <ul class="judges d-flex">
                            <li class="judge">
                                <h3 class="text-center"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                                <img src="<?php echo $image[\'url\']; ?>" alt="" />
                                <p><?php the_content(); ?></p>
                                <a class="judge-link" target="_blank" href="<?php echo $link ?>"><?php echo $socialicon ?></a>
                            </li>
                        </ul>
                    </div>
                <?php } wp_reset_postdata(); ?>
            </div> <!-- end row -->
        </div> <!-- end container -->

    <?php };

    return ob_get_clean();
}
add_shortcode(\'judges\',\'judges_shortcode\');