0

I am building a website for a client, and I needed to build a game release calendar using Custom Post Type and Advanced Custom Fields. Everything is built out, but the last step.

I need to be able to query the CPT automatically whenever a new game is added, and pull it into the WP Bakery Visual Composer front end via shortcode. Below is the code added to the functions.php file, but I'm getting syntax errors because of "<". Can anyone help me with the proper syntax/formatting for getting it to save and call in properly via the shortcode?

FUNCTIONS.PHP code

// Custom Game Releases Shortcode
add_shortcode( 'my_vc_php_output', 'game_release_listings');
function game_release_listings( $atts ) {
    <?php 

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'game_release'
));

if( $posts ): ?>    
    <ul>
        
    <?php foreach( $posts as $post ): 
        
        setup_postdata( $post );
        
        ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_field('release_date'); ?></a>
        </li>
    
    <?php endforeach; ?>
    
    </ul>
    
    <?php wp_reset_postdata(); ?>

<?php endif; ?>
}
3
  • 1
    you should use WP_Query instead of get_posts, it's a little faster and the documentation is much more detailed with more examples, and will probably answer your question for you. Also, shortcodes return their content as strings, your function is not doing that though which means your shortcode is badly broken. If you can't use a string variable use an output buffer, you can't echo or output directly in a shortcode or you'll break things
    – Tom J Nowell
    May 5 at 16:52
  • @TomJNowell Appreciate the response. In that case, what is the best route to take in your opinion so I can add this to the client's frontend?
    – M. Straw
    May 5 at 20:29
  • Looks like you reopen the <?php tags line after your function. Removing that will solve syntax issues
    – Bysander
    May 13 at 11:33

1 Answer 1

1

I ultimately figured out getting a shortcode to work by reworking the code to the following:

add_shortcode( 'gr_shortcode', 'gr_shortcode' );
function gr_shortcode() {
    $buffer = '<h3>Upcoming Titles</h3>';
    $q = new WP_Query(array(
        'post_type' => 'game_release',
        'posts_per_page' => 3
    ));
    while ($q->have_posts()) {
        $q->the_post();
        $buffer = $buffer.get_the_title().'<br>'.$buffer.the_field('release_date');
    
    }
    wp_reset_postdata();
    return $buffer;
}
1
  • 1
    the above code uses the_field which is an ACF function that outputs rather than returns its value, this would result in the release date appearing in the incorrect location in the shortcode as well as anything that has that output, usually at the beginning. ACF will have a version that returns instead of outputs, my guess is get_field. Also when I mentioned output buffers I meant PHP output buffers, e.g. php.net/manual/en/function.ob-get-clean.php but the $buffer variable works too
    – Tom J Nowell
    May 5 at 23:19

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.