首页 文章

显示自定义分类的帖子

提问于
浏览
0
  • 更新2添加名称作为字段而不是slug并添加the_title()只是给我一个页面 Headers 的回显...
$args = array(
        'post_type' => 'feestlocaties',
        'showposts' => '3',
        'orderby'   => 'rand',  
         'tax_query' => array(
                array(
                    'taxonomy' => 'locatie',
                    'field'    => 'name',
                    'terms'    => the_title(),
                ),
            ),
        );
  • 更新Jonnhyd23的代码就像一个魅力!!谢谢!有没有办法让条款动态化?就像 Headers 是阿姆斯特丹我可以做 'terms' => '<?php the_title(); ?>' 之类的东西吗?

在过去的几个小时里,我一直在这里 . 也许这里有人可以帮助我?

我想在循环中显示来自自定义分类的特定帖子 . 情况就是这样:

  • 自定义分类:feestlocaties

  • 我想要展示的帖子选择了阿姆斯特丹(已选中)(如类别) .

我试过的代码:

<div id="main-filter">

    <!-- Start the Loop. -->
    <?php $args = array(
    'post_type' => 'feestlocaties',
    'tax_query' => array(
        array(
            'taxonomy' => 'locatie',
            'field'    => 'slug',
            'terms'    => 'amsterdam',
        ),
    ),
); ?>
<?php $query = new WP_Query( $args ); ?>

<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>


        <!-- Test if the current post is in category 3. -->
        <!-- If it is, the div box is given the CSS class "post-cat-three". -->
        <!-- Otherwise, the div box is given the CSS class "post". -->
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <div class="container post-item">
                <div class="col-sm-3 no-padding">
                        <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                            <?php the_post_thumbnail(array(400,355)); // Declare pixel size you need inside the array ?>
                        <?php endif; ?>
                    </div>
                    <div class="col-sm-9 no-padding">
                <h1 class="overzicht"><?php the_title(); ?></h1>
                    <?php html5wp_excerpt('html5wp_index'); ?>

                <div class="col-sm-12 no-padding loop-overzicht">
                    <?php $prijs = get_Field('vanaf_prijs'); ?>
                    <?php $pers = get_Field('aantal_personen'); ?>
                    <?php $time = get_Field('tijdsduur'); ?>
                <ul class="loop-opsomming text-right">
                    <li><?php echo '<i class="fa fa-euro"></i>Vanaf ' . $prijs . ' p.p.' ?></li>
                    <li><?php echo '<i class="fa fa-group"></i>Vanaf ' . $pers . ' personen' ?></li>
                    <li><?php echo '<i class="fa fa-clock-o"></i>Vanaf ' . $time . ' uur' ?></li>
                </ul>
            </div>
        </div>
            </div>
        </a>
            <?php wp_pagenavi(); ?>

    <?php endwhile; endif; wp_reset_postdata(); ?>

</div>

但没有任何表现 . 任何帮助都会很棒 . 谢谢!

2 回答

  • 0

    你只是展示你用于WP_Query的论据,还是你的所有代码?尝试使用tax_query参数 .

    $args = array(
        'post_type' => 'your_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'feestlocaties',
                'field'    => 'slug',
                'terms'    => 'amsterdam',
            ),
        ),
    );
    $query = new WP_Query( $args );
    
    if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post();
    
    
    //execute code
    
    
    endwhile; endif; wp_reset_postdata();
    
  • 0

    所以,我在abit周围fiddeld,这是适合我的代码 . 干杯Johnnyd23

    <?php $args = array(
        'post_type' => 'feestlocaties',
        'showposts' => '3',
        'orderby'   => 'rand',  
        'tax_query' => array(
            array(
                'taxonomy' => 'locatie',
                'field'    => 'name',
                'terms'    => get_the_title(),
            ),
        ),
    ); ?>
    <?php $query = new WP_Query( $args ); ?>
    
    <?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
    

    这将使 Headers 在WP_Query中动态地发布 .

相关问题