我目前将category.php模板设置为循环显示当前类别,然后显示与该类别匹配的帖子 .

该类别中的帖子都使用来自单独的自定义帖子类型的信息 .

使用案例:

按气味显示产品列表 . 每个产品都有一个“容器”的自定义字段,它通过来自ACF的邮政对象与自定义邮政类型的“容器”相关 .

当我创建产品时,选择一个“容器”,我希望页面从该容器中提取信息,包括价格 .

因此,最终结果将是“Lavender”的类别页面列出我们的帖子:

  • 薰衣草 jar 蜡烛20美元

  • 薰衣草锡蜡烛$ 15

  • 薰衣草沐浴露10美元

“薰衣草”是当前的类别,“ jar 蜡烛”“锡蜡烛”“洗澡”是自定义帖子类型“容器”的条目,“20美元”“15美元”“10美元”是从自定义帖子中提取的价格键入与该特定容器关联的容器 .

理想情况下,我想说“嘿,从帖子中拉出 Headers ,从与之相关的CPT中扣除价格 . ”

我想要实现的是该类别中的每个帖子,显示 Headers ,永久链接和价格 . 价格是自定义帖子类型的一部分 .

以下是我正在使用的内容:

<ul class="list-items categories">
    <?php
        // Query Current Category ID
        $cat = get_queried_object();
        $cat_id = $cat->cat_ID;

        // Start new WP Query for current Category
        $custom_query = new WP_Query( 
            array( 
                'post_type' => array('post', 'products'),
                'cat' => $cat_id
            )
        );

        if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

        <li>
            <a href="<?php the_permalink();"><?php the_post_thumbnail('thumbnail'); ?></a>
            <a href="<?php the_permalink(); ?>" title="<?php the_title();?>: $<?php the_field('base_price'); ?>+">
                <p><?php the_title();?></p>
                <?php
                    $post_object = get_field('container');
                    if( $post_object ): 
                        // override $post
                        $post = $post_object;
                        setup_postdata( $post ); 
                ?>
                <p class="price">$<?php the_field('base_price'); ?>+</p>
                <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
                <?php endif; ?>
            </a>
        </li>
    <?php endwhile; wp_reset_query();?>
</ul>

<?php else : ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

以上工程在这里实现了价格:

<p class="price">$<?php the_field('base_price'); ?>+</p>

但显然我需要它也将价格也放在 a href title 中 . 这样做时,它没有正确拉 the_title .

我想基本上拿这个块:

<?php
    $post_object = get_field('container');
    if( $post_object ): 
        // override $post
        $post = $post_object;
        setup_postdata( $post ); 
?>

并将其变为变量,以便我可以选择使用的内容:

$base_price = the_field('base_price');

来自该类别的帖子:

  • the_permalink

  • the_post_thumbnail

  • the_title

从自定义帖子类型:

  • base_price

  • (以及其他人)

这可能吗?实现这一目标的最佳方法是什么,以便我可以选择哪些内容在哪里?或者有更好的方法来实现这一目标吗?