Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

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?
WordPressPHPjQueryWeb Development

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

Custom PHP code?

You probably looking for the is_page() function

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

Open in new window

Julian Hansen

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?
Robert Granlund

ASKER
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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Julian Hansen

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?
Kimputer

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).
Robert Granlund

ASKER
@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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kimputer

<?php

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

//code here
 
}
?>
Julian Hansen

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

Robert Granlund

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Robert Granlund

ASKER
@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
)
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.