简体   繁体   English

如何在 Wordpress 中添加短代码以使用 ACF 查询自定义帖子类型?

[英]How can I add a shortcode to query Custom Post Type with ACF in Wordpress?

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.我需要能够在添加新游戏时自动查询 CPT,并通过短代码将其拉入 WP Bakery Visual Composer 前端。 Below is the code added to the functions.php file, but I'm getting syntax errors because of "<".下面是添加到 functions.php 文件的代码,但由于“<”,我收到语法错误。 Can anyone help me with the proper syntax/formatting for getting it to save and call in properly via the shortcode?任何人都可以帮助我使用正确的语法/格式来保存它并通过简码正确调用吗?

FUNCTIONS.PHP code FUNCTIONS.PHP代码

// 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; ?>
}

There's a rogue <?php tag in your code.您的代码中有一个流氓 <?php 标签。 Start with removing that.从删除它开始。 Also you can remove unnecessary ones at the bottom.您也可以删除底部不需要的。 Try this:尝试这个:

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

$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();

endif; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM