0

I currently have a Portfolio custom page with CPT and its corresponding custom taxonomy as well(categories and tags). So far its working well when talking about displaying the categories in the page, the problem is that when I add more than one category into one post, only one gets into the front end. Here is what I mean:

<?php
/*
Template Name: Portfolio
Template Post Type: page
*/
?>
<?php get_header(); ?>
<?php include("includes/page-header.php"); ?>
<div class="container">
<div class="pull-left">
                <button class="btn btn-default btn-sm" 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 cero
                            foreach ( $terms as $term ) :  //Para cada termino:
                                echo "<button class='btn btn-sm btn-primary' data-toggle='portfilter' data-target='".$term->slug."'>".$term->name."</button>\n";
                            endforeach;
                        endif 
                    ?>
            </div>
            <div class="row store">
            <?php 
            $portfolio_query = new WP_Query(array(
                'post_type' => 'portfolio',
                'order' => 'DESC',
            ));
            ?>
            <?php if($portfolio_query->have_posts()) : while($portfolio_query->have_posts()) : $portfolio_query->the_post();?>
            <?php $terms_portfolio = get_the_terms( get_the_ID(''), 'portfolio_categories'); ?>
               <div class="col-md-4  products text-center"  data-tag="<?php echo $terms_portfolio[0]->slug; ?>">
                <?php if(has_post_thumbnail()) : ?>
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('portfolio-page', array('class' => 'img-responsive')); ?></a>
                <?php endif; ?>
                    <div class="products-info">
                    <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                    <!-- Mostrar botones si valor no esta vacio -->
                    <!-- Ver Demo Button -->
                    <?php $demostracion_url = get_post_meta($post->ID, 'url_para_ver_demo', true); ?>
                    <?php if ($demostracion_url == '#') : ?>
                    <?php else: ?>
                    <a href="<?php echo demo_url(get_the_ID()); ?>" class="btn btn-default btn-sm" target="_blank" rel="noopener nofollow noreferrer"><i class="fa fa-eye" aria-hidden="true"></i></a>
                    <?php endif; ?>
                    <!-- Download Free File Button -->
                    <?php $archivo_url = get_post_meta($post->ID, 'url_para_descargar_archivo_gratis', true); ?>
                    <?php if ($archivo_url == '#') : ?>
                    <?php else : ?>
                    <a href="<?php echo descargar_url(get_the_ID()); ?>" class="btn btn-info btn-sm" target="_blank" rel="noopener nofollow noreferrer" download><i class="fa fa-download" aria-hidden="true"></i></a>
                    <?php endif; ?>
                    </div>
               </div>
            <?php endwhile; ?>
            <?php else : ?>
            <div class="alert alert-danger">No product found</div>
            <?php endif; ?>
            </div>
</div>
<!-- /.row -->
<?php get_footer(); ?>

Right here is where I get the problem:data-tag="<?php echo $terms_portfolio[0]->slug; ?>" How can I get the other categories for each post?

1

if your terms and their slugs are: red, blue, green, yellow.

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

would output only the first

<div data-tag="red"

if you do

<?php
$terms_portfolio_slugs = array();
foreach ($terms_portfolio as $tp) {
    $terms_portfolio_slugs[] = $tp->slug;
}
$terms_portfolio_csv = implode (', ',$portfoliotags);
?>

<div data-tag="<?php echo $terms_portfolio_csv?>">

your output would be all of them

<div data-tag="red, blue, green, yellow"

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.