Forums

[resolved] Display Category parent (defined by name), but not children (10 posts)

  1. EntarteteMuzak
    Member
    Posted 1 year ago #

    Hi,
    I'm trying to display a category parent, but would like to leave out the category children.
    This is my code:

    <?php query_posts(
    array(
         'category_name' => 'Category',
         'posts_per_page' => 3,
         'order' => 'DESC'
    )); ?>

    Do I use some sort of exclude?

    I know how to display just the parent (if I am on the category page):

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      query_posts(array('category__in' => array($cat), 'posts_per_page' => -1));
    }
    ?>

    but not if I define the parent by name (in some cases I want to display a certain category on another page).

  2. David Gwyer
    Member
    Posted 1 year ago #

    As long as you have access to the post ID this is pretty easy using the get_the_category() function:

    http://codex.wordpress.org/Function_Reference/get_the_category

  3. EntarteteMuzak
    Member
    Posted 1 year ago #

    Yes, but I want to use the category name, not ID.

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    you should be able to get the cat ID from the cat name:

    $cat_id = get_cat_ID('Category');

    http://codex.wordpress.org/Function_Reference/get_cat_ID

  5. David Gwyer
    Member
    Posted 1 year ago #

    The get_the_category() function retrieves all the categories associated with the post ID. It returns an object including the category name, just read down the page for an example of getting the first category associated with the post.

    http://codex.wordpress.org/Function_Reference/get_the_category#Show_the_First_Category_Name_Only

    So, to get the first category name, use something like:

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>

    To get all the category names use a foreach loop like so:

    $categories = get_the_category();
    foreach ($categories as $category) {
      echo $category->cat_name;
    }

    If you use get_the_category() outside the loop you will need to manually enter the post_id as an argument.

  6. EntarteteMuzak
    Member
    Posted 1 year ago #

    Hmm. I think I may have been unclear on what I want.

    If I have this:

    <?php query_posts(
    array(
         'category_name' => 'Category',
         'posts_per_page' => 3,
         'order' => 'DESC'
    )); ?>

    How can I add something like this?
    'exclude' => 'Subcategory1, Subcategory2, Subcategory3'
    or
    'exclude' => 'children'

  7. alchymyth
    The Sweeper
    Posted 1 year ago #

    it is absolutely clear what you want:

    one way to do this is to use 'category__in' and the category id, which you can get from the category name with 'get_cat_ID()'

    nothing new here - already suggested in an earlier reply.

  8. EntarteteMuzak
    Member
    Posted 1 year ago #

    Ok.
    But, I'm sorry I still don't get it.
    Could you put something together so that I get the hang of it?

    I want to display 3 posts on the index-page from a certain category. This category has three children or subcategories. If I use my code snippet I get posts from both the parent category and the children. I want to be able to exclude the children from index-page and just display the parent category.

    I'd rather not base the solution on IDs since I need to use the theme on different sites and category IDs may differ (that is if I have to write the IDs myself and not have WordPress retrieve them for me).

  9. alchymyth
    The Sweeper
    Posted 1 year ago #

    <?php
    $ci = get_cat_ID('Category');
    query_posts(
    array(
         'category__in' => $ci,
         'posts_per_page' => 3,
         'order' => 'DESC'
    )); ?>

    http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters

  10. EntarteteMuzak
    Member
    Posted 1 year ago #

    Well, that did it!
    Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic