0

The page slug for each of our wordpress articles is a unique set of seven digits, and we are using that string of numbers as the page slug.

In trying to learn how to customize our site even further, I'd like to learn how to echo that slug in template files (outside the loop - it would actually be used in the sidebar). Here's what I've tried so far (and failed miserably):

At the top of the post template file, I have this code:

<?php $doi = get_page_template_slug( $post_id ); ?>

So I'm trying to create a $doi variable that has the page slug.

Later on in the template file, I'm trying to echo that variable:

<?php echo $doi; ?>

That also fails. Anyone got time to help a PHP newbie out?

  • possible duplicate of How can I get page slug – kraftner Aug 11 '14 at 19:30
  • Thanks for suggesting a possible duplicate - but I wasn't able to discern an answer from that answer. Since I'm trying to do this outside the loop, I couldn't make sense out of the second post on that page. – Hal Atkins Aug 11 '14 at 19:35
  • If you use the Outside the Loop instructions from the duplicate question, your page slug should be stored in the $slug variable. Then you should be able to use it as you're attempting to use $doi in your question. – Pat J Aug 14 '14 at 23:41
1

Try this:

$post = get_post($post_id); 
$slug = $post->post_name;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.