I think you’re misunderstanding the function get_page_template_slug
When you call $template = get_page_template_slug( $post_id );
it won’t return index.php
if you are using the default theme template. It only works when viewing Pages and will either return the slug of the Custom Page Template assigned to that Page or an empty string otherwise.
So if ( 'index.php' == $template )
will always be false.
Rather than trying to detect which template file is running, use WP’s is_ functions.
true == is_front_page()
on your site’s home page, for example.
There is a whole range of these functions to check the sort of page you are on. Have a read up on them in the documentation available online. If really feel the need to check whether you are using the index.php template then you could set a global flag variable at the start of that template and check it within your function, I suppose.
Related Posts:
- Populate editor with some content of a page with a page template
- Function to return true if current page has child pages
- Proper use of Output Buffer
- List of all theme customizer control types?
- Need help setting default setting value for radio button in theme customizer
- function_exists call in function.php
- Check if a menu is empty?
- Is it possible to get a theme customizer setting from wp.customize using jquery?
- Debugging an error: wp_enqueue_style was called incorrectly
- How to get the registered sidebar’s name by its id?
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- Dynamically change feature image in customiser
- One button to change all settings in theme customizer?
- Hide a menu-item and its submenus and display a ‘Log in’ link if the user is logged out
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Design view breaking on Pages
- Understanding WordPress child theme custom JS loading
- How can I loop into two different DIVS without repeating the DIVs
- Removing unnecessary wordpress files
- Remove css styles from specific page
- How can one utilize a variable as a callback function name for add_settings_field
- How do I list the_tags() into HTML data-attribute
- Switching between custom templates in a post type of the admin menu
- How to make thumbnail image fit into a div where image dimentions are completely different?
- Custom Logo URL | Help me print the URL of the custom logo I inserted into my theme
- What is the fastest way to load PHP functions that are only used in one theme template?
- My Own layout in WooCommerce pages [closed]
- Differences when using the the_time and the_date functions
- Changing the template hierarchy
- Can’t load WP function into external function
- Trigger popup in a php if/else statement
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- Showing content from one page on another
- Converting HTML Template to WordPress Theme
- How to remove the cufon script from Dzonia Lite theme [closed]
- How do I add custom bulk actions to multiple custom post types?
- what is the best practice to add new field to an api route
- Can’t access variable outside for loop
- Error in custom php function doesn’t exist
- Catchable fatal error: Object of class stdClass could not be converted to string after WP 4.7
- Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
- Skt full width basic slideshow problem
- How to disable controls in theme customizer?
- How do I link a button I created in theme customizer to a function?
- Trying to retrieve post meta
- Is there a way to conditionally check whether a WordPress post title is empty?
- Is the “_s” on this `sprintf(__(‘Page %s’, ‘_s’), max($paged, $page))` just refer to a text domain?
- Inject class in body when particular page template is used
- WordPress admin-ajax.php
- Use .php file as page instead of wordpress page & template file?
- Use WordPress function in php file
- Adding wrapper elements in the_date() like in the_title()?
- How to obtain the current website URL in my theme?
- What exactly do this function declared into functions.php file of a WP theme?
- Show meta box only for default page template
- How to call multiple functions from multiple files into a WordPress page template [closed]
- get_template_directory adding FTP root folders in urls
- Theme not calling Jquery properly
- Where is this function’s callback getting its arguments from?
- What is the best way to define constant options for a theme?
- How to make function appear in sentence?
- get_page_template_slug returns incorrect path
- Removing “Powered by” footer using child theme PHP [closed]
- How to add aria role and schema markup to custom walker container
- get_page_templates only return templates with “home” in the filename
- Warning: call_user_func() expects parameter 1 to be a valid callback, function
- Is there a hack for using is_page() within the function.php file?
- Show different website layout if no sidebar added
- Need advice on theme customizer and child themes
- How to make sure relative URL works when site is not on root domain?
- Display additional page templates and a sidebar on plugin activation
- Getting text from custom field from customizer
- Dynamically switch file in get_template_directory_uri() | Function [closed]
- Need help for some PHP code
- Uncaught TypeError: extract(): Argument #1 ($array) must be of type array, null given
- How to change basename url for wp-admin?
- how to replace h1 entry title with h2 in category pages only
- Contain multiple page templates in one PHP custom template file in WordPress?
- Problem with displaying CSS Stylesheets – Am I adding them correctly in my wordpress child theme?
- require get_template_directory() . ‘path/to-my/file.php’ BREAKS customize > themes functionality
- How can I prevent a shortcode div from extending beyond its boundaries?
- Menu to the right of screen on desktop using Bootstrap 4
- remove_action() not working in page template – Genesis
- What is the proper way to call a function (from functions.php) on a link click?
- Create a custom theme-specific page, invisible in the admin-panel?
- Extend the WP_Customize_Image_Control class to change its non-frame $button_labels
- A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- Save the outputted image into the Media Library, with a different filename and extension
- WordPress get_post_meta issue
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- How to add button to top of theme customizer?
- Adding code to the function file
- Display custom meta box in my template file
- Adding php within a return statement [closed]
- I need a button to appear when not loged in and another when logged in, I need help fixing code PLEASE!
- Load slideshow.css file only if Slideshow is checked / on
- Loop on a wordpress Page instead of content coming from the WP text editor
- How can I fix my pagination?
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week