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

Friday, August 26, 2022

[FIXED] How to catch the specific page template in WordPress

 August 26, 2022     custom-wordpress-pages, wordpress, wordpress-theming     No comments   

Issue

Here is a screenshot:

See the screenshot

I want to catch a specific page template and show meta box according the specific page template. I have tried to with it:

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

but is not working.


Solution

Add the following code to functions.php:

add_filter('template_include', 'var_template_include', 1000);
function var_template_include($t) {
    $GLOBALS['current_theme_template'] = basename($t);
    return $t;
}
    
function get_current_template() {
    if (isset($GLOBALS['current_theme_template'])) {
        return $GLOBALS['current_theme_template'];
    } else {
        return false;
    }
}

You'll then be able to do the following:

$pageTemplate = get_current_template();


Answered By - Hillel
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

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