WordPress.org

Ready to get started?Download WordPress

Forums

Only show posts of current category? (6 posts)

  1. CodeyMonkey
    Member
    Posted 2 years ago #

    Hi,

    How do I show posts posted within the current category page i'm viewing? (Using category.php)

    e.g. current category = /category/portfolio/

    And I only want to show posts that are posted within this category?

    I don't want it to show posts from other categories like "blog" or "updates" etc for exampl, only those in "portfolio".

    Important: I don't want to have to hardcore the term/category "portfolio" in the query, it should be using the "current page" as the idtenifier.

    I've looked everywhere but can't find a solution!

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    not sure what your question is, as a category archive would only show posts of that one category (and its child categories)

    unless your theme has no archive.php and/or category.php and is using index.php (which might be edited with a custom query) to show the categories. or has a custom query in category.php which messes with the original query_string.

    you could paste the full code of category.php into a http://pastebin.com/ and post the link to it here, for someone to have a look at it.

  3. CodeyMonkey
    Member
    Posted 2 years ago #

    Hi Alchymyth,

    I'm pretty new to wordpress, so I might have to use archive.php instead of category_portfolio.php, but i'll see what you think.

    I'm building a Portfolio website with just

    Home/Portfolio/Blog/Contact

    Home = index.php
    Portfolio = category_portfolio.php
    Blog = category_blog.php
    Contact = (I havn't got this far yet, might use "Pages" and a plugin)

    /blog/ & /portfolio/sub-categories - will need different looking archives.

    Portfolio has sub categories like "Web Design/Illustartion/Print etc" and if a user goes to /category/portolio it would list all posts posted within those child categories...

    But currently it shows posts from both "Portfolio" and "Blog" I don't know how to differentiate, what do you suggest?

    I can currently list the "child Categories" only using:

    <?php
    
    $args=array(							'orderby' => 'name',
    'order' => 'ASC'
    );
    $categories=  get_categories('child_of='.get_query_var('cat'));
    foreach($categories as $category) {
    echo '<li class="segment-' . $category->cat_ID.'"><a href="#" class="' . $category->cat_ID.'">' . $category->name.'</a></li>';
    }
    ?>

    But... I don't know how to pull the posts from these child categories only.

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    were you thinking of using a category template:
    http://codex.wordpress.org/Category_Templates

    instead 'category_portfolio.php' you might need to name the file 'category-portfolio.php'

    this template would then automatically get called to display the 'portofolio' category archive.

    there is also the possibility to make the child categories use the parent category template;
    for instance http://brassblogs.com/blog/making-child-categories-recognize-parent-displays
    or web search 'make child category use parent category template'

  5. CodeyMonkey
    Member
    Posted 2 years ago #

    Hi,

    Sorry I meant to use a "-" instead of an "_"... So if I was to go the category route...

    How would I show only the current categories posts?

    e.g. posts of /category/portfolio/

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    How would I show only the current categories posts?

    in teh category template, you could do 'nothing' and simply start the loop with the usual:

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

    this would show posts of the current category and of its child categories.

    to show only post of the current category, add a line like this before the start of the loop:

    <?php global $wp_query;
    $args = array_merge( $wp_query->query, array( 'category__in' => array(get_query_var('cat')) ) );
    query_posts( $args );
    ?>

    (untested)

Topic Closed

This topic has been closed to new replies.

About this Topic