开发者

What is happening with this PHP echo and how do I resolve it?

I am trying to add some logic to a wordpre开发者_开发技巧ss 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=()


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.


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 . ")";


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 );


Looks like the_ID() echoes the ID itself.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜