Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Alessandro Maculotti
Alessandro Maculotti
4,954 Points

WP_Query problem

Hi! I will try to do a loop that display a custom post with a the same category of the current article. If I pass the category name to a simple variable the code works, when I pass a variables this doesn't work.

Here my code, probably i have a syntax error.

<?php
    wp_reset_postdata();
    wp_reset_query();

    $currentCategory = get_the_category_list();
    $args = array(
      'post_type' => 'puppies',
      'category_name'  => $currentCategory
    );
    $the_query = new WP_Query( $args );?>

Thanks in advice

2 Answers

Bob McCarty
PLUS
Bob McCarty
Courses Plus Student 16,618 Points

Alessandro

Did you try echoing the $currentCategory value?

<?php
    wp_reset_postdata();
    wp_reset_query();

    $currentCategory = echo get_the_category_list(', ');
    $args = array(
      'post_type' => 'puppies',
      'category_name'  =>  $currentCategory
    );
    $the_query = new WP_Query( $args );?>
Alessandro Maculotti
Alessandro Maculotti
4,954 Points

Hi Bob and thanks so much.

Yes I tried this ticks but doesn't work. If I'll do this the page returns a 500 server error.

Do you think I have to push it into array?

Thanks a lot and sorry for my bad english.