【问题标题】:How to create filterable portfolio in WordPress?如何在 WordPress 中创建可过滤的投资组合?
【发布时间】:2018-04-10 10:08:42
【问题描述】:

我正在 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',));
        ?>     
<?php 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; ?>">
            <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>

这些类别工作正常,但我仍然无法获得&lt;div class="col-md-4" data-tag="this is what I need"&gt; 内的data-tag 的信息

有人可以帮我解决这个问题吗?

这是当前状态:

【问题讨论】:

  • get_terms 返回一个数组,所以即使你实际上对返回值做了一些事情(没有echo 那里),你最终会得到类似data-tag="Array" 的东西......所以实际问题变成了,你首先需要什么具体价值?
  • 如果你说你需要那里的 slug,那为什么不只是 data-tag="&lt;?php echo $term-&gt;slug; ?&gt;" (当然还有一个适当的转义函数,取决于该值可能包含的内容。)
  • 先生,我已经试过了,但我发现自己做不到。(这是我第一次尝试)

标签: javascript php wordpress custom-post-type custom-taxonomy


【解决方案1】:

使用这个:

$terms_portfolio = get_the_terms( get_the_ID(), 'portfolio_categories');

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

【讨论】:

  • 我试过了,但没有用,然后我用$term 替换了$terms[0],它实际上“有点”有效,但只适用于最后一个类别。
  • &lt;div class="col-md-4" data-tag="portfolio-category-5"&gt;&lt;/div&gt;
  • 您能否通过添加 $terms, $terms = get_the_terms(get_the_ID(), 'portfolio_categories'); 的输出来检查一下print_r($terms);
  • 这应该工作我不知道出了什么问题通过添加这个 print_r(get_the_terms(get_the_ID(), 'portfolio_categories')); 调试输出;
  • 我已经更新了答案你是否也相应地更新了你的答案
猜你喜欢
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2015-05-19
  • 2017-05-15
  • 2018-01-23
相关资源
最近更新 更多