You need to determine which page uses a particular wordpress template and display its address.
Yes, there is a function is_page_template()
, but it checks the current page.
Is there any elegant way?
|
1 answer
The name of the WordPress template used on the page is stored in the metadata. This code retrieves data about all pages, extracts the _wp_page_template meta, compares with the necessary one and displays the canonical url of the found page.
function find_template () { $my_template = 'my-template.php'; $args = array ( 'numberposts' => -1, 'post_type' => 'page'); $posts = get_posts( $args ); foreach( $posts as $post ) { $template = get_post_meta($post->ID, '_wp_page_template'); if ($template === $my_template) { // сказать об этом echo wp_get_canonical_url( $post->ID ); } } }
|