I want to create a loop based on custom post types and taxonomies. But i don't really understand the WP_query and loop.
I have a custom post type called "hanstholm" with a custom taxonomy called "kategori". In that i have "tilbud" and several others. I've tried to create a loop with this:
<?php $query = new WP_Query( array(
'post_type' => 'Hanstholm', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'kategori', // taxonomy name
'field' => 'kategori', // term_id, slug or name
'terms' => 'tilbud', // term id, term slug or term name
)
) ) );
while ( $query->have_posts() ) : $query->the_post();
?>
<section class="section">
<div class="container-wrap">
<div class="container">
<div class="row" style="padding-bottom:0">
<div id="artikel-normal">
<div id="artikel-image-left" class="col-sm-12 col-md-12 col-lg-6" style="background:linear-gradient( rgba(0, 0, 0, 0.0), rgba(0, 0, 0, 0.1)), url(<?php the_field( 'intro_billede' ); ?>); background-position: 50% 50%; background-size: cover;">
</div>
<div id="artikel-content-right" class="col-sm-12 col-md-12 col-lg-6">
<div class="hotel-name-artikel">
<?php the_field( 'intro_lille_overskrift' ); ?>
</div>
<div class="forside-artikel-overskrift">
<h2><?php the_title(); ?></h2>
</div>
<div class="forside-artikel-intro">
<?php the_field( 'intro_tekst' ); ?>
</div>
<?php $intro_button_1 = get_field( 'intro_button_1' ); ?>
<?php if ( $intro_button_1 ) { ?>
<div class="forside-artikel-link">
<div><a class="button--tertiary" style="float:left" href="<?php echo $intro_button_1; ?>"><?php the_field( 'intro_button_1_tekst' ); ?></a></div>
<?php } ?>
<?php $intro_button_2 = get_field( 'intro_button_2' ); ?>
<?php if ( $intro_button_2 ) { ?>
<div><a class="button--quaternary" style="float:left" href="<?php echo $intro_button_2; ?>"><?php the_field( 'intro_button_2_tekst' ); ?></a></div>
<?php } ?>
</div>
</div>
</div>
</div>
What am i doing wrong here? It displays nothing.
I want to create a loop based on custom post types and taxonomies. But i don't really understand the WP_query and loop.
I have a custom post type called "hanstholm" with a custom taxonomy called "kategori". In that i have "tilbud" and several others. I've tried to create a loop with this:
<?php $query = new WP_Query( array(
'post_type' => 'Hanstholm', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'kategori', // taxonomy name
'field' => 'kategori', // term_id, slug or name
'terms' => 'tilbud', // term id, term slug or term name
)
) ) );
while ( $query->have_posts() ) : $query->the_post();
?>
<section class="section">
<div class="container-wrap">
<div class="container">
<div class="row" style="padding-bottom:0">
<div id="artikel-normal">
<div id="artikel-image-left" class="col-sm-12 col-md-12 col-lg-6" style="background:linear-gradient( rgba(0, 0, 0, 0.0), rgba(0, 0, 0, 0.1)), url(<?php the_field( 'intro_billede' ); ?>); background-position: 50% 50%; background-size: cover;">
</div>
<div id="artikel-content-right" class="col-sm-12 col-md-12 col-lg-6">
<div class="hotel-name-artikel">
<?php the_field( 'intro_lille_overskrift' ); ?>
</div>
<div class="forside-artikel-overskrift">
<h2><?php the_title(); ?></h2>
</div>
<div class="forside-artikel-intro">
<?php the_field( 'intro_tekst' ); ?>
</div>
<?php $intro_button_1 = get_field( 'intro_button_1' ); ?>
<?php if ( $intro_button_1 ) { ?>
<div class="forside-artikel-link">
<div><a class="button--tertiary" style="float:left" href="<?php echo $intro_button_1; ?>"><?php the_field( 'intro_button_1_tekst' ); ?></a></div>
<?php } ?>
<?php $intro_button_2 = get_field( 'intro_button_2' ); ?>
<?php if ( $intro_button_2 ) { ?>
<div><a class="button--quaternary" style="float:left" href="<?php echo $intro_button_2; ?>"><?php the_field( 'intro_button_2_tekst' ); ?></a></div>
<?php } ?>
</div>
</div>
</div>
</div>
What am i doing wrong here? It displays nothing.
Share Improve this question edited Jan 29, 2020 at 14:02 webmaster touche asked Jan 29, 2020 at 12:47 webmaster touchewebmaster touche 32 bronze badges 1 |1 Answer
Reset to default 0Maybe the hanstholm in $args without the uppercase ?
Also a problem with tax_query
Don't forget to use var_dump($query) for see what hapening.
<?php $query = new WP_Query( array(
'post_type' => 'hanstholm', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'kategori', // taxonomy name
'field' => 'name', // term_id, slug or name
'terms' => 'tilbud', // term id, term slug or term name
)
) ) );
while ( $query->have_posts() ) : $query->the_post();
?>
'field' => 'slug'
instead of'field' => 'kategori'
– rudtek Commented Jan 29, 2020 at 17:12