tags:

views:

151

answers:

7

I am trying to add some logic to a wordpress template. I want to build a simple if conditional which will check if a variable equals 26. The variable "$value" must contain some extra hidden characters, because when I echo the content... 26 appears before the echo string values that should appear first. Therefore, the if-condition does not work either. What is happening here and how can i fix it?

CODE:

$value=the_ID();   // a wordpress function which contains the id of the current page/post.
echo "value=(" . $value . ")";

OUTPUT:

26value=()
+2  A: 

the_ID() isn't returning a value, it's printing it out. That's why you can't assign it to that variable.

If you can edit the function have it return the value (although that may break other parts of the app that are using that function). Or capture it like this:

ob_start();
the_ID();
$value = ob_get_clean();
echo "value=(" . $value . ")";
John Conde
+1  A: 

Looks like the_ID() echoes the ID itself.

Felix Kling
+6  A: 

This is because the_ID() is a template function: it only echo's out the ID; it doesn't return it. You want to be using get_the_ID() instead.

Brendon
Or ob_start();the_ID();$value=ob_get_clean(); Why somebody would do that? Because they can :) (Disclaimer: Not to be taken seriously)
Bart van Heukelom
I'm such a noob at php and wordpress.Thanks! Where is the best place to see the wordpress docs that would have shown me the get_the_ID() function?
panofish
@Bart van Heukelom or because they aren't a wordpress coder and didn't know get_the_ID() existed. ;)
John Conde
http://codex.wordpress.org/Template_Tags/the_ID
Alex JL
Wow, I am amazed at the speed and quality of the responses at stackoverflow. I will wield this power carefully and try not to abuse it. THANKS AGAIN.
panofish
+2  A: 

What happens when you comment out the echo? Does the 26 still appear? If so then the function does echo it instead of returning it.

Use var_dump to print out the variable's content for debugging:

var_dump( $value );
poke
agreed. you fuind out if it is a string, how many characters it contaions etc etc.
Bingy
+1  A: 

You could either do this:

echo "value=(".the_ID().")";

or if you'd like to assign value to use later in your script, this:

$value=get_the_ID();
echo "value=($value)";

You say you'd like to use an if in the script, so that could work similarly -

if(get_the_ID()==26){/*do whatever*/}

You could of course also use $value==26 if you assigned it earlier.

Alex JL
+1  A: 

As many people suggest that the_ID() do the echoing. There are a few things you can do here.

First, see if WordPress has a function to return the ID instead of printing it (I personally don't know).

Second, you may just rearrange the code to let it print as you like ... like this

echo "value=(" . the_ID() . ")";

OR

echo "value=("; the_ID(); echo ")";

Third, If you really want to have it in a variable, you may capture the echo output. Like this

ob_start();
the_ID();
$Value = ob_get_contents();
ob_end_clean();
echo $Value;

Hope this helps.

NawaMan
A: 

I bet the function you are calling echoes the number 26..

so.. value will actually = null..

so the_ID() probably is a function like this:

function the_ID(){
    $id = 26;
    echo $id;
}

ie: it does not return anything, it echoes the result. (if you traul through teh wordpress source code you could probably fuind exactly what is happening... and find out where the_ID() is actulaly getting ethe page id from.

I did a quick search on the wordpress instructions:

Note: This function displays the ID of the post, to return the ID use get_the_ID(). Usage

<?php the_ID(); ?>

so you are using the wrong function

use get_the_ID() not the_ID()

the_ID(); //prints teh page id
$value=get_the_ID(); //returns the page_id

http://codex.wordpress.org/Template_Tags/the_ID

Bingy