Wordpress Only run code if on a certain page

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?
LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Custom PHP code?

You probably looking for the is_page() function

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

Open in new window

0
Julian HansenCommented:
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?
0
rgranlundAuthor 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?
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Julian HansenCommented:
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?
0
KimputerCommented:
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).
1
rgranlundAuthor 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?
0
KimputerCommented:
<?php

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

//code here
 
}
?>
0
Julian HansenCommented:
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

0
rgranlundAuthor 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
0
rgranlundAuthor 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
)
0
Julian HansenCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.