我对多个页面使用相同的页面模板。-> page-base.php
在使用此模板的每个页面上,我想显示使用CPT
.
我的问题是,如何使数组动态化,以自动更改“分类法”?
这是我当前的代码
<?php
$terms = get_terms(
array(
\'taxonomy\' => \'catmaison\',
\'hide_empty\' => false,
));
if ( ! empty( $terms ) && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$the_query = new WP_Query( array(
"posts_per_page" => 1,
"orderby" => \'date\',
"order" => \'DESC\',
"tax_query" => array(
array (
\'taxonomy\' => \'catmaison\',
\'field\' => \'term_id\',
\'terms\' => $term->term_id,
),
),
) );
?>
UPDATE
$taxonomy = get_field(\'taxonomy\');
$terms = get_terms(
array(
\'taxonomy\' => $taxonomy,
\'hide_empty\' => false,
));
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
foreach ( $terms as $term ) {
$the_query = new WP_Query( array(
"posts_per_page" => 1,
"orderby" => \'date\', // this is the default
"order" => \'DESC\', // this is the default
"tax_query" => array(
array (
\'taxonomy\' => $taxonomy, // use the $tax you define at the top of your script
\'field\' => \'term_id\',
\'terms\' => $term->term_id, // use the current term in your foreach loop
),
),
) ); ?>
<!--LOOP CONTENT -->
<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12 pb-20 cpt-categories">
<div class="cat-images">
<!-- Image -->
<?php
$image = get_field(\'image_category\', $term);
if( !empty($image) ): ?>
<img src="<?php echo $image[\'url\']; ?>" class="img-fluid pb-20" alt="<?php echo $image[\'alt\']; ?>" />
<?php endif; ?>
<!-- Image -->
</div>
<?php $pageone = get_the_permalink($the_query->posts[0]); ?>
<h4><a href="<?php echo $pageone; ?>"><?= $term->name ?></a></h4>
<?php echo $cat; ?>
</div>
<!--LOOP CONTENT -->
<?php
} // End foreach
} // End if
//endif; ?>
所以我已经按照你的要求更新了代码。我正在使用ACF Pro我的网站。但我不理解你的提议。
这是我想你告诉我的,对吗?