• hi there i’m new to all of this, so please excuse lameness

    what am i doing wrong here?

    i’m trying to use regex to match for the first part of a post slug (each time the same slug is used it adds a hyphen and an incremented interger), so the slug could be: blue, blue-2, blue-3, etc

    this line currently works in my header.php for the one off occurence of the slug blue-2:
    if(is_page('blue-2')){echo "kubrickbgblue.jpg";} else

    and i’m trying to replace that with something that matches any occurence of a slug starting with ‘blue’:
    $blue = ereg("^[blue]", is_page); //i'm escaping both square brackets but they seem to have been stripped out in this post
    //i think this should make $blue useable as a boolean
    if($blue) {echo 'kubrickbgblue.jpg';} else

    but alas i’m having no joy
    clearly i’m doing something wrong
    any suggestions regex users out there?

Viewing 8 replies - 1 through 8 (of 8 total)
  • is_page() returns only boolean true or false: you’re trying to use it as though it returned a string. You need to use another function that returns the post_title, not sure what that is. You could pull it from the DB directly, if you can’t find it otherwise.

    Also, instead of using the ereg set of functions, you should use the preg ones since they’re about a billion times faster (seriously, you do actually notice the difference):

    preg_match ('/find_this/', in_this);

    https://uk.php.net/manual/en/function.preg-match.php

    Thread Starter leeland

    (@leeland)

    hey thanks for that maerk,

    yes i thought is_post was a string
    if it’s a boolean, how come
    if(is_page('blue-2')){echo "kubrickbgblue.jpg";} else
    if(is_page('yellow')){echo "kubrickbgyellow.jpg";} else

    works?

    i’ll check your link and keep on mulling

    is_page('blue-2')

    will still only return true or false. Instead of checking whether or not it is a page, it just checks whether or not it is page ‘blue-2’.

    Update:

    You need to get the page_title first. If you’re doing this within the loop, the title should already be accessible via $post->post_title

    All you should need to do is:

    if ( preg_match('/^(blue)/', $post->post_title) ) {
    echo 'kubrickblue.jpg';
    }

    Thread Starter leeland

    (@leeland)

    you’re most patient maerk
    this looks good, i’ll give your suggestion a go
    it’s a pity, as i liked the way i could change the slug and not affect the page title, keeping the names of these pages more informative
    thanks again!

    I’m so sorry, I’ve made a mistake! The post_title is the actual title of the post, the post slug is stored under post_name.

    Try this:

    if ( preg_match('/^(blue)/', $post->post_name) ) {
    echo 'kubrickblue.jpg';
    }

    You can change the post slug all you want and it won’t affect the title. Also, you can change the title, and it won’t touch the slug ?? It’s actually a good idea not to change the slug too much, since it’s used in the URL of the posts.

    Thread Starter leeland

    (@leeland)

    thanks maerk
    sorry for the lag, i’ve been away
    i can’t seem to get this preg_match to work at all
    not sure what the problem is and can’t spend anymore time on it
    sorry, i’m giving up and using an embarrassingly blunt list of possible ‘blues’ like this:


    if(is_page('blue')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-2')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-3')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-4')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-5')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-6')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-7')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-8')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-9')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-10')){echo "kubrickbgblue.jpg";} else
    if(is_page('blue-11')){echo "kubrickbgblue.jpg";} else

    i’m sure your stomach will turn when you see this
    i’m totally embarrassed and hang my head in shame
    o well
    thanks for the generous help anyway
    regards

    Hey, no shame! Coding is complicated, sometimes things don’t work and you can’t always see why!

    If you post the contents of the PHP file to https://pastebin.be I’ll take a look at it and see what I can do.

    Thread Starter leeland

    (@leeland)

    thanks maerk
    here’s the pastebin link for the working header.php file
    https://pastebin.be/3024/

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘regex question’ is closed to new replies.