ACF高级自定义字段|帮助我获取WooCommerce主页上的字段

时间:2020-02-04 作者:Jason Is My Name

我一辈子都搞不明白为什么这些领域没有成功(我已经尝试了30多种方法来实现这一目标)。

这与我以前使用这个出色(但令人沮丧)插件的时候完全一样。

这是我的代码:

<?php
    $has_homepage_side_points = get_field(\'has_homepage_side_points\');
    $homepage_side_points = get_field(\'homepage_side_points\');
?>

<?php if( $has_homepage_side_points === TRUE ) : ?>

    <div class="wooside-column">

        <?php
        while( have_rows(\'homepage_side_points\') ) : the_row();
            $has_how_to_use = get_sub_field(\'has_how_to_use\');
            $how_to_use = get_sub_field(\'how_to_use\');
            $has_featured_material = get_sub_field(\'has_featured_material\');
            $featured_material = get_sub_field(\'featured_material\');
            $has_featured_designer = get_sub_field(\'has_featured_designer\');
            $featured_designer = get_sub_field(\'featured_designer\');
        ?>

            <?php if( $has_how_to_use === TRUE ) : ?>

                <?php
                while( have_rows($how_to_use) ) : the_row();
                $title = get_sub_field(\'title\');
                $points = get_sub_field(\'points\');
                ?>

                    <div class="wooside-block how-to-use">
                        <?php if ($title != \'\') : ?>
                            <h3><?php echo $title; ?></h3>
                        <?php endif; ?>

                        <?php
                        while( have_rows(\'points\') ) : the_row();
                        $icon = get_sub_field(\'icon\');
                        $text = get_sub_field(\'text\');
                        ?>
                            <div class="wb-point">
                                <img src="<?php echo $icon; ?>" class="wb-icon">
                                <p class="wb-point-text"><?php echo $points; ?></p>
                            </div>
                        <?php endwhile; ?>
                    </div>

                <?php endwhile; ?>

            <?php endif; ?>

            <?php if( $has_featured_material === TRUE ): ?>

                <?php while( have_rows($featured_material) ) : the_row();
                $title = get_sub_field(\'title\');
                $button_text = get_sub_field(\'button_text\');
                ?>

                    <div class="wooside-block featured-product">

                        <?php 
                        $args = array (
                            \'post_type\'         => \'product\',
                            \'post_status\'       => \'publish\',
                            \'posts_per_page\'    => \'1\',
                            \'product_tag\'       => \'featured\'
                        );

                        $the_query = new WP_Query( $args ); ?>

                        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

                            <?php $product_thumbnail = get_the_post_thumbnail($the_query->post->ID, \'full\'); ?>

                            <div class="wooblock-inner"<?php if ($product_thumbnail != \'\') : ?> style="background-image: url(<?php echo $product_thumbnail; ?>);"<?php endif; ?>>
                                <a href="<?php the_permalink(); ?>" class="box-link"></a>
                                <?php if ($title != \'\') : ?>
                                    <h3 class="featured-product-title">
                                        <?php echo $title; ?>
                                    </h3>
                                <?php endif; ?>

                                <div class="featured-product-name"><?php get_the_title(); ?></div>

                                <?php if ($button_text != \'\') : ?>
                                    <div class="wooside-button">
                                        <?php echo $button_text; ?>
                                    </div>
                                <?php endif; ?>
                            </div>

                        <?php
                            endwhile;
                            wp_reset_postdata();
                        ?>
                    </div>

                <?php endwhile;?>

            <?php endif; ?>

            <?php if( $has_featured_designer === TRUE ): ?>

                <?php
                while( have_rows($featured_designer) ) : the_row();
                $title = get_sub_field(\'title\');
                $button_text = get_sub_field(\'button_text\');
                ?>

                    <div class="wooside-block wooside-featured-designer">

                        <?php 
                        $args = array (
                            \'category_name\'     => \'case-study\',
                            \'tag\'               => \'featured\',
                            \'posts_per_page\'    => \'1\'
                        );

                        $the_query = new WP_Query( $args ); ?>

                        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

                            <?php $designer_thumbnail = get_the_post_thumbnail($the_query->post->ID, \'full\'); ?>

                            <div class="wooblock-inner"<?php if ($designer_thumbnail != \'\') : ?> style="background-image: url(<?php echo $designer_thumbnail; ?>);"<?php endif; ?>>
                                <a href="<?php the_permalink(); ?>" class="box-link"></a>
                                <?php if ($title != \'\') : ?>
                                    <h3 class="featured-designer-title">
                                        <?php echo $title; ?>
                                    </h3>
                                <?php endif; ?>

                                <!--
                                <?php
                                $case_study = get_field(\'case_study_page_content\');
                                $is_case_study = get_field(\'is_case_study\'); ?>
                                <?php if( $is_case_study ): ?>
                                    <div class="is-case-study">
                                        <img src="/wp-content/themes/bolt/assets/img/case-study.svg" alt="Case Study">
                                    </div>
                                <?php endif; ?>
                                -->

                                <div class="featured-designer-name"><?php get_the_title(); ?></div>

                                <?php if ($button_text != \'\') : ?>
                                    <div class="wooside-button">
                                        <?php echo $button_text; ?>
                                    </div>
                                <?php endif; ?>
                            </div>

                        <?php
                            endwhile;
                            wp_reset_postdata();
                        ?>
                    </div>

                <?php endwhile;?>

            <?php endif; ?>

        <?php endwhile; ?>

    </div>

<?php endif; ?>
以下是我所有字段的屏幕:

enter image description here

如果有人能帮我找出哪里出了问题,我将不胜感激。

EDIT (解决方案):

因此,您不能使用$post->ID或类似的标准调用,必须使用get\\u选项(“woocommerce\\u shop\\u page\\u ID”)。然后,您必须在对字段的调用中使用此选项,例如$has\\u homepage\\u side\\u points=get\\u字段(\'has\\u homepage\\u side\\u points\',get\\u选项(\'woocommerce\\u shop\\u page\\u id\');

谢谢,杰森。

1 个回复
SO网友:Jason Is My Name

如果使用WooCommerce主页,则必须使用get\\u选项在get\\u字段中指定:

$has_homepage_side_points = get_field(\'has_homepage_side_points\', get_option( \'woocommerce_shop_page_id\' ));
这为我解决了这个问题,我希望它能帮助其他人,杰森。

相关推荐

Homepage Not Found Error

好的,所以出于某种原因,我的主页总是显示为“找不到页面”。我搜索了无数讨论同一问题的其他帖子,但似乎没有一个答案对我有帮助。我对网站还不熟悉,所以我不知道该怎么办。当我开始我的网站时,总是会出现“页面未找到”错误,但我认为这是因为我删除了示例帖子,没有其他帖子。当我开始发帖时,事情并没有改变,我将设置更改为“静态页面”,这很有效。。。几个小时。那天晚些时候我重新登录时,错误再次出现。我曾尝试将我的主页从“您的最新帖子”(页面出现“未找到页面”错误,即使我有两篇帖子)更改为“静态页面”(当我创建一个页面并将