WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Listing Links to All Pages Using A Particular Template (54 posts)

  1. apax27
    Member
    Posted 2 years ago #

    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!!!

  2. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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>
  3. apax27
    Member
    Posted 2 years ago #

    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!!!!!!

  4. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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.

  5. apax27
    Member
    Posted 2 years ago #

    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.

  6. apax27
    Member
    Posted 2 years ago #

    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.

  7. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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?

  8. apax27
    Member
    Posted 2 years ago #

    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.

  9. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

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

  10. apax27
    Member
    Posted 2 years ago #

    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.

  11. apax27
    Member
    Posted 2 years ago #

    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!

  12. apax27
    Member
    Posted 2 years ago #

    <?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.

  13. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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>
  14. apax27
    Member
    Posted 2 years ago #

    <?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 ...

  15. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

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

  16. apax27
    Member
    Posted 2 years ago #

    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.

  17. apax27
    Member
    Posted 2 years ago #

    My fault- I pasted it after playing around with it after getting the error. This was your code

    <?php
    		$product_pages_args = array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'product.php'
    );
    
    $product_pages = get_pages( $product_pages_args );
    ?>
    <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>
    
    </div>
  18. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    And the error you got was Warning: Invalid argument supplied for foreach() in ...?

    What do you get from:

    var_dump( $product_pages );
  19. apax27
    Member
    Posted 2 years ago #

    Hi I am sorry for being such a newbie. Can you tell me where to put that? Never feel like you're being too specific or assume that I know what you're talking about because I most likely don't! :) thanks

  20. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    Okay, try this:

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

    That will output the content of $product_pages, and then stop processing. Report back whatever that output is.

  21. apax27
    Member
    Posted 2 years ago #

    array(0) { }

  22. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    So, that's the problem: WordPress isn't finding any pages that are using the template file product.php.

    Are you sure this is the correct template file name?

  23. apax27
    Member
    Posted 2 years ago #

    That is so weird- yes I am 100% positive. I double checked even to see if there were any other template pages with the same name and there is only one with the template name Product and that is product.php. There are over 300 pages with this template. I even tried the same code with other template files to check it and they didn't work either

  24. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    Can you provide a live link to the site/page in question?

  25. apax27
    Member
    Posted 2 years ago #

    Sorry for dissapearing.

    I think I have figured out a bug w/ WordPress.

    I have 2 templates, product.php and product-category.php

    I also have business.php and business-category.php.

    Those 4 templates do not work with the above code. But other templates will work, such as homepage.php or location.php I think because both templates have the word "product" in them, it doesn't know which one to return. I am going to try changing the name of the template and will keep you updated.

  26. apax27
    Member
    Posted 2 years ago #

    Ignore my last reply

    I finally figured out why it wasn't working. But still frustrated because I can't figure out the fix.

    All of the pages with the template product.php have parent pages. So I changed one of the pages to "no parent" and lo and behold- the array returned that page title in its result.

    So this only works on pages that have no parent! The problem is- the pages in this template need to have parents for the structure of site.

    I feel like I am so close to figuring this out yet so far!!!

    I appreciate you taking the time to help me. Sorry for disappearing- I had been spending too much time on this site and neglecting my other clients. You are a good person for volunteering your time to help clueless people like me :)

  27. apax27
    Member
    Posted 2 years ago #

    Still open-ended on this. Anyone out there know how to make this array work on child pages???

  28. SpaceDogDeveloper
    Member
    Posted 2 years ago #

    I don't think get_pages is going to do what you want it to here. Try running a new wp_query for your custom fields and then create a new loop with that query using the_title(), the_permalink(), the_content(), etc.

    http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

  29. apax27
    Member
    Posted 2 years ago #

    Thank you! I am determined/desperate to figure this out!! Too many of these posts get left unresolved and hopefully this will help someone in the future as well.

    So I tried:

    $query = new WP_Query( array( 'meta_key' => 'wp_page_template', 'meta_value' => 'product.php'

    based on the instructions in that link and it did not return any results. Was I using it wrong?

  30. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    So I tried:

    $query = new WP_Query( array( 'meta_key' => 'wp_page_template', 'meta_value' => 'product.php'

    based on the instructions in that link and it did not return any results. Was I using it wrong?

    Partly. This:

    'wp_page_template'

    ...needs to be this:

    '_wp_page_template'

    Also, this query assumes that your custom page template file name is product.php. If it isn't, you'll need to modify 'meta_value' accordingly.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.