WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using Page name to return a Category (13 posts)

  1. gilgemesh
    Member
    Posted 6 years ago #

    I understand that Pages are static. I also know that I can use Category Templates to style the Pages in inumerable ways. However my PHP skills are not honed well enough to be able to figure out the best mothod to do the following:

    Current setup:
    Each main page uses a template to show a list of the latest posts in it's subpage categories. Each subpage only shows posts in the category associated with that post. For instance, reviews would include movies and books, while books only includes books. Should be simple, right.

    Except in order to create the pages this way I have 20 php files, and growing. I was hoping that there was a simpler way I could use a call for the page name, returning Books for instance, to call a category with the same name, Books.

  2. moshu
    Member
    Posted 6 years ago #

    I also know that I can use Category Templates to style the Pages in inumerable ways.

    Wrong. You use Category_Templates to style the category listings (or archives). They have nothing to do with Pages.
    And showing only posts from a certain category - it is done out of the box by WP: when you click a category's name.

    So, I am confused about what you tried to describe above.

  3. gilgemesh
    Member
    Posted 6 years ago #

    OK, perhaps I misspoke when I mentioned Category Templates. Here is the full code that I am using to create a page that returns only a single category `<?php
    /*
    Template Name: Booze
    */
    ?>
    <?php get_header(); ?>

    <div id="content">

    <?php query_posts('cat=12'); ?>

    <?php is_tag(); ?>
    <?php if (have_posts()) : ?>

    <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
    <?php /* If this is a category archive */ if (is_category()) { ?>
    <h2 class="pagetitle">Our Latest Alcohol Reviews</h2>

    <?php } ?>

    <?php while (have_posts()) : the_post(); ?>
    <div class="post">
    <h4 id="post-<?php the_ID(); ?>">" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h4>
    <small>By <?php the_author_posts_link('namefl'); ?> • <?php the_time('M jS, Y') ?> • Category: <?php the_category(', ') ?></small>
    <div class="entry">
    <?php the_excerpt() ?>
    </div>

    <!--<p class="postmetadata"><?php the_tags('Tags: ', ', ', ''); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>-->

    <hr />
    </div>

    <?php endwhile; ?>

    <div class="navigation">
    <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
    </div>

    <?php else : ?>

    <h2 class="center">Not Found</h2>
    <?php include (TEMPLATEPATH . '/searchform.php'); ?>

    <?php endif; ?>

    </div>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>`

    The issue I have is that I need to create one of those for every page. What I want to do is take this code <?php query_posts('cat=12'); ?> and instead of Querying cat=12 I want to query cat = page_name somehow and use the page name to pull a category of that same name. That way I only need 1 template for the whole site.

    Hopefully that is a better explaination.

  4. moshu
    Member
    Posted 6 years ago #

    No. Weong approach. Forget the Pages.
    Use a category template (category.php*) and you don't need tricks in the Loop.
    Why that doesn't work for you?

    *save your index.php or archive.php as category.php

  5. gilgemesh
    Member
    Posted 6 years ago #

    Why that doesn't work for you?

    I am glad you ask that. The header menu of the blog is created with a javascript that pulls from the Pages and subpages lists. So, while it would be great to use a category template, I would still need to create a new one for every static Page. I am hoping that it will be easier to figure out how to use the Page_name to call the category than it will be to try to figure out a way to re-write the whole JS menu to call a set list of categories.

    Oh, Also, I have a category template for the larger growing list of categories. We just want our main navigation list to remain set. And I thought there might be a way to use one PHP file to call the category for each of the pages instead of needing a new one for each page.

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    It seems to me that you're taking this all backwards.

    To explain, you'd rather use Pages instead of the existing, built in category mechanisms, and go to a whole extra lot of effort and trouble... simply to avoid changing some minor javascript that builds what is basically just a menu?

    Get your priorities in order here. More than anything else, you do maintenance of the content of the site. If you're having to edit the theme and create new Pages and such whenever you make new posts and catgories and such, then you've created a whole lot of new work for yourself just to avoid screwing around with some PHP and javascript one-time? Doesn't make much sense.

    Use the templates for what they were designed for. If you want to show a list of posts from a specific category, then you should absolutely be using a Category Template, not a Page Template. If this doesn't fit your theme's menu system, then change that menu system in a way such that it is doing the right thing.

    I am hoping that it will be easier to figure out how to use the Page_name to call the category than it will be to try to figure out a way to re-write the whole JS menu to call a set list of categories.

    No, it would absolutely not be easier. It would be way simpler to simply make the menu pull the list of categories instead of the list of pages. It might even be as simple as replacing a call to wp_list_pages with a similar call to wp_list_categories.

    Don't make workarounds for problems. Attack the problems directly.

  7. moshu
    Member
    Posted 6 years ago #

    The header menu of the blog is created with a javascript that pulls from the Pages and subpages lists.

    So, what? Replace the wp_list_pages with
    <?php wp_list_categories('title_li='); ?>
    and you are done.
    Tested with the original mimbo2.2!

    EDIT. Otto was faster - (had a phone call in the meantime)

  8. gilgemesh
    Member
    Posted 6 years ago #

    How do I get the sub_categories to become drop downs from the menu, like the sub-Pages do in the mimbo2.2 theme, when I use that code?

  9. moshu
    Member
    Posted 6 years ago #

    Did you try it? - No, you didn't.
    I just posted above that I have tested it, and it works. So, don't ever reply to me before testing what was suggested!

  10. gilgemesh
    Member
    Posted 6 years ago #

    Actually, don't get pompous, I DID TRY IT. AND, I have continued to TRY it in as many different ways as I know how. It returns JUST the main CATEGORY. IT DOES NOT RETURN THE SUB CATEGORIES.

    So, before you get all yelly and mean, PLEASE do NOT assume ANYTHING.

    AND I REALLY RESENT YOUR TONE.

  11. gilgemesh
    Member
    Posted 6 years ago #

    I realize it is possible you are looking at the actual blog site. I use a test server.

    And I see Otto's post. I am TRYING. But it still DOES NOT RETURN THE SUBPAGE.

  12. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Try this:
    <?php wp_list_categories('hierarchical=0&title_li='); ?>

  13. gilgemesh
    Member
    Posted 6 years ago #

    Perfect. Thank you Otto. One other question. If I want to use just specific categories, would I just make it:

    `<?php wp_list_categories('hierarchical=0&include=2,6,8&title_li='); ?>

    And thank you as well to Moshu. I am sorry. I must be a bit thick skulled when it comes to this.

Topic Closed

This topic has been closed to new replies.

About this Topic