Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Conditions: Page have template, for example: /* Template name: News */

Explaining:

We have many pages, and one page with assigned template (/* Template name: News */).

I want on main page (index.php) retrieve page(News) url and title.

share|improve this question
1  
I think you're going to have to clarify your question a little. It's very cryptic. –  chrisguitarguy May 15 '12 at 14:41
add comment

1 Answer 1

up vote 0 down vote accepted

Let me try to summarize what you want and answer:

I want on main page (index.php) retrieve page(News) url and title.

So you have a "News" page that's using a specific template called "News" and you want to grab that page's ID, URL, and title to display on the front page, correct?

Get a Page by Template

There is no built-in way to grab a page based on the template it's using. Instead, you have to search for all pages based on a hidden meta value that matches your page template. So, let's say your News template is named news-template.php in the theme, then you'd use the following code to get the first page that use that template:

$news_pages = get_pages( array(
    'meta_key'   => '_wp_page_template',
    'meta_value' => 'news-template.php',
    'number'     => 1
) );

$news_page = $news_pages[0];

Remember, since get_pages() returns an array, you have to manually select the first element of that array. Now this $news_page object contains the content of your News page.

Displaying Page Information

Now, inside your index.php page you can display this page's information by referencing the $news_page object.

  • Page ID: $news_page->ID
  • Page URL: get_page_link( $news_page->ID )
  • Page Title: $news_page->post_title
share|improve this answer
    
Thaanks :) You helped me ;) –  Vital May 15 '12 at 15:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.