簡體   English   中英

相關產品ACF WordPress

[英]Related Products ACF WordPress

我沒有使用WooCommerce插件,只是您的普通網站。

我有一個頁面,我需要讓用戶選擇一些“相關產品”。

現在,我正在使用ACF,並考慮使用Post_Object允許用戶選擇產品。

這需要做的是獲得產品名稱,還獲得該產品的圖像和描述。

我使用了ACF網站中的這段代碼來嘗試獲取發布對象的標題。

    <?php

$post_object = get_field('post_object');

if( $post_object ): 

    // override $post
    $post = $post_object;
    setup_postdata( $post ); 

    ?>
    <div>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
    </div>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

但是,即使這樣也不會顯示任何內容?

任何人都可以看到明顯的問題嗎?

由於允許多個選擇,因此get_field將返回一個發布對象數組。 因此,您將需要使用以下代碼遍歷該數組。 此代碼假定您的Post Object字段名為“ related_products”,然后調用該帖子的摘錄值,該值將首先檢查Excerpt字段,如果不存在,則將根據該帖子的內容生成摘錄。

    $related_products = get_field('related_products');

    if( $related_products ): ?>
            <ul>
            <?php foreach( $related_products as $post): // variable must be called $post (IMPORTANT) ?>
                    <?php setup_postdata($post); ?>
                    <li>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            <?php the_excerpt(); ?>
                    </li>
            <?php endforeach; ?>
            </ul>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM