Wordpress Only run code if on a certain page

Robert Granlund
Robert Granlund used Ask the Experts™
on
I have a wordpress site and some custom code that I only what to run if the post is part of a Series.
So my address http://www.MYSITE.com/series/interesting-facts

I want the custom code to run when I am in "Series".  How do I do that?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Custom PHP code?

You probably looking for the is_page() function

if (is_page('interesting-facts')) {
  // Do something awesome here
}

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
After re-reading the question I don't think this will work.

I am assuming you are using a plugin to manage your series? Which one?

Author

Commented:
I'm not using a plugin.
But the above is not correct, what I mean is:
if (is_page('series')) {
  // Do something awesome here
}

Can I do that?  Using that part of the URL?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
No - the is_page only works on the name of the page - series would be common to all pages.

How have you implemented your pages in your series?

Commented:
Use $_SERVER['REQUEST_URI'], check that the first part is '/series'
Just make sure those pages are based on a template (and have the template contain the PHP code).

Author

Commented:
@Julian:
The template is taxonomy-series.php per the wordpress instructions.  For somereason I can't get it to work by the template name.

@Kimputer:
Use $_SERVER['REQUEST_URI'], check that the first part is '/series' -- How do I write that?

Commented:
<?php

if (substr($_SERVER['REQUEST_URI'], 0, 7) == '/series') {

//code here
 
}
?>
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
The $_SERVER approach will most likely work but I am interested to see what the WP way of doing this is.

If you put this code into taxonomy-series.php - what do you get
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
echo "<pre>" . print_r($term, true) . "</pre>"; ?>

Open in new window

Author

Commented:
AH!  Also, I know this is not recomended but can y'all look at this question?
https://www.experts-exchange.com/questions/29008620/Wordpress-Query.html

Author

Commented:
@julian:
WP_Term Object
(
    [term_id] => 16
    [name] => Serie 1
    [slug] => serie-1
    [term_group] => 0
    [term_taxonomy_id] => 16
    [taxonomy] => series
    [description] =>
    [parent] => 0
    [count] => 1
    [filter] => raw
    [term_order] => 0
)
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Ok - so you can try checking for
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term['taxonomy'] == 'series') {
   // good to go here
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial