0

Function

function get_taxonomy_names($post_id, $term_name) {
$collections = wp_get_post_terms($post_id, $term_name, array("fields" => "all"));
$count       = count($collections);
$return = '';
if($count > 0) {
    $i = 1;
    foreach($collections as $collection) {
        $return .= $collection->name;
        if($count > 1) {
            if($count == $i) {
                continue;
            }
            $return .= ', ';
        }
        $i++;
    }
}
return $return;

}

Product template

< $products_category = get_categories(array('taxonomy'=>'category-products'));?>


    <div class="sorting-block">
        <div class="content-xs">
            <ul class="sorting-nav sorting-nav-v1 text-center">
            <? foreach ( $products_category as $collection ): ?>
                <li class="filter" data-filter="category"><?=$collection->name;?></li>
            <?endforeach;?>    
            </ul>
        </div>


    <? $products = get_posts( array('post_type' => 'products', 'posts_per_page' => 400,) ); ?>

    <? foreach ( $products as $post ) : setup_postdata( $post ); ?> 
    <? $pic               = types_render_field("product-pic", array("url"=>"true")); ?>
    <? $serial_number     = types_render_field("serial-number", array("url"=>"true")); ?> 

        <ul class="row sorting-grid">
            <li class="col-md-3 col-sm-6 col-xs-12 mix" data-cat="category">
                <a href="#">
                    <img class="img-responsive" src="<?=$pic;?>" alt="">
                    <span class="sorting-cover">
                        <span><? the_title(); ?></span>
                        <p></p>
                    </span>
                </a>
            </li>     
        </ul>
    <?endforeach;?>

Problem

I have product category which I did by types plugin 'custom taxonomies'. I want to show products by category when somebody clicks the category name that should show just that products which registered to for example "acril" category. Problem is when I am debugging products it is not showing category. So I should connect both foreach circle if you paid attention I have data-filter in circle 1 and data-cat in the second circle with value 'category'. that should be dynamic and what can be key for that connection if post not showing the any keys.

  • Please stop using those crappy short php tags. You should never use them, they are not recommended in WordPress and just poor coding habits IMHO – Pieter Goosen Dec 28 '15 at 5:11
  • I know when I am rushing do that or that is just bad habit!!! – Ravshan Abdurasulov Dec 28 '15 at 5:14
0

You need to pass the category name as well if you want to show posts from specific category..

You just need to add one more param to the get_posts array.

REPLACE THIS

<? $products = get_posts( array('post_type' => 'products', 'posts_per_page' => 400,) ); ?>

WIth THIS

<? $products = get_posts( array('post_type' => 'products',  'category_name' => 'category_name_here', 'posts_per_page' => 400,) ); ?>

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.