0

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');
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');
  • tnx for the comment @mrben522 sadly getting a 503 Service Unavailable :/ – Damir Jan 16 '19 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.