WordPress.org

Ready to get started?Download WordPress

Forums

Creating Page Based on Category (18 posts)

  1. enailor
    Member
    Posted 5 years ago #

    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?

  2. xdesi
    Member
    Posted 5 years ago #

    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.

  3. mores
    Member
    Posted 5 years ago #

    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.

  4. moshu
    Member
    Posted 5 years ago #

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

  5. taghaboy
    Member
    Posted 5 years ago #

    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/

  6. mores
    Member
    Posted 5 years ago #

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

  7. moshu
    Member
    Posted 5 years ago #

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

  8. mores
    Member
    Posted 5 years ago #

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

  9. mores
    Member
    Posted 5 years ago #

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

  10. enailor
    Member
    Posted 5 years ago #

    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

  11. enailor
    Member
    Posted 5 years ago #

    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.

  12. mmubashar
    Member
    Posted 5 years ago #

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

  13. mreider
    Member
    Posted 4 years ago #

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

  14. funkboybilly
    Member
    Posted 4 years ago #

    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.

  15. funkboybilly
    Member
    Posted 4 years ago #

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

  16. mcbozo
    Member
    Posted 4 years ago #

    thank you great article
    mysite:mp3canavari

  17. saineys
    Member
    Posted 4 years ago #

    funkboybilly::

    Hai thanks funkboybilly...
    Actually i am trying to get some code..from past 3 days ...
    while I got it here by ur comment...and I tried to use this for my one...

    it works perfectly thank u very much...

    Thanks
    anil.
    saineys@gmail.com

  18. twexpert
    Member
    Posted 4 years ago #

    I added on to funkboybilly code so that it looks similar to the category page whihc includes title link, date, tags, etc.
    In the page template paste it under the
    <h1><?php the_title(); ?></h1><br /> that is already there.

    Here is the code:

    <!-- CODE TO SHOW POSTS -->
    <?php $catname = wp_title('', false); ?>
    <?php query_posts("category_name=$catname&showposts=10"); ?>
    <?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
    foreach ($posts as $post) : start_wp(); ?>
    <h1><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    
    				<div class="dateleft">
    					<p><span class="time"><?php the_time('F j, Y'); ?></span> <?php _e("by", 'studiopress'); ?> <?php the_author_posts_link(); ?> &nbsp;<?php edit_post_link(__('(Edit)', 'studiopress'), '', ''); ?> <br /> <?php _e("Filed under", 'studiopress'); ?> <?php the_category(', ') ?></p>
    				</div>
    
    				<div class="dateright">
    					<p><span class="icomment"><a rel="nofollow" href="<?php the_permalink(); ?>#comments"><?php comments_number(__('Leave a Comment', 'studiopress'), __('1 Comment', 'studiopress'), __('% Comments', 'studiopress')); ?></a></span></p>
    				</div>
    
    			<div class="clear"></div>
    <?php the_excerpt(__('Read more', 'studiopress'));?>
    			<div class="clear"></div>
    
    			<div class="postmeta2">
    <p><span class="tags">Tags: <?php the_tags('') ?></span></p>
    			</div>
    <?php endforeach; ?>
    
    <!-- END-->

    Then remove the line of code below this:
    <?php the_excerpt(__('Read more', 'studiopress'));?> that was previously there so you dont have you content displaying twice.

Topic Closed

This topic has been closed to new replies.

About this Topic