Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this php code I am using to list all post (titles) with the taxonomy categories names, and its working fine however I will like to break it up and avoid using echo within the php code so that I can insert some more code. Here's the code I have that works --

<?php
$custom_terms = get_terms('videoscategory');
$other_custom_terms = get_terms('product_category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'product', 'orderby' => 'title', 
                'order' => 'asc',
        'tax_query' => array(
  'relation' => 'AND',
'orderby' => 'title', 
                'order' => 'asc',
    array(
        'taxonomy' => 'product_category',
        'field' => 'slug',
        'terms' => $other_custom_term->slug,
'orderby' => 'title', 
                'order' => 'asc',
    ),
            array(
                'taxonomy' => 'videoscategory',
                'field' => 'slug',
                'terms' => $custom_term->slug,
'orderby' => 'title', 
                'order' => 'asc',

            ),
        ),
  );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
        endwhile;
     }
}
} ?>

And here's the code I have now that I have edited to try and accomplish what I would like to do --

<?php
$custom_terms = get_terms('videoscategory');
$other_custom_terms = get_terms('product_category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'product', 'orderby' => 'title', 
                'order' => 'asc',
        'tax_query' => array(
  'relation' => 'AND',
'orderby' => 'title', 
                'order' => 'asc',
    array(
        'taxonomy' => 'product_category',
        'field' => 'slug',
        'terms' => $other_custom_term->slug,
'orderby' => 'title', 
                'order' => 'asc',
    ),
            array(
                'taxonomy' => 'videoscategory',
                'field' => 'slug',
                'terms' => $custom_term->slug,
'orderby' => 'title', 
                'order' => 'asc',

            ),
        ),
     );
}
}
}
     $loop = new WP_Query($args);
     if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); ?>
<div class="box <?php echo $custom_term->slug; ?>">
        <h1 style="font-weight:bold;font-size:18px;margin-top:10px;"><?php echo $custom_term->name ?></h1>
<h2><a href="<?php get_permalink(); ?>"><?php get_the_title(); ?></a></h2>
</div>
       <?php endwhile; ?>

Basically what Im trying to do is assign the term name or slug as a class to my div so that I can use isotope filter to filter the list of posts by category. However I am receiving parse/syntax errors, and things are not working. What am I doing wrong? Any help is appreciated, thanks.

share|improve this question
    
Functions that start with get_ return a string or array or object..., functions that start with the_ echo out the result. Review your code with that in mind. –  elclanrs Apr 15 '13 at 5:47
    
Can you show parse/syntax errors that you are receiving? –  Vinod Dalvi Apr 15 '13 at 5:50
    
@elclanrs Thanks for the tip. Vinod Dalvi The error I received was for the extra } , however even after removing it did not work. –  mygm26 Apr 15 '13 at 6:39

1 Answer 1

up vote 1 down vote accepted

You have an extra "}", there must be two such braces for two for each loops. Also you need to have endif; statement at end after endwhile. Parsing error may be due to extra "}". Please try below code and let us know if it works.

 <?php
$custom_terms = get_terms('videoscategory');
$other_custom_terms = get_terms('product_category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'product', 'orderby' => 'title', 
                'order' => 'asc',
        'tax_query' => array(
  'relation' => 'AND',
'orderby' => 'title', 
                'order' => 'asc',
    array(
        'taxonomy' => 'product_category',
        'field' => 'slug',
        'terms' => $other_custom_term->slug,
'orderby' => 'title', 
                'order' => 'asc',
    ),
            array(
                'taxonomy' => 'videoscategory',
                'field' => 'slug',
                'terms' => $custom_term->slug,
'orderby' => 'title', 
                'order' => 'asc',

            ),
        ),
  );

     $loop = new WP_Query($args);
     if($loop->have_posts()) 
     {
        ?>
        <div class="box <?php echo $custom_term->slug; ?>">
        <h1 style="margin-top:10px;"><?php echo $custom_term->name;?></h1>

      <?php       
      while($loop->have_posts()) : $loop->the_post(); ?>
            <h2><a href="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></a></h2>
      <?php  endwhile; ?>
      </div>
     <?php   
     }
}
} ?>
share|improve this answer
    
Hello thanks for the response. However when using the suggested code it shows only 1 category name repeatedly and without any posts titles/links. So it just repeats 1 category's custom_term->name repeatedly. Any idea as to why it's doing so? –  mygm26 Apr 15 '13 at 6:41
    
@mygm26 have edited my answer, please change "}}" for for each loops at end of code. –  Rajeev Vyas Apr 15 '13 at 6:57
    
Hello and thanks again. I updated my code with your edited code and it seems to be headed in the right direction, however it is now showing me all the category names several times and still no posts titles. Here's a screenshot of how it looks with the edited code -- i50.tinypic.com/2w5lfti.png and here's a screenshot of how it's suppose to look, using my original code -- i45.tinypic.com/2u7r5og.png –  mygm26 Apr 15 '13 at 7:05
    
@mygm26 See again i have edited, have tried to break php code from your original code. –  Rajeev Vyas Apr 15 '13 at 7:11
    
Thanks again for your time and responses. Though it works as my original code did, it's not what I was looking for, you removed the most essential code I am trying to use and utilize, which is <div class="box <?php echo $custom_term->slug; ?>"> I am trying to place the category name and posts that belong to that category within a div. That's why I was trying to break up the code, because when using the div like such -- –  mygm26 Apr 15 '13 at 7:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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