简体   繁体   中英

Display custom taxonomy in a loop Wordpress

Ok, this is probably an easy one. But I can't seem to figure it out for some reason.

I have a custom post type called: Beachevents. There i have a couple of events. I also have a custom taxonomy called: Thema.

When making my beachevent pages (not posts) i created some types of thema's (themes). Like: Strand Spellen (the slug is strand-spellen).

Now I want to make a loop that display's only strand-spellen with thumbnail and all that stuff.

Does anyone know how I go about this?

I tryed some codes like these but do don't do the trick.

$args = array(
            'post_type' => 'beachevents',
            'posts_per_page'=> -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'strand-spellen',
                    'field' => 'slug',
                    'terms' => 'all'
                )
            )
        );
        $products = new WP_Query( $args );
        if( $products->have_posts() ) {
            while( $products->have_posts() ) {
                $products->the_post();
                ?>

                    <div class='content'>
                        <h2><?php the_title(); ?></h2>
                    </div>
                <?php
            }
        }
        else {
            echo 'There seems to be a problem, please try searching again or contact customer support!';
        }

Thanks!

You're close!

In your tax_query, taxonomy needs to refer to 'beachevents' and terms needs to refer to 'strand-spellen'.

So, your code will look like this:

    'tax_query' => array(
            array(
                'taxonomy' => 'thema',
                'field' => 'slug',
                'terms' => 'strand-spellen'
            )
        )

For more information on building your queries, you may find the WP_Query documentation useful - there's a section in there on taxonomy queries.

Thanks to Tim for his help. Here is my full code for people who encounter this same problem.

<?php $args = array(
    'post_type' => 'beachevents',
    'posts_per_page'=> -1,
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => array(
        array(
        'taxonomy' => 'thema',
        'field' => 'slug',
        'terms' => 'strand-spellen'
                )
            )
        );

        $products = new WP_Query( $args );
            if( $products->have_posts() ) {
                while( $products->have_posts() ) {
                    $products->the_post();
?>

<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
    }
        }
            else {
                echo 'There seems to be a problem, please try searching again or contact customer support!';
            } ?>

Including ordered by title and ASC. Hope I coded it correctly...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM