Support

Account

Home Forums General Issues Turn tags filterable

Solving

Turn tags filterable

  • I have build a template with custom fields for categories and tags that I can select. When selected it outputs those articles that fits within the categories and tags. I am using a new WP_Query loop. Everything outputs fine.

    Now I want to make a filter of those tags so when selected, it only shows articles that have that selected tag. I’m having trouble doing that. Any help on how to fix this?

    This is my current code:

    <section id="primary" class="content-area full-width" itemtype="http://schema.org/Blog" itemscope="itemscope" itemprop="mainContentOfPage" role="main">
    
    	<header class="entry-header">
    		<h2 class="entry-title" itemprop="headline"><?php the_title(); ?></h2>
    	</header><!-- .entry-header -->
    
    	<?php
    	$args = array(
    	'post_status' => get_field( 'post_status' ),
    	'category__in' => get_field( 'categories' ),
    	'tag__in' => get_field( 'tags' ),
    	'post_type' => get_field( 'post_type' ),
    	'posts_per_page' => get_field( 'posts_per_page' ),
    	'orderby' => get_field( 'orderby' ),
    	'order' => get_field( 'order' ),
    	'nopaging' => true
    	);
    	$args['category__in'] = get_field( 'categories' );
    	$args['tag__in'] = get_field( 'tags' );
    
    	$postlist = new WP_Query( $args );
    		if ( $postlist->have_posts() ) : ?>
    
    <?php the_field('tags'); ?>
    
    			<div class="row">
    				<?php while ( $postlist->have_posts() ) : $postlist->the_post(); ?>
    					<?php get_template_part( 'content', 'recept' );
    				endwhile;
    			echo '</div>';
    
    				the_posts_pagination( array(
    						'prev_text'          => __( 'Vorige Pagina', 'ikbenirisniet' ),
    						'next_text'          => __( 'Volgende Pagina', 'ikbenirisniet' ),
    						'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'ikbenirisniet' ) . ' </span>',
    					) );
    
    			else :
    				get_template_part( 'content', 'none' );
    			endif; ?>
    		</div>
    </section>

    I want to put <?php the_field('tags'); ?> into tags that are filterable.

  • Do you mean that you want this filter on the front end so that visitors can select a tag and only show posts in a specific tag?

  • Yes, that’s what I’m looking for. I managed to get it working just now but my tags shows multiple times in my filter. For example, I have 6 posts showing and each tag shows up 6 times.

    I have the full code here:

    http://pastebin.com/y5Rw2bhk

  • But you have the filtering working?

    As far as the repeating tags, do you mean this part of your code is?

    
    <ul id="filters" class="filter-menu">
            <?php while ( $postlist->have_posts() ) : $postlist->the_post(); ?>
                    <?php $posttags = get_the_tags();
                            echo '<li><a href="#" class="active" data-filter="*">Alles</a></li>';
                            if ($posttags) {
                                    foreach($posttags as $tag) {
                                            echo '<li><a href="#" data-filter="' . '.' . $tag->name . '">' . $tag->name . '</a></li>';
                                    }
                            }
                            ?>
                    <?php endwhile; ?>
            </ul>
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.