-1

What I have is a:

  • Taxonomy: menu_type

    • Term: drink (parent term)
      • Sub term: drink 1, drink 2 (child terms)
    • Term: appetizer (parent term)
      • Sub term: appetizer 1, appetizer 2 (child terms)

I want to go to the drink page and have it display like this - all the posts within that taxonomy:

  • Drink

    • Drink 1

      • Drink 1 post
      • Drink 1 post
    • Drink 2

      • Drink 2 post
      • Drink 2 post

And the same for appetizer.

But, right now I get this:

  • Drink
    • Drink 1 post
    • Drink 1 post
    • Drink 2 post
    • Drink 2 post

Here is my code:

/*
Template Name: taxonomy-menu_type
*/
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
<?php get_header(); ?>

<div class="container-fluid ">
    <div class="row page-title-row">
        <div class="container">
            <h2><span>Menu</span> | <?php echo apply_filters( 'the_title', $term->name ); ?></h2>

            <?php if ( category_description() ) : // Show an optional category description ?>
                <p><?php echo category_description(); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>

<div class="container-fluid other-pgbody">
    <div class="row infor-row">
        <div class="container">

        <?php global $query_string;
        query_posts( $query_string . '&order=ASC' ); ?>
        <?php if (have_posts()) : ?>

            <?php
            $terms = get_terms($post->ID, "print_type");
            echo '<h3 class="child-term">'.$term->name.'</h3>';
            ?>

            <div class="col-sm-12 menu-list">

                <?php while (have_posts()) : the_post(); ?>

                    <div class="col-sm-12 price-row col-md-6 clearfix">

                        <div class="item-price">
                            <?php if ($price = get_post_meta($post->ID,'_marvilmedia_price',true)): ?><?php echo '$'.$price; ?><?php endif; ?>   
                        </div>

                        <div class="item-info">
                            <span><?php the_title(); ?></span><br/>
                            <?php $terms_as_text = get_the_term_list( $post->ID,'menu_side', '', ', ');
                            if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>
                        </div>
                    </div>

                <?php endwhile; ?>
                <!--/table-->
            </div>

            <?php else :
                echo '<h2>No Menu Item for '.$term->name.'</h2>'; ?>
            <?php endif; ?>

        </div>
    </div>

    <div class="row free-row">                
    </div>

</div>
<?php get_footer(); ?>

SOLVED

I found the answer in another post that was answered by @Gustav for anyone looking a solution like you can follow the link or view the code below it works perfectly. List post from current taxonomy children

THE WORKING CODE

        <?php 
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
    $termchildren = get_term_children( $current_term->term_id, $taxonomyName );
    foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName );
    $wpq = array (
    'taxonomy'=>$taxonomyName,
    'term'=>$term->slug,
    'order'=>'asc',
    'orderby'=>'title');
    $query = new WP_Query ($wpq);
    $taxonomy_img = get_term_meta( $term->term_id, 'tax-image', true );
    //echo "$term->name:<br />"; 
    ?>

    <div class="row infor-row">
    <div class="container">

    <?php echo '<h3 class="child-term">'.$term->name.'</h3>'; ?>
    <div class="col-sm-12 menu-list">

    <?php if ($query->have_posts() ) : ?> 
    <?php while ($query->have_posts() ) : $query->the_post(); ?>
    <div class="col-sm-12 price-row col-md-6 clearfix">
    <div class="item-price"><?php if ($price = get_post_meta($post->ID,'_marvilmedia_price',true)): ?><?php echo '$'.$price; ?><?php endif; ?></div>
    <div class="item-info">
    <span><?php the_title(); ?></span><br/>
    <?php $terms_as_text = get_the_term_list( $post->ID,'menu_side', '', ', '); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>
    </div>
    </div> 

    <?php endwhile; ?> 

    <!--/table-->
    </div>
    </div>
    </div>

    <div class="row free-row" style="background-image: url('<?php echo $taxonomy_img; ?>');">                 
    </div>
    <?php else :
    echo '<h2>No Menu Item for '.$term->name.'</h2>'; ?> 
    <?php endif;?>
    <?php wp_reset_query(); ?>
    <?php
    echo "";   
    }
    ?>

1 Answer 1

0

SOLVED

I found the answer in another post that was answered by @Gustav for anyone looking a solution like you can follow the link or view the code below it works perfectly. https://wordpress.stackexchange.com/questions/123059/list-post-from-current-taxonomy-children

THE WORKING CODE

        <?php 
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
    $termchildren = get_term_children( $current_term->term_id, $taxonomyName );
    foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName );
    $wpq = array (
    'taxonomy'=>$taxonomyName,
    'term'=>$term->slug,
    'order'=>'asc',
    'orderby'=>'title');
    $query = new WP_Query ($wpq);
    $taxonomy_img = get_term_meta( $term->term_id, 'tax-image', true );
    //echo "$term->name:<br />"; 
    ?>

    <div class="row infor-row">
    <div class="container">

    <?php echo '<h3 class="child-term">'.$term->name.'</h3>'; ?>
    <div class="col-sm-12 menu-list">

    <?php if ($query->have_posts() ) : ?> 
    <?php while ($query->have_posts() ) : $query->the_post(); ?>
    <div class="col-sm-12 price-row col-md-6 clearfix">
    <div class="item-price"><?php if ($price = get_post_meta($post->ID,'_marvilmedia_price',true)): ?><?php echo '$'.$price; ?><?php endif; ?></div>
    <div class="item-info">
    <span><?php the_title(); ?></span><br/>
    <?php $terms_as_text = get_the_term_list( $post->ID,'menu_side', '', ', '); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>
    </div>
    </div> 

    <?php endwhile; ?> 

    <!--/table-->
    </div>
    </div>
    </div>

    <div class="row free-row" style="background-image: url('<?php echo $taxonomy_img; ?>');">                 
    </div>
    <?php else :
    echo '<h2>No Menu Item for '.$term->name.'</h2>'; ?> 
    <?php endif;?>
    <?php wp_reset_query(); ?>
    <?php
    echo "";   
    }
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.