WordPress.org

Ready to get started?Download WordPress

Forums

What is the "first category"? (3 posts)

  1. kath
    Member
    Posted 5 years ago #

    Hi everyone,

    This is going to seem like a stupid question but I can't find documentation that is specific enough for me. In short form, what I want to know is, what is the "first category" for a post that has more than one category? Is it the first category that was assigned to that post? Is it the category with the lowest ID assigned to the post? If one adds many categories to a post before clicking "save", which is the "first category"?

    I need to know because it could potentially make setting up a newsletter I'm working on a lot easier. The newsletter is released in issues. I think it makes sense to use categories for my organization into categories, rather than tags -- so "Issue 12" would be a category, as would "in every issue" and "featured" whereas tags would be based on the content of a given article, like "job searching" or "cupcakes".

    So I need to assign articles [posts] to multiple categories. But I also want to have the navigation bars etc on single post pages link other articles in the same issue, so I'll need to get the issue category that a given post is assigned to. If I can do this by assigning the issue category before assigning any other category, that would be simple. Another possible way (I guess) is trying to return the category by name which includes the string "issue", get the ID of this category, and find all the other articles in that category. But this sounds like it would be less possible than just adding categories in a given order.

    I hope that makes sense. Using wordpress 2.6.2.

  2. MichaelH
    Member
    Posted 5 years ago #

    When using something like the WordPress Default theme, when posts are displayed, a list of the Categories assigned each post is also displayed. That list of Categories is displayed using the template tag, the_catetgory, which displays all the categories assigned that post, in category name order (hoping that addresses your 'first' category question).

    So if a post is in a category called "Issue 12", then when a user clicks on that category link, they will be taken to a 'category archive' page showing all posts that are assigned the Issue 12 category.

    Resources:
    Category Templates
    Stepping Into Templates
    Stepping Into Template Tags
    Template Hierarchy

  3. kath
    Member
    Posted 5 years ago #

    That's not quite what I'm curious about, no, but thanks. There's also a way to grab just the "first category" -- let me find where I found this. Here we go:

    Template Tags / get the category.

    In that page it says:

    Show the First Category Name Only

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

    (Echoes the first array ([0]) of $category.)

    ... but it does not specify how (by what mechanism -- order of add, alphabetically etc) the first category is chosen. I just want to know in a specific sense what "
    (Echoes the first array ([0]) of $category.) " means, IE, are categories stored in the order they are saved to post in the database, or what? I think the normal category listings list alphabetically because they are set to list that way in the default theme. In the barebones theme I'm working with, the first category listed is both (by coincidence) the first category assigened to the post and the first category by id. The code used is Posted in <?php the_category(', ') ?> and it's definitely not listing categories alphabetically.

    That was why I put "first category" in quotes -- because I want to get a specific category that could be termed the "first category" but I wanted to know how wordpress understands that term.

Topic Closed

This topic has been closed to new replies.

About this Topic