如何在WordPress中创建可过滤的文件夹?

时间:2017-10-29 作者:Kirasiris

我正在WordPress中创建一个公文包自定义页面,到目前为止,我已经能够从自定义分类中获得类别。

现在,我想要的是,当单击一个选项时,它应该获取带有自定义类别的帖子。

这就是我目前的情况:

    <div class="row">
    <div class="col-lg-12">
        <div class="pull-right">
            <button class="btn btn-small btn-default" data-toggle="portfilter" data-target="all">All</button>
                <?php 
                    $terms = get_terms("portfolio_categories"); // Consigue todas las categorias del custom taxonomy.
                    $termsString .=  $term->slug;
                    $count = count($terms); //Cuantos categorias son?
                    if ( $count > 0 ){  //Si es que hay mas de uno
                        foreach ( $terms as $term ) {  //Para cada termino:
                            echo "<button class=\'btn btn-small btn-primary\' data-toggle=\'portfilter\' data-target=\'".$term->slug."\'>".$term->name."</button>\\n";
                        }
                    } 
                ?>
        </div>
    </div>
</div>
<br/>
<div class="row">
        <?php $portfolio_query = new WP_Query(array(
          \'post_type\' => \'portfolios\',
          \'order\' => \'DESC\',
          ));
        $terms_portfolio = get_the_terms( get_the_ID(), \'portfolio_categories\');
        ?>     
        <?php if($portfolio_query->have_posts()) : while($portfolio_query->have_posts()) : $portfolio_query->the_post();  ?>
        <div class="col-md-4" data-tag="<?php echo $terms_portfolio[0]->slug; ?>">
            <div class="thumbnail">
            <?php if(has_post_thumbnail()) : ?>
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(\'portfolio-page\'); ?></a>
            <?php endif; ?>
                <div class="caption">
                    <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                </div>
            </div>
        </div>
        <?php endwhile; ?>
        <?php  else : ?>
        <div class="alert alert-danger text-center"><p>Ningun portfolio encontrado</p></div>
        <?php endif; ?>
</div>
有人能查一下我的密码吗?我无法在中显示类别data-tag 外径div col-md-4

这是当前状态:

enter image description here

源代码中的输出如下:

<div class="col-md-4" data-tag=""></div>

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

您正在设置$terms_portfolio 太早了。您正在使用get_the_terms( get_the_ID(), \'portfolio_categories\'), 但是get_the_ID() 不会是循环中的当前投资组合,因为$portfolio_query->the_post() 尚未运行。

更改此位:

<?php $portfolio_query = new WP_Query(array(
          \'post_type\' => \'portfolios\',
          \'order\' => \'DESC\',
          ));
        $terms_portfolio = get_the_terms( get_the_ID(), \'portfolio_categories\');
        ?>     
        <?php if($portfolio_query->have_posts()) : while($portfolio_query->have_posts()) : $portfolio_query->the_post();  ?>
        <div class="col-md-4" data-tag="<?php echo $terms_portfolio[0]->slug; ?>">
收件人:

<?php 
$portfolio_query = new WP_Query(array(
    \'post_type\' => \'portfolios\',
    \'order\' => \'DESC\',
));

if($portfolio_query->have_posts()) : 
    while($portfolio_query->have_posts()) : $portfolio_query->the_post();  
        $terms_portfolio = get_the_terms( get_the_ID(), \'portfolio_categories\'); 
        ?>

        <div class="col-md-4" data-tag="<?php echo $terms_portfolio[0]->slug; ?>">

结束