Support » Themes and Templates » Listing Links to All Pages Using A Particular Template

  • Resolved apax27

    (@apax27)


    I don’t know why topics that are unresolved always get closed in this forum. I have been trying to find a solution to this post

    So I want a page that shows links to all pages with a particular template, sorted by date. (they already have parent pages so it can’t be anything that utilizes parent pages)

    any suggestions are greatly appreciated!!!

Viewing 15 replies - 1 through 15 (of 53 total)
  • FYI, posts without activity auto-close after a certain amount of time, and all posts, regardless of activity, also auto-close after a certain amount of time.

    Anyway, to answer your question: yes, you can do that. 🙂

    Try using get_pages() with the meta_key argument.

    WordPress stores the page template as meta data, as the wp_page_template meta key. The value is either 'default' (if no page template is assigned), or the name of the custom page template PHP file, e.g. 'template-archives.php' (if you’ve created an “Archives” custom page template).

    So, for example:

    $archive_pages_args = array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'template-archives.php'
    );
    
    $archive_pages = get_pages( $archive_pages_args );

    This will give you an array of pages that use the specified template; so all you need to do is output them:

    <ul>
    <?php
    foreach ( $archive_pages as $archive_page ) {
        echo '<li><a href="' . get_permalink( $archive_page->ID ) . '">' . $archive_page->post_title  . '</a></li>';
    }
    ?>
    <?ul>
    Thread Starter apax27

    (@apax27)

    Thank you for such a quick reply! I really appreciate your help. I apologize- I’m a little slow with the php. Do I use both bits of code, the first one followed by the second? I can’t seem to get it to work.

    Thank you!!!!!!

    No problem.

    Making it work properly in your specific use case would require considerably more information.

    Have a read of the get_pages() Codex entry, for detailed instructions on the use of this function.

    Thread Starter apax27

    (@apax27)

    Maybe I should explain what I’m trying to do. I have a website that lists a bunch of categories and in each category is different products. (for example automotive, clothing, media, etc.) The categories are my parent pages. I already have a list of the child pages for each category.

    Now I want to create a page that lists ALL products in order of the date they were posted. but I do not want to list the categories on this page. There might be a simpler way to do this.

    Each product has a template of product.php.

    Thread Starter apax27

    (@apax27)

    Thank you I will read that article as well. I’m still learning wordpress. As a designer the side of my brain that makes websites look “pretty” works much better than the side that understands the programming end! Thank you for your patience.

    That sounds like a fundamentally different issue than the one in your OP.

    Are you talking about static pages, or single blog post pages, or archive index pages?

    What page is outputting your list of products?

    Thread Starter apax27

    (@apax27)

    These are pages, not posts.

    All product pages have the same template. So I thought there might be a way to compile a list of links to pages that all have the same template. Right now I am able to compile a list of products that have the same parent (the category) So I thought maybe if there was a way to make this list just based on the template, and then sort in date order.

    It should be pretty simple but I can’t get the code to work. I know how to compile a list of all pages and sort that list, but I only want to show those that have the “Product” template, not pages like “Home” or “About us” or any other pages on the site.

    What code can’t you get to work? The example code I posted earlier?

    Thread Starter apax27

    (@apax27)

    Any code- I have tried playing with it for hours and days and I can’t get anything to work.

    Now I have given up and I am looking for plugins, although I am not finding any that will list a sitemap of all pages with the same template.

    Thread Starter apax27

    (@apax27)

    I cannot find a plugin or an answer to what I am looking for and I am getting very frustrated 🙁 I feel like this should be so simple.

    Let me try another explanation

    Right now I have a page this page, which I call the product category page

    <?php
    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    $count = 0;
    foreach($pages as $page)
    {
    $content = $page->post_content;
    ?>
    <div class="category-listing">
    <table>
    <tr>
    <td>
    <a href="<?php echo get_page_link($page->ID) ?>"><?php echo $content ?>
    </td>
    <td>
    <h4><?php echo $page->post_title ?></h4>
    <p><?php echo get_post_meta($page->ID,'product_number',true); ?><br />
    	<strong>List Price:</strong><?php echo get_post_meta($page->ID,'list_price',true); ?><br />
    <strong>Our Price:</strong><?php echo get_post_meta($page->ID,'discount_price',true); ?></p>
    <p><a href="<?php echo get_page_link($page->ID) ?>">More Information</a></p>
    </td>
    </tr>
    </table>

    And here is an example of this page
    HERE

    As you can see, these products are located in the Electronics category.

    Electronics is the Parent page, and all of these product pages that are listed are the child pages.

    I basically want to make a completely separate page with ALL the products in all categories. Every product is given the template “Product” or “product.php”

    That is why I wanted to list only the pages with the same template.

    There has to be an easy way to do this. I am ripping my hair out trying to figure this out. If anyone has any input, please help!

    THANKS! I am extremely grateful for ALL input!

    Thread Starter apax27

    (@apax27)

    <?php $args = array(
    	'meta_key' => '_wp_page_template',
    	'meta_value' => 'product.php'
    );
    foreach($args as $page)
    {
    $content = $page->post_content;
    ?>

    This does not work. But I feel like I might be on the right track.

    This does not work. But I feel like I might be on the right track.

    Try the code I posted above (modified here using your template file name):

    $product_pages_args = array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'product.php'
    );
    
    $product_pages = get_pages( $product_pages_args );

    Then output it, e.g.:

    <ul>
    <?php
    foreach ( $product_pages as $product_page ) {
        echo '<li><a href="' . get_permalink( $product_page->ID ) . '">' . $product_page->post_title  . '</a></li>';
    }
    ?>
    <?ul>
    Thread Starter apax27

    (@apax27)

    <?php $args = array(
    	'meta_key' => '_wp_page_template',
    	'meta_value' => 'product.php'
    );
    foreach($args as $page)
    {
    $content = $page->post_content;
    ?>
    
    <div class="category-listing">
    <ul>
    <?php
    foreach ( $product_pages as $product_page ) {
        echo '<li><a href="' . get_permalink( $product_page->ID ) . '">' . $product_page->post_title  . '</a></li>';
    }
    ?>
    </ul>

    Something seems wrong. I am getting an error:
    Warning: Invalid argument supplied for foreach() in …

    What happens if you try using the code I posted, instead of your own?

    Thread Starter apax27

    (@apax27)

    I’m confused- that is the code you posted. I copied and pasted. The only thing I changed was the <?ul> to because I assumed that was a typo.

Viewing 15 replies - 1 through 15 (of 53 total)
  • The topic ‘Listing Links to All Pages Using A Particular Template’ is closed to new replies.