Support » Themes and Templates » Show only X Category Posts on Page

  • Resolved CrapCat

    (@crapcat)


    Hi everyone

    Please bear with me, as I am VERY new to PHP and the skills I do have are so rusty as to be non-existent.

    I’ve been using MovableType since it came into existance, but WordPress is something that I’ve only been dabbling with for a year, and only just gained the confidence to do design work. I just complete kittylovesjapan.com based on the Elegant Grunge theme.

    I now am trying to convert another of my website from MT to wordpress, but it needs to look the same.

    I have a bunch of Pages that only show the category that matches that page. The Index Page shows all categories EXCEPT Contact.

    So on Page “In the Press” I want to show ONLY posts from Category “In the Press”.

    I have 6 pages in all that I need to do this with. I want to use Pages, as I don’t want the person who adds most content to do anything except access Categories – she tends to break things otherwise.

    Be aware of my bad PHP skills, and that I do need more than
    DO STUFF in any code help you wish to give me ๐Ÿ™‚

    Natalie

Viewing 15 replies - 1 through 15 (of 20 total)
  • I had an idea!

    Is there a way to

    get <?php wp_list_categories( array('show_count=0&amp;title_li=') ); ?>

    To display the same as <?php wp_page_menu( array( 'show_home' => 1 ) ); ?>

    ?

    I tried change page to category and categories, but they are invalid.

    Just having my categories display like the menu array actually solves my problem if someone can tell me how to do it?

    Hi

    going back to your first post, I feel you had the right idea there. What you are looking for is called query_posts
    http://codex.wordpress.org/Template_Tags/query_posts

    You want to use pages (I agree that is the best way to go), so you will modify your page.php template file like this

    code like this goes before the WordPress loop
    (I am making up page names and cat ID’s – we are associating a cat ID with a page)

    <?php
    $catID = 0;
    if (is_page('contribute')) {
      $catID=6;
    } elseif (is_page('about')) {
      $catID=9;
    }
    
    if ($catID) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts("cat=$catID&amp;paged=$paged");
    } ?>

    Now the WP loop (already in page.php) starts:

    <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>

    What we are doing is associating a category with a page – on that page only posts from that category will display

    Thanks stvwlf, but I dont get this bit at all :

    if ($catID) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts("cat=$catID&amp;paged=$paged");
    } ?>

    am I meant to change something in there to get it to work? catId?

    If my page is called “In the Press” or “New in Store” do I have to put hard spaces?

    Because I’ve created these two pages, and linked their catId as you example above, but it shows no posts, even though I have written a post for each category for testing purposes.

    This is on my local test machine, so I can’t show you the actual site.

    <?php
    $catID = 0;
    if (is_page('New in Store')) {
      $catID=3;
    } elseif (is_page('In the Press')) {
      $catID=6;
    }
    
    if ($catID) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts("cat=$catID&amp;paged=$paged");
    } ?>
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<div class="post" id="post-<?php the_ID(); ?>">

    for is_page() use the page-slug not the page title – you find that just below the title in the editor – most likely new-in-store and in-the-press
    if (is_page('new-in-store')) {

    make sure that where its says “ampersand amp;” that you have only a single ampersand not amp; the forum software messes that up in code segments

    you have the paged code correct. the first line about $paged is to keep the paging working correctly when there are more posts than fit on one page. don’t change those two lines

    Ok my code is now:

    <?php
    $catID = 0;
    if (is_page('New in Store')) {
      $catID=3;
    } elseif (is_page('In the Press')) {
      $catID=6;
    } elseif (is_page('Contact')) {
      $catID=7;
    }
    
    if ($catID) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts("cat=$catID&amp;paged=$paged");
    } ?>

    They all work! Except for New In Store

    I checked the catID is correct (yes, it is 3) and that there is a post in that category, (yes, it is called “This is a post in the NIS category”) with the category ticked.

    But it does not show!

    is the post published?

    oh I fixed it! It is cAsE sEnSiTiVe!!!

    You rock, thanks so much!

    You can do this without PHP code.

    Click on the category.

    Grab the URL.

    Install the “Page Links To” plug-in, and link to the Categories URL.

    Hello All,

    This works for me.. HOWEVER

    Pagination does not work!!!!

    ‘<div class=”navigation”>
    <?php if(function_exists(‘wp_pagenavi’)) { wp_pagenavi(); } ?>
    </div><!– Closes navigation–>’

    when i click on page 2 of posts it does not show the rest of the posts but only results on first page.. any parameter i need to add?

    thank you

    here is the code

    ‘——————————————————

    <?php
    /*
    Template Name: page_restaurants
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="main">
    
    <div id="contentwrapper">
    
    <?php
    global $more;
    $more = 0;
    ?>
    
    <?php query_posts('category_name=restaurants'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
    
    <div class="topPost">
    <?php edit_post_link('Edit', ' | ', ''); ?></span></p>
    <h2 class="topTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
      <p class="topMeta">Location ยป <?php the_category(', '); ?>
      <div class="topContent"><?php the_content('(More Info...)'); ?></div>
    
      <div class="cleared"></div>
    </div> <!-- Closes topPost --><br />
    
    <?php endwhile; ?>
    
    <div class="cleared"></div> 
    
    <?php endif; ?>
    
    <div class="navigation">
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    </div><!-- Closes navigation-->
    
    <div class="cleared"></div>
    
    </div> <!-- Closes contentwrapper-->
    
    <?php get_sidebar(); ?>
    
    <div class="cleared"></div>
    </div><!-- Closes Main -->
    
    <div class="cleared"></div>
    <?php get_footer(); ?>

    please help I am tryng this and is not working. I kepp gettin ghthese post to show up on my home page not in their own category.

    Many thanks! This code is working exactly as it should.
    However, I’m trying to use it on a page with Query Posts plugin/widget so I can display a particular category in the sidebar, but the implementation of this code is no longer showing the widget in the sidebar.
    I think I’m looking for code that will show a category on the page, and also allow other categories to be displayed in said page’s sidebar.
    Any advice or pointers?

    I was having a lot of trouble with the code posted in the prior posts, but found my answer in the WordPress documentation on Pages. Take a look here.

    This is a great function. I’ve got it working properly on my site.

    Is there a way to insert these posts below the page’s content instead of it just completely overiding the page’s content?

    Here is the code on my page.php file

    <div class="content">
    
    <?php
    $catID = 0;
    if (is_page('Sessions')) {
      $catID=177;
    } 
    
    if ($catID) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts("cat=$catID&paged=$paged");
    } ?>
    
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    Need multiple loops–see if this example helps understand:
    http://wordpress.pastebin.ca/1653262

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Show only X Category Posts on Page’ is closed to new replies.