Group Product Types

时间:2021-11-26 作者:Ryan Hammond

我正在使用高级自定义字段,并试图将自定义帖子类型与下拉字段中的类似值组合在一起。例如。其他的将是它自己的标题和投影仪将有一个标题和两个项目。

下面是我如何设置ACF的enter image description here

这是我的输出enter image description here

这是迄今为止我掌握的代码。

<?php

/*
Template Name: Products By Brand
Template Post Type: Brand
*/

get_header();


//new 

// args - get all the brands, filtered by audience
$args = array(
    \'numberposts\'   => -1,
  \'post_type\'   => array(\'brand\', \'products\'),
);
$loop = new WP_Query( $args );
$counter=0;
$total_posts = $loop->post_count;
$column_divide = 4;


// query
$the_query = new WP_Query( $args );

?>

<div class="fl-content-full container">
    <div class="row">
        <div class="fl-content col-md-12">
        <div class="fl-row fl-row-full-width fl-row-bg-none">
        <div class="fl-row-content-wrap">
        <div class="fl-row-content fl-row-fixed-width">


<div class="row">
    <div class="col-lg-12">
        <?php echo get_the_post_thumbnail( get_the_ID(), \'medium\' ); ?>
        <p>product summary <?php the_field(\'product_summary\'); ?></p>
        <p>manufacture website <?php the_field(\'website_url_en\'); ?></p>
    </div>
</div>


<div class="row">
    <?php
    if( $the_query->have_posts()):
    while ( $the_query->have_posts() ) : $the_query->the_post(); $counter++;

    $brand_association = get_field(\'brand_association\');
    $product_type = get_field(\'product_type\');
    if( $brand_association ):
    ?>

    <div class="col-lg-12">
        <h2><?php the_field(\'product_type\') ?></h2>
    </div>
    <div class="col-lg-3">
        <p><img src="<?php the_field(\'product_image\') ?>"/></p>
        <p><?php the_field(\'product_details\') ?></p>
        <p><a href="<?php the_field(\'hyperlink_to_product_page\') ?>">Product Web Page</a></p>
        <p><a href="<?php the_field(\'hyperlink_to_specifications\') ?>">Product Sepcifications</a></p>   
    <?php endif; ?>
    </div>
    
<?php
  if($counter % $column_divide == 0) echo \'<div class="row">\';
    endwhile;
    endif;
    wp_reset_postdata();
?>

</div><!-- END .col-lg-12-->

            </div><!-- END .row -->
        </div>
        </div><!-- END .fl-content .col-lg-12-->
    </div><!-- END .row -->
</div><!-- END .fl-content-full .container -->


        <h1>hero banner - using beaver themer</h1>


<?php get_footer(); ?>

1 个回复
SO网友:Ryan Hammond

这是其他任何可能遇到这一问题的人的答案。

<?php

/*
Template Name: Products By Brand
Template Post Type: Brand
*/

get_header();



// get all of the custom taxonomy terms
$taxonomy = \'product_type\';
$arg_terms = array(
    \'orderby\' => \'id\',
    \'order\' => \'ASC\',
);
$taxonomy_terms = get_terms($taxonomy, $arg_terms);
$counter=0;
$column_divide = 4;


?>

    <div class="fl-content-full container">
    <div class="row">
        <div class="fl-content col-md-12">
        <div class="fl-row fl-row-full-width fl-row-bg-none">
        <div class="fl-row-content-wrap">
        <div class="fl-row-content fl-row-fixed-width">


<div class="row">
    <div class="col-lg-12">
        <?php echo get_the_post_thumbnail( get_the_ID(), \'medium\' ); ?>
        <p>product summary <?php the_field(\'product_summary\'); ?></p>
        <p>manufacture website <?php the_field(\'website_url_en\'); ?></p>
    </div>
</div>


<div class="row">

<?php

// if there are some taxonomy terms, loop through each one and get the posts in that term
if($taxonomy_terms) {
    foreach($taxonomy_terms as $taxonomy_term) {

        $args = array(
            \'post_type\' => array(\'brand\',\'products\'),
            "$taxonomy" => $taxonomy_term->slug,
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
        );

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


<?php       
    if ( $query->have_posts() ) : ?>


            <div class="col-lg-12">
        <h2><?php echo $taxonomy_term->name; ?></h2>
    </div>


            <?php while ( $query->have_posts() ) : $query->the_post(); $counter++;

            $brand_association = get_field(\'brand_association\');
        if( $brand_association ):

        ?>

    <div class="col-lg-3">
        <p><img src="<?php the_field(\'product_image\') ?>"/></p>
        <p><?php the_field(\'product_details\') ?></p>
        <p><a href="<?php the_field(\'hyperlink_to_product_page\') ?>">Product Web Page</a></p>
        <p><a href="<?php the_field(\'hyperlink_to_specifications\') ?>">Product Sepcifications</a></p>   
    </div>
            <?php endif; endwhile;

  if($counter % $column_divide == 0) echo \'<div class="row">\';
  ?>

        <?php wp_reset_postdata(); // so nothin\' weird happens to other loops
        endif;

    }
}
?>

</div><!-- END .col-lg-12-->

            </div><!-- END .row -->
        </div>
        </div><!-- END .fl-content .col-lg-12-->
    </div><!-- END .row -->
</div><!-- END .fl-content-full .container -->
</div>


        <h1>hero banner - using beaver themer</h1>
<?php get_footer(); ?>