Shahid javid Shahid javid - 7 months ago 36
PHP Question

My filterable gallery not showing exact results WordPress

I am using filterable gallery to filter my gallery items based on there category. Every thing looks great but it does not filter my results.

JS
files and
CSS
files are added and showing up as well. By inspecting my html in chrome I came to know that the structure for my filters is like
gallery-small
gallery-large
gallery-medium
etc while in my WordPress version its showing up like
gallery small
gallery large
gallery medium
. I thing the culprit is that missing dash, can any one point out what's wrong with my code ? below is mine code used to fetch categories it works fine but only dashes missing, I am unable to add those dashes.

$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => '',
'hierarchical' => true,
'child_of' => 0,
'childless' => false,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);

$terms = get_terms('gallery_category', $args);
<?php
foreach($terms as $term) { ?>
<button class="button" data-filter=".<?php $links = $term->name; ?></button>
<?php }?>


Any one to figure out that why my code not showing proper results as I wanted ??? its really urgent for me

Answer

It looks like your categories are not single word and so, splits and looks like multiple words as space added that issue. You have to add a quick fix in which you will be adding you missing - by coding. So, here is your code that will work for you.

$args = array(
            'orderby'           => 'name', 
            'order'             => 'ASC',
            'hide_empty'        => false, 
            'exclude'           => array(), 
            'exclude_tree'      => array(), 
            'include'           => array(),
            'number'            => '', 
            'fields'            => 'all', 
            'slug'              => '',
            'parent'            => '',
            'hierarchical'      => true, 
            'child_of'          => 0,
            'childless'         => false,
            'get'               => '', 
            'name__like'        => '',
            'description__like' => '',
            'pad_counts'  => false, 
            'offset'   => '', 
            'search'  => '', 
            'cache_domain'   => 'core'
            ); 

            $terms = get_terms('gallery_category', $args);
            <?php 
            foreach($terms as $term) { ?>
            <button class="button" data-filter=".<?php $links = $term->name; $links = str_replace(' ', '-', $links); echo $links; ?>"><?php echo $term->name; ?></button>
           <?php }?>

Only minor change I made is inside your <button> tag, that simply removed your spaces with a - For, further reading check String Replace in PHP Hope that solves your issue by now.