Solved

Check for string in WordPress Slug/URL

Posted on 2014-01-13
1
550 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
ID: 39778038
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

830 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