• I would like to set up a page template that allows me to build the page based on the posts of a certain category. I know I can used the cat=# setup to do this, but I want something that automates this a bit.

    My idea is to have the page load, look at the title of the page and if there is a category with that same name, show the posts of that category.

    An example:
    Page is created called “Dogs”
    I would want the Dogs page to look for a category named dogs and if it is there show the posts. If not, then some other default text.

    The challenge is that I don’t want to have to create a new page template for each category. I want the page to get the title, and if category=’title’, show posts.

    I hope I am describing this in a way that makes sense.
    Any ideas?

Viewing 15 replies - 1 through 15 (of 17 total)
  • So sort of like related posts?

    Then in the default page template, you could setup a queryposts

    Which displays posts where ‘cat’=$pagetitle

    else do default text if no posts found.

    yes, makes sense.

    Uhm, I actually did this once, but I can’t find it anymore.

    Anyhow, you know how to create a template, probably?
    (requires some comment stuff in the beginning of the page)

    then use something like

    <?php query_posts('category_name='.the_title()); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<div>
    	<h1><?php the_title(); ?></h1>
    	<p><?php the_content(); ?></p>
    	</div>
    <?php endwhile; else: ?>no match<?php endif; ?>

    This is not tested … I might have it wrong, the category_name=’.the_title()) bit.

    Aren’t the Category_Templates doing exactly what has been asked in OP? Why the need to mess with Pages?

    if u want some thing like : More from this category
    go to this tut :
    http://www.darrenhoyt.com/2008/06/11/displaying-related-category-and-author-content-in-wordpress/

    Probably so that they can use pages in the navigation and have some menu items show post categories.

    Wouldn’t be easier to change the navigation instead of messing around with Pages and forcing WP to do “unnatural” things?

    If you’re doing it for yourself, it probably is.
    But if you want to hand over the site to a third party, who have no idea how to change php files and ftp them to a server, it’s simpler to tell them “create a category, then create a new page with the same name, use template X, and use the menu position number to set the position”.

    Well, I was just trying to create another template like that and realized my code doesn’t work 🙂

    must be

    <?php query_posts('category_name='.get_the_title()); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<div>
    	<h1><?php the_title(); ?></h1>
    	<p><?php the_content(); ?></p>
    	</div>
    <?php endwhile; else: ?>no match<?php endif; ?>

    Notice: it’s not the_title() but get_the_title()!

    Thread Starter Ed N.

    (@enailor)

    Moshu,
    The reason for this is simple. I am using WP as a CMS and want to allow my clients to create a new page that feeds off of articles in a selected category. Sure I could create a category-name.php file, but that requires me to manually set this up each time.

    My idea is that they have a page for “Dogs” and can add info into the WYSIWYG editor for that page that will populate the top of the page. However, below that will be the latest articles (posts) that relate to that category.

    Actual application is for a real estate site. Lets say I have a “Local” page and under that category I allow my clients to add a new category for a city. I would want them to be able to create a page with the same title as the category that loads related articles yet still post the page’s post info so that they can give a general description of the city. Then any posts they make to that category would populate only on that city’s page below the page’s post data.

    Does this make sense?
    Ed

    Thread Starter Ed N.

    (@enailor)

    Ok. So using the Flutter plugin, I am able to create a custom field for my clients to enter the category number. I am also able to create a custom write panel in which I have hidden custom fields (just for simplity sake) and named it “Category Page”.

    In my page code, I have created a WP_query that pulls the cat number, and runs a loop based on that cat number. If the cat number is blank, it does nothing.

    Example of use:
    Lets say you run a website about cars. You have pages about Ford, Chevy and Dodge.

    So now you have a page for Ford cars. The content portion of the page has a quick summary about Ford, the types of cars, ect. However, as you write posts about Ford products, news, ect, this page will now begin to become much more dynamic.

    In doing it this way, my client can have a “template” page for each category without having me create a hard coded descriptive section at the top… and I like that!

    Anyway… this is what I am doing and why.

    enailor, can u post your codes here? That would be really helpful..

    Any chance we can get a copy of that query you wrote enailor? I’m running into the exact same issue 🙁

    Here is the code I used to get this to work:

    <?php $catname = wp_title(''); ?>
    <?php query_posts("category_name=$catname&showposts=3"); ?>
    <?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
    foreach ($posts as $post) : start_wp(); ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    It automatically pulls the name of the page and then displays the first 3 posts from the category that has the same name. Hope this helps.

    Sorry just realized I had one piece missing. Make sure to add “false” to the wp_title(”, false) to make sure the variable is output as a string and not echoed. See below:

    <?php $catname = wp_title('', false); ?>
    <?php query_posts("category_name=$catname&showposts=3"); ?>
    <?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
    foreach ($posts as $post) : start_wp(); ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    thank you great article
    mysite:mp3canavari

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Creating Page Based on Category’ is closed to new replies.