簡體   English   中英

如何在 Wordpress 中添加短代碼以使用 ACF 查詢自定義帖子類型?

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

我正在為客戶構建網站,我需要使用自定義帖子類型和高級自定義字段構建游戲發布日歷。 萬事俱備,只差最后一步。

我需要能夠在添加新游戲時自動查詢 CPT,並通過短代碼將其拉入 WP Bakery Visual Composer 前端。 下面是添加到 functions.php 文件的代碼,但由於“<”,我收到語法錯誤。 任何人都可以幫助我使用正確的語法/格式來保存它並通過簡碼正確調用嗎?

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

您的代碼中有一個流氓 <?php 標簽。 從刪除它開始。 您也可以刪除底部不需要的。 嘗試這個:

// 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