7

I have been researching all over the net and forums regarding my question but I can't seem to produce the correct results. Basically I'm trying to display the terms or product attributes for only a specific product category.

Here is the code I have been working on.

<fieldset class="liquor-types-control filter-controls" >

    <?php 

    $terms = get_terms( 'wine', /*<--Product Category */ 'pa_liquor-types' /* <--Product Attribute */);
    foreach ( $terms as $term ) :
    ?>

    <label> 
        <input type="checkbox" value="<?php echo "." . $term->slug ?>"  /> 
        <?php echo $term->name ?>
    </label>

    <?php endforeach; ?>


</fieldset>


(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid taxonomy.
                )

        )

    [error_data] => Array
        (
        )
)
clestcruz
  • 1,081
  • 3
  • 31
  • 75

4 Answers4

5

The var_dump is showing that you are using taxonomies on WordPress. While I don't have experience directly with Wordpress, the Wordpress site docs say:

Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies:

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

From the function reference:

$term = get_term( $term_id, $taxonomy );

Gives you term slug: e.g.: term-slug-example

$slug = $term->slug;

Gives you term name: e.g. Term Name Example

$name = $term->name; 

First, make sure you are using the correct version - you are using the syntax from prior 4.5.0

Second, the error is saying that the taxonomy pa_liquor-types is invalid. You need to check where this is being defined.

Check your create_initial_taxonomies() function syntax and post it if necessary.

mseifert
  • 5,390
  • 9
  • 38
  • 100
3

try something like this:

<?php
$terms = get_terms( 'wine', 'pa_liquor-types');

foreach($terms as $term) { ?>
    <input type="checkbox" value="<?php echo "." . $term['slug'];?>"/>
    <?php echo $term['name'];?>
<?php } ?>
Grey
  • 877
  • 9
  • 29
2

Use ; after $term->slug and $term->name.

 <label> 
        <input type="checkbox" value="<?php echo $term->slug; ?>"  /> 
        <?php echo $term->name; ?>
    </label>
Jomol MJ
  • 671
  • 1
  • 6
  • 23
0

Get terms of wine and liquor-types seperately, merge them to single array $terms and loop through that array to get all terms. Hope it helps, haven't tested the code yet.

<fieldset class="liquor-types-control filter-controls" >

    <?php 
    global $post;
    $terms_wine = get_the_terms(get_the_ID(),'wine');
    $terms_liquor = get_the_terms(get_the_ID,'pa_liquor-types');
    $terms = array();
    foreach($terms_wine as $wine){
        array_push($terms,$wind);
        }
    foreach($terms_liquor as $liquor){
        array_push($terms,$liquor);
        }


    foreach ( $terms as $term ) :
    ?>

    <label> 
        <input type="checkbox" value="<?php echo "." . $term->slug ?>"  /> 
        <?php echo $term->name ?>
    </label>

    <?php endforeach; ?>


</fieldset>
Yamu
  • 1,652
  • 10
  • 15
  • Not sure if you have the $post or not, if not the get the id of current product whose terms you want to list and replace $post by the id of product. – Yamu Nov 24 '16 at 08:16