我首先要告诉我的目标,而不是我到目前为止所提出的目标。
我的目标:
示例:
Al,对。我先是自己试了一下。到目前为止,这是我的代码:
<?php
function productlist_sc($atts){?>
<?php
global $post;
$categories_array = array();
$thecategories = get_categories();
foreach( $thecategories as $category ){
$categories_array[] = $category->slug;
}
if($atts[ 'category' ]){
$atts[ 'category' ] = explode( ",", $atts[ 'category' ] );
}
//collect values, combining passed in values and defaults
$values = shortcode_atts(array(
'category' => ''
),$atts);
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'slug' => $values['category']
) );
$current = get_the_ID($post->ID);
$cargs = array(
//'child_of' => 0,
'orderby' => 'name',
'parent' => 0,
'order' => 'ASC',
'hide_empty' => 1
//'taxonomy' => 'category', //change this to any taxonomy
);
// first sort all selected posts per category
foreach ( $categories as $tax ) :
// List posts by the terms for a custom taxonomy of any post type
$args = array(
'post_type' => 'products',
'orderby' => 'ASC',
'posts_per_page'=>-1,
'category_name' => $tax->slug
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<h2>Internal ID: <?php echo $tax->name; ?></h2><?php
// second sort all selected posts per custom_field_1
$mykey_values = get_post_custom_values( 'custom_field_1' );
foreach ( $mykey_values as $key => $value ) :
?><h3><?php the_field('custom_field_1'); ?></h3>
<div class="rtt_prod_table">
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<?php if( get_field('custom_field_1') || get_field('custom_field_2') || get_field('custom_field_3') ): ?>
<ul>
<?php if( get_field('custom_field_2') ): ?>
<li>
<?php the_field('custom_field_2'); ?>
</li>
<?php endif; ?>
<?php if( get_field('custom_field_3') ): ?>
<li>
<?php the_field('custom_field_3'); ?>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile; ?> <!-- end of the loop -->
<div><!-- end .accord-content -->
</div>
<?php wp_reset_postdata();
endforeach; // custom_field_1
?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
endforeach; /* end $tax */ ?>
<?php
}
答案 0 :(得分:0)
好的,所以我找到了答案。 我会在这里发布,供所有寻求解决方案的人使用。 :)
步骤:
这是我在上面的代码中添加的代码
$stack = array();
// query
$args = array(
'post_type' => 'products',
'orderby' => 'ASC',
'posts_per_page'=>-1,
'category_name' => $tax->slug
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if( get_field('custom_field_1') ):
$stack[] = get_field('custom_field_1');
endif;
}
wp_reset_postdata();
}
$result = array_unique($stack);
sort($result);
foreach ( $result as $tax2 ) :
// run you code here
endforeach;
有一个伟大的一天男孩和女孩!