get url page template

I have created a page template where I need current page URL

I have tried

get_site_url();  

and

get_permalink(); 

but it shows nothing.

Let me know is anything missing in code.

Replay

if you're using a page template, then to get current page URL you could try to use this function:

get_permalink();

or

echo get_permalink();

if you want to print the result

If you want to get URL of current custom page template, get_permalink() should work inside that template as long as the global $post is available.

But if you need to get URL of the custom page template from outside of that template, you need some thing like:

/**
 * @param  string  $template  File name. E.g: `page-templates/page-about.php`
 */
function wpse233616_get_page_template_url($template)
{
  $theme_root = is_child_theme() ? STYLESHEETPATH : TEMPLATEPATH;

  if ( !file_exists($theme_root . '/' . $template) )
    throw new \InvalidArgumentException( sprintf( __('Template %s not found.', 'textdomain'), $template ) );

  $query = new \WP_Query([
    'post_type'      => 'page',
    'meta_key'       => '_wp_page_template',
    'meta_value'     => $template,
    'no_found_rows'  => 1,
    'posts_per_page' => 1
  ]);

  if ($query->post->ID) {
    return esc_url( get_page_link($query->post) );
  } else {
    return false;
  }
}

Category: page template Time: 2016-07-30 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.126 (s). 12 q(s)