简体   繁体   English

相关产品ACF WordPress

[英]Related Products ACF WordPress

I am not using a WooCommerce plugin, just your normal site. 我没有使用WooCommerce插件,只是您的普通网站。

I have a page, which I need to let the user select some 'related products'. 我有一个页面,我需要让用户选择一些“相关产品”。

Now, I am using ACF, and looking at using the Post_Object to allow the user to select the product. 现在,我正在使用ACF,并考虑使用Post_Object允许用户选择产品。

What this needs to do, is get the product name, but also get that products image and description. 这需要做的是获得产品名称,还获得该产品的图像和描述。

I have used this code, from the ACF site, to attempt to grab the post object title. 我使用了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; ?>

But even this won't display anything? 但是,即使这样也不会显示任何内容?

Is there any obvious issues anyone can see? 任何人都可以看到明显的问题吗?

Since you are allowing multiple selections, the get_field will return an array of post objects. 由于允许多个选择,因此get_field将返回一个发布对象数组。 So you will need to loop through that array, using the following code. 因此,您将需要使用以下代码遍历该数组。 This code assumes that your Post Object field is named 'related_products', and then calls the post's excerpt value, which will check the Excerpt Field first, and if not present, will generate an excerpt from The Content of the post. 此代码假定您的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