0

I'm creating a Portfolio custom page in WordPress, so far I've been able to get the categories from the custom taxonomy.

Now what I want is when clicking on an option, it should fetch the post with the custom category.

This is what I have so far:

    <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>

Can somebody check my code? I'm unable to show the category in the data-tag od the div col-md-4

This the current status:

enter image description here

The output of this in the source code is the following:

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

You're setting $terms_portfolio too early. You're using get_the_terms( get_the_ID(), 'portfolio_categories'), but get_the_ID() won't be the current portfolio in the loop because $portfolio_query->the_post() hasn't run yet.

Change this bit:

<?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; ?>">

To:

<?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; ?>">
  • Oh my God, it works!!!. I've been trying to solve this the whole day lol . I just did not know how to make it happen!! :/. Thank you sir!! – Kirasiris Oct 29 '17 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.