最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Help understand and create a loop with WP_query

programmeradmin1浏览0评论

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
  • first your post type is capitalized. Are you sure that's how it was created? Secondly, try 'field' => 'slug' instead of 'field' => 'kategori' – rudtek Commented Jan 29, 2020 at 17:12
Add a comment  | 

1 Answer 1

Reset to default 0

Maybe 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();
?> 
发布评论

评论列表(0)

  1. 暂无评论