Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check for string in WordPress Slug/URL

Posted on 2014-01-13
1
Medium Priority
?
730 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

885 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