I'm using the same page template for multiple pages. -> page-base.php
On each page using this template, I want to display categories, created with a CPT
.
My question is, how can I make my array dynamic, to change the 'taxonomy' automatically?
This is my current code
<?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; ?>
So i've updated my code like you tell me to do.
I'm using ACF Pro the my website. But i don't understand your proposal.
This is that i think you tell me to di, that's right ?
I'm using the same page template for multiple pages. -> page-base.php
On each page using this template, I want to display categories, created with a CPT
.
My question is, how can I make my array dynamic, to change the 'taxonomy' automatically?
This is my current code
<?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; ?>
So i've updated my code like you tell me to do.
I'm using ACF Pro the my website. But i don't understand your proposal.
This is that i think you tell me to di, that's right ?
Share Improve this question edited Mar 14, 2019 at 14:50 WDCreativ asked Mar 11, 2019 at 9:20 WDCreativWDCreativ 32 silver badges6 bronze badges 6- Have you checked my answer? Did it work? Please write an answer if you used another solution. – Sally CJ Commented Mar 13, 2019 at 0:45
- Sorry for late. I've tried your answer but it didn't work.. so i dont know how to do – WDCreativ Commented Mar 14, 2019 at 8:22
- Hi, please check the updated answer. – Sally CJ Commented Mar 14, 2019 at 10:29
- Thanks for your answer. please check my uptade above – WDCreativ Commented Mar 14, 2019 at 14:50
- 1 Ohh, i've found the solution. I've just re-read and re-read your solution to understand. Thanks a lot for your answer, you saved my life ! – WDCreativ Commented Mar 14, 2019 at 15:04
1 Answer
Reset to default 0You can use custom field.
For example, you could name it taxonomy
, and then in your template, use get_post_meta()
to retrieve the taxonomy:
$post_id = get_the_ID(); // the ID of the current post
$taxonomy = get_post_meta( $post_id, 'taxonomy', true );
And change the 'taxonomy' => 'catmaison'
in your code to:
'taxonomy' => $taxonomy
Example:
$terms = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
),
);
UPDATE
I normally don't suggest any plugins in my answer, but in your case, it might be easier for you to just use a plugin like ACF.
So go ahead, install and activate it, and follow the documentation on creating a custom field. But as an example, let's use the same name as I've suggested before: taxonomy
. It should be a simple Text field:
(Update: For other readers, the text field looks like below.)
Now in your page template, you can use this code to retrieve the taxonomy (slug) associated with the specific page:
$taxonomy = get_field( 'taxonomy' );
And then as I've mentioned before, change the 'taxonomy' => 'catmaison'
in your code to:
'taxonomy' => $taxonomy