WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

if i usage below code, that show category id "20"

<?php echo $ct_portfoliocatid; ?>

But when i usage "in_category" for generate category ID '20'. Like this:

<?php if (in_category('<?php echo $ct_portfoliocatid; ?>')) : ?> 
<?php include (TEMPLATEPATH . '/portfolio.php'); ?> 
<?php else : ?>
<?php endif; ?>

The code did not work, please help me.

share|improve this question

closed as off topic by s_ha_dum, brasofilo, Johannes Pille, toscho Apr 12 '13 at 3:06

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

This is a pure PHP question. Your code is broken-- badly broken. Among other things, single quoted strings do not expand, and there is no need for the php tags. Try : in_category($ct_portfoliocatid) Voting to close as off-topic per the faq. – s_ha_dum Apr 11 '13 at 23:19
I have test like this: <?php if (in_category($ct_portfoliocatid)) : ?> but still not work – v123shine Apr 11 '13 at 23:24

If you are using in_category correctly, your code modified as per my comment should work. That is...

if (in_category($ct_portfoliocatid)) :
    include (TEMPLATEPATH . '/portfolio.php');
else :

You don't need all those messy opening and closing PHP tags so I removed them and I don't know why you have an else at all.

From the Codex for in_category:

Tests if the current post (or any specified post) is assigned to any of the specified categories.

in_category() considers only the categories a post is directly assigned to (the checked categories in Write/Edit Post panel), not the parents of the assigned categories (but see Testing if a post is in a descendant category below).

This tag can be used to test the current post within The Loop or (since Version 2.7) outside the Loop during a single post request. You can use it anywhere if you specify which post you want to test.

So, if that code is in the Loop (you did not supply the second parameter so it defaults to the current post in the Loop), and if the category is directly assigned to the post and not a parent category of a category assigned to the post, that code should work, and does work when I try it. Of course, $ct_portfoliocatid also has to be set correctly and I have no way to know if it is.

Again, your original code is badly broken but perhaps another issue is that you are not using the function correctly.

share|improve this answer
+1 for your patience. – vancoder Apr 11 '13 at 23:41
I create "Theme Option" in wp admin. This is piece of my code for create a form on "Theme Option" : array( "name" => "CATEGORY", "type" => "title"), array( "type" => "open"), array( "name" => "Portfolio Category ID", "desc" => "Insert your 'Portfolio Category ID' in here", "id" => $shortname."_portfoliocatid", "std" => "", "type" => "text") When I go to "Theme option" I enter category ID "20" on the Form. – v123shine Apr 12 '13 at 0:02
I load the category ID using this code: <?php echo $ct_portfoliocatid; ?> – v123shine Apr 12 '13 at 0:04
And now i want that code on here: <?php if (in_category('<?php echo $ct_portfoliocatid; ?>')) : ?> <?php include (TEMPLATEPATH . '/portfolio.php'); ?> <?php else : ?> Default category <?php endif; ?> Forgive me if my english language not good. – v123shine Apr 12 '13 at 0:05
Please edit your question to include all of this code and additional information. – s_ha_dum Apr 12 '13 at 0:29

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