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 wp_dropdown_categories working without setting child_of. Once I set child_of, all categories disappear. Here's my code, what should I do to enable child_of?

function dropdown(){
    $book_term=term_exists('book','category');
    $book_term_id = $book_term['term_id']; 
    $dropdown = array(
    'child_of'   => $book_term_id,
    'hide_empty' => 0
    )
?>
    wp_dropdown_categories( $dropdown );
<?php
}
share|improve this question
    
Where is $book_term_id defined? –  Stephen Radford Jan 4 '12 at 11:45
    
inside this function, $book_term=term_exists('book','category'); $book_term_id = $book_term['term_id']; I tried var_dump, the id is correct. –  Jenny Jan 4 '12 at 11:54
    
You have the call to the function outside the PHP context and you're missing a trailing semicolon ; after the function. Please turn define( 'WP_DEBUG', true ); on in your wp-config.php file and tell us about the error output. –  kaiser Mar 5 '13 at 13:20

2 Answers 2

not sure if this is THE problem, but there are problems. your PHP (WP) function is outside of PHP:

?>
    wp_dropdown_categories( $dropdown );
<?php

so it'll print as text, not execute.

also, when you put it w/in PHP, make sure to add a semi-colon after the array's closing parenthesis:

  $dropdown = array(
    'child_of'   => $book_term_id,
    'hide_empty' => 0
  );
  wp_dropdown_categories( $dropdown );
}
share|improve this answer

The variable $book_term_id has not been set. You either need to pass it to your function or set it in your function.

Pass it to your function:

<?php dropdown($book_term_id); ?>

Set it in your function:

function dropdown(){
  $book_term_id = 5; // if a global variable, you can just use:  global $book_term_id;  
  $dropdown = array(
    'child_of'   => $book_term_id,
    'hide_empty' => 0
  )
  wp_dropdown_categories( $dropdown );
}
share|improve this answer
    
sorry, I just edited my question. I have the book_term_id defined inside the funtion. Initially I didn't paste it here just for simplify things. –  Jenny Jan 4 '12 at 11:58
    
Ok. I see you've updated your code. Are you getting the term id properly? See updated code above. –  Velvet Blues Jan 4 '12 at 11:59
    
I think I get the id correctly. I tried var_dump, it returns the id number. So, my $book_term_id is the same as using the id number. –  Jenny Jan 4 '12 at 12:04
    
What did you do var_dump on? On $book_term_id? If not, do an echo on $book_term_id and see what it returns. (I assume there are child categories.) –  Velvet Blues Jan 4 '12 at 12:08
    
either var_dump or echo $book_term_id returns number of 56. this 56 is the top level term, it has children, but not grandchildren. $book_term->term_id doesn't work, echo or var_dump $book_term->term_id got no result. –  Jenny Jan 4 '12 at 12:15

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.