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; ?>
}
WP_Query
instead ofget_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'techo
or output directly in a shortcode or you'll break things<?php
tags line after your function. Removing that will solve syntax issues