Solved

Check for string in WordPress Slug/URL

Posted on 2014-01-13
1
534 Views
Last Modified: 2014-01-13
On my WordPress site, I have a structure like:

Home
page1
page2
Subject
   pageA
   pageB
      page2B
      page3B
About
Contact


where pageA, pageB, page2B and page3B all have "Subject" as their parent page.

I'm want to write a function that would detect if the term "Subject" is in the URL/page slug.

I've been trying various combinations of WordPress functions such as is_page() and get_ancestors(), but can't seem to find a way of achieving what I need.

So, the psuedo-code would be:


if (URL contains "Subject" or parent is "Subject") {
   echo something
}
else {
   echo something else
}
0
Comment
Question by:Tintin
1 Comment
 
LVL 48

Accepted Solution

by:
Tintin earned 0 total points
Comment Utility
I've come up with the following solution, but I'll leave this question open, in case someone has a better suggestion.

$path = get_permalink();

if ( is_page() && strpos($path,'Subject')) {
     echo "<h3>Subject</h3>";
}
else {
     echo "<h3>Not Subject</h3>";
}
        

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now