3

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.

  • Can you provide some context as to why you want to do this? – Tom J Nowell Nov 29 '16 at 16:04
  • @Tom J Nowell ... question updated accordingly. – glvr Nov 30 '16 at 15:32
  • I am actually not sure why you used is_single( 'beef-stew' ) @gulliver, just to note that both beef and stew permalink parts have the same conditional power and hyphen is just to separate them? – prosti Nov 30 '16 at 16:03
  • If you're using wp_nav_menu it will already mark the current active menu item and any ancestors, but it looks like your real question is actually "How do I dynamically add CSS classes to menu nav items depending on a check?", and that this is an XY Problem – Tom J Nowell Nov 30 '16 at 21:01
  • @prosti ... from memory (and I've been away in hospital, so can't immediately recall), I think 'beef-stew' is a cut/paste from the Codex example. – glvr Dec 21 '16 at 13:33
4

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.

  • @ Howdy_McGee ... thanks. Appreciated. I've thus far not been able to add it to existing code (original post updated), and will search more for ways to do so. If you're able to provide a clue, I'll welcome it. :-) – glvr Nov 30 '16 at 15:35
  • @gulliver - Well, from how you describe your issue, it sounds like you would want to replace your is_single() conditional with this instead. – Howdy_McGee Nov 30 '16 at 15:58
2

In a similar way as @Howdy_McGee, we might try to construct an helper function:

/**
 * @param $s String 
 * @return bool
 */
function queried_post_name_contains_wpse247782( $s )
{
    $obj = get_queried_object();

    return     $obj instanceof \WP_Post // Make sure it's a post object
            && false !== strpos( $obj->post_name, $s ); // post name contains it
}

and use it like:

if( is_single() && queried_post_name_contains_wpse247782( 'beef' ) )
{
    // ...
}

Hope you can extend it futher to your needs!

  • 1
    @ birgire ...thanks. Regrettably, my small knowledge of php is insufficient to understand. Sorry. :-) – glvr Nov 30 '16 at 15:37
  • I hopefully simplified it by replacing the reg-ex part with strpos and removed the is_single() check out of the helper function. @gulliver – birgire Nov 30 '16 at 15:48
  • @ birgire ... thanks. Sorry for my slow response - unexpected surgery. – glvr Dec 21 '16 at 13:30
  • no problem - All the best for your recovery @gulliver – birgire Dec 21 '16 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.