Is there a way (and if so, ‘how?’) to apply a conditional to a page/post for which the slug contains a specific word.

In the case of the Codex example of:

is_single( 'beef-stew' )

The condition will still apply if the slug contains ‘beef’, rather than being ‘beef-stew’.

UPDATE: In response to a request for context…

I have a nav-menu which uses a conditional statement (example below) to apply a css class to an item when it’s the page being viewed.

 <?php if (is_single('mon')) { echo " class=\"current\""; }?>

In the example above ‘mon’ is ‘monday’, with similar for other days in a weekly schedule. By adding to a template, it can be used for any day provided I set an appropriate slug… an example of which is to use ‘mon’ instead of ‘Monday April 4’.

I want to apply the condition to just part of the slug, thus saving the time of manually modifying the slug prior to saving… and also removing the situation of mis-function if I forget to do so.

2 Answers
2

WordPress has no conditional for substring testing but you can do this using PHPs built-in function: strpos()

global $post;

if( false !== strpos( $post->post_name, 'beef' ) ) {
    // Do Things
}

The above returns true if beef is found somewhere in the post slug.

Leave a Reply

Your email address will not be published. Required fields are marked *