$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Make taxonomy query dynamic|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Make taxonomy query dynamic

matteradmin10PV0评论

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
 |  Show 1 more comment

1 Answer 1

Reset to default 0

You 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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far