WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to show only subcategories posts in category.php? (10 posts)

  1. modeman
    Member
    Posted 1 year ago #

    Hello, im building a website with products catalog, and i need help with category.php file.

    In my site are created 3 categories (FACE,HAIR,BODY) and category FACE has 3 subcategories (Cream,Cleaners,Scrubs)

    i want category.php work like that:
    I click on category named "FACE" than i only see 3 subcategories with links and i choose one of the subcategory, when i click on subcategory then i want to see all posts from that subcategory.

    Here is category.php screenshot, right now i have a code that shows selected category all subcategories with thubnail and link to subcategory, please can anybody help me with that.

    Screenshoot

    Code:

    <?php get_header(); ?>
    <div class="content">
    <div class="breadcrumbs"></p>

      <?php if (function_exists('wordpress_breadcrumbs')) wordpress_breadcrumbs(); ?>

    <p> </div>
    <div class="title"><?php single_cat_title(); ?></div>
    <div class="cat-item-main">
    <?php
    //# first of all, you need to get the category array,
    // and I did it, putting the line bellow inside the loop
    $category = get_the_category(get_the_ID());</p>
    <p>// once you have the category array, the rest of the code will work wherever you want on the page </p>
    <p>//# getting the main category of the page
    $catid=$category[0]->category_parent;
    if($catid==0){
    $catid=$category[0]->cat_ID;
    }</p>
    <p>//# now letz get the children categories of the main category
    $categories = get_categories('child_of='.intval($catid)); </p>
    <p>foreach ($categories as $category) {
    //# check if it is a real parent category with subcategories
    if ($category->parent ==$catid):
    echo
    '
    <div class="cat-item">
    <div class="image">'.get_cat_icon("echo=false&cat=".$category->cat_ID).'</div>
    <div class="text">
    term_id ).'">'.$category->cat_name.'
    </div></div>
    '</p>
    <p> ;
    //# here we go, getting the subcategories
    $subcategories= get_categories('child_of='.intval($category->cat_ID));
    foreach ($subcategories as $subcategory) {
    echo '<span class="subcategory" style="padding-left:12px">';
    echo ''.$subcategory->cat_name.'</span>';
    }</p>
    <p> endif;
    }</p>
    <p>?></p>
    <p> </div>
    </div>
    <?php get_footer(); ?>
    `

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    your posted code is somehow corrupted - please use the the http://pastebin.com/ to re-post the code - see http://codex.wordpress.org/Forum_Welcome#Posting_Code

    to get the current category ID of a category archive, use:

    get_query_var('cat')

    do you have any posts directly in the 'FACE' category?
    how many levels of child categories are you planning?

  3. modeman
    Member
    Posted 1 year ago #

    Code repost here

    Yes i have 1 post in category 'FACE' its also attached to child category 'Cream'.

    I planning only 1 level of child categories. Category -> Child category

    But i want to show posts only when i choose the child category.

    Any Suggestions?

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    http://pastebin.com/5KjhYFCH

    what is the reason for this section:

    //# here we go, getting the subcategories
            $subcategories=  get_categories('child_of='.intval($category->cat_ID));
            foreach ($subcategories as $subcategory) {
                echo '<span class="subcategory" style="padding-left:12px">';
                echo '<a href="">'.$subcategory->cat_name.'</a></span>';
            }

    because that would be sub-sub-cats ...

  5. modeman
    Member
    Posted 1 year ago #

    Yes this code part i will delete i dont need it.

  6. modeman
    Member
    Posted 1 year ago #

    In your code i have an error Parse error: syntax error, unexpected $end can you please help to fix it?

  7. alchymyth
    Forum Moderator
    Posted 1 year ago #

    corrected (and sub-sub code removed):

    http://pastebin.com/jASTNxZ5

  8. modeman
    Member
    Posted 1 year ago #

    Thanks, alchymyth for your help, code is working now.

    But i have another problem, i use plugin "Custom Fields and Types".
    I have created custom type named "Katalogas" from witch im adding posts.

    My problem is that if i create new post and attach to sub-category "CREAMS",that post shows on all sub-categories, how to separate posts to show only on selected sub-categories?

    My category.php code

  9. alchymyth
    Forum Moderator
    Posted 1 year ago #

    try to add the current category into this query:

    $query = new WP_Query( array( 'post_type' => array( 'katalogas' ) ) );

    example:

    $query = new WP_Query( array( 'post_type' => array( 'katalogas' ), 'category__in' => array( get_query_var('cat') ) ) );
  10. modeman
    Member
    Posted 1 year ago #

    Worked! Very thanks for your help you are awesome :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.