最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - CPT in a shortcode

matteradmin9PV0评论

I'm having trouble with putting my custom post type content in the shortcode and was wondering can someone help me with this.

I need this in shortcode:

   // 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');

I'm having trouble with putting my custom post type content in the shortcode and was wondering can someone help me with this.

I need this in shortcode:

   // 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');
Share Improve this question asked Jan 16, 2019 at 12:29 DamirDamir 1131 gold badge1 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should use ob_start() and ob_get_clean() for that. Also it looks like you're not using any attributes so you can ditch the call to 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');

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far