0

I have this function :

<?php
function getmypost($number)
    {
        query_posts('p=1828');
        while (have_posts()) : the_post();
        the_title('<h1>', '</h1>');
        the_content();
        endwhile;
    }
?>

I need to make the 1828 as a variable I have tried this:

    query_posts('\'p='. $number .'\'');

But it does not work. What would be the right way to do this?

Junuxx
  • 14,011
  • 5
  • 41
  • 71
menardmam
  • 9,860
  • 28
  • 85
  • 113

2 Answers2

3

If I understand you correctly

query_posts('p='.$number);

should work.

If you need a single quote ' in the string you'd escape the '

query_posts('p=\''.$number.'\'');

or using double quotes (more elegant, and you can put the variable straight in. Dominik already suggested this in his answer)

query_posts("p='$number'");
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • ok, it work.. byt i need more explaination.... inside the () there must be the '' the same as the delimiter... how can it work ? – menardmam Feb 10 '10 at 15:04
  • you could also use query_posts("p=$number"); – Dominik Feb 10 '10 at 15:04
  • 2
    or `("p={$number}")` which would also work if you have a more complex variable name, such as `("p={$numbers[$index]['foo']->value}")` – Wim Feb 10 '10 at 15:11
  • 1
    This works because it is essentially getting changed to p=valueofnumberhere. What you had was before: query_posts('\'p='. $number .'\''); was getting changed to 'p=valueofnumberhere' (notice how your value has extra quotes in it). If you modified what you had query_posts('p='. $number .''); it would work as well, although the extra .'' at the end is unnecessary. – Brian Feb 10 '10 at 15:12
  • 1
    @marc-andre: You have no delimiters in your example. The ' that you see are the marks of a string in PHP: http://de2.php.net/manual/en/language.types.string.php – Boldewyn Feb 10 '10 at 15:13
0

You could use

query_posts("'p=$number'");
Dominik
  • 1,194
  • 6
  • 9