PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] Find out whether a template file is used by a page and get that page -Wordpress

 March 13, 2022     php, templates, wordpress     No comments   

Issue

I'm developing now a wordpress theme, and wondering if there is an option to know if a template file is being used by a page? I need the link to that page... thanks!


Solution

The function get_page_template_slug( $post_id ) will return the slug of the currently assigned page template (or an empty string if no template has been assigned - or false if the $post_id does not correspond to an actual page). You can easily use this anywhere (in The Loop, or outside) to determine whether any page has been assigned a page template.

is_page_template(); function will return true when template is used. you can also pass file name to check if particular template is applied or not.

if ( is_page_template('about.php') ) {
// Returns true when 'about.php' is being used.
} else {
// Returns false when 'about.php' is not being used.
}

There is one more method too. You can use get_post_meta to get value of applied template.

global $post;
get_post_meta($post->ID,'_wp_page_template',true);


Answered By - Bhavesh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing