0

I want te make a filter based on post-categories. I'm trying to add the category-names in a data-category-attribute, but It doesn't seem to work. This is what I tried:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> data-category="<?php $category_detail=get_the_category(); foreach($category_detail as $cd){ $cd->cat_name;}?>">
    <header class="entry-header">
        <?php
        if ( is_singular() ) {
            the_title( '<h1 class="entry-title">', '</h1>' );
        }
        else {
            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
        }

        if ( 'post' === get_post_type() ) {
            ?>
            <div class="entry-meta">
                <?php dfib_theme_entry_meta(); ?>
            </div>
        <?php } ?>
    </header>
    <?php dfib_theme_post_thumbnail(); ?>
        <div class="entry-content">             
            <?php 
                the_excerpt();

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'dfib-theme' ),
                'after'  => '</div>',
            ) );
            ?>
        </div>
    <footer class="entry-footer">
        <a href="<?php the_permalink(); ?>" class="btn">Meer info</a>
    </footer>
</article>

What am I doing wrong here?

1

You'll need to echo back your results in PHP.

foreach($category_detail as $cd){ echo $cd->cat_name; }

Also you might want to consider putting something between each category. If you have categories/terms like Blue, Red, Green. Then how you are currently looping over them, it will print it back as:

data-category="BlueRedGreen"

Hope that helps!!

|improve this answer|||||

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.