• Resolved t567

    (@t567)


    hi @ all,

    i’m trying to get wp_title as a string in order to use it in a if statement, hence i want to display content only if the string has a certain value (page title). i put the following into the top level index.php:

    <?php $var = wp_title('',false);  // putting wp_title into string
    echo "$var";  // just a test - works, prints the title of the page
    if($var == "PAGE TITLE") // no more than a simple if statement...
    {
     echo "lorem ipsum"; // no output :(
    ?>

    i’m not using any theme but inserted wp into an existing website.

    any hints?

    regards t567

Viewing 7 replies - 1 through 7 (of 7 total)
  • Is $var actually “PAGE TITLE”? To do that sort of comparison, the two strings must be exactly the same. If $var is ‘Page Title” it won’t work (different capitalisation), or if there’s two spaces, or any one of a lot of other things.

    There’s other PHP functions that might be more useful, like strcmp () so have a look at a few options and see what works for you.

    Thread Starter t567

    (@t567)

    hi michael,

    thnx for your reply.

    >Is $var actually “PAGE TITLE”?

    no, i guess i didn’t put it clear enough, i’m trying to use wp_title as a variable. so if wp_title i.e. is “Mustang”, hence the related page is about mustangs and called accordingly, i want to use the variable “Mustang” in the if statement in order to display some extra content only related to the page about mustangs.

    the above script works fine as it is but it does not when inserted in the index.php.

    OK, either way the actual string that is is doesn’t really matter, as the yshould match if they are the same.

    Try changing the comparison function to strcmp() and see waht that outputs. That’s really all that I can think of to do.

    Thread Starter t567

    (@t567)

    ok, i’ll give it a try…

    Thread Starter t567

    (@t567)

    hm, no, doesn’t do anything else. the strcmp() works fine on a test-file but doesn’t on the index.php.

    <?php $var = wp_title('',false);
    echo "$var";  // just a test, works
    if (strcmp("$var","Mustang") == 0)
    {
    echo "bananas are somewhat smaller than mustangs.";
    }
    ?>

    maybe i shouldn’t mess around to much with wp, hence there is some more to it than bananas and mustangs ??

    wp_title() seems to return the string with (two) prepended space characters;

    try to use:

    if(trim($var) == "PAGE TITLE")

    https://php.net/manual/en/function.trim.php

    Thread Starter t567

    (@t567)

    >wp_title() seems to return the string with (two) prepended space characters;

    obviously it does. your hint immediately did solve the problem. thanx a lot!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘wp_title –> var’ is closed to new replies.