• Resolved travelvice

    (@travelvice)


    This topic is really drivin’ me a little nuts, not finding the answer to such a simple question…

    How do I display a category title using its ID? — any cat, using an ID# that I’ve already got. I’m outside ‘the loop’.

Viewing 5 replies - 1 through 5 (of 5 total)
  • <?php echo get_the_category_by_id(10); ?>

    Change 10 to the category ID you have.

    Thread Starter travelvice

    (@travelvice)

    bravo Kafkaesqui. (patting you on the back)

    To expand on this, I hgave a follow-up question.

    How do I get the DISPLAY TITLE of as displayed in the loop to be a variable for the category displayed?
    That is, I want only the post from category 1 appear in the loop – other category posts appear in the sidebar. Here is the code I am presently using to display the category Title in the loop, the DISPLAY TITLE is fixed:

    <div class="title">
    <h3><a href="http://www.site.com/wp-rss2.php?cat=1" title="Title">
    <img align="left" border="0" hspace="3" src="http://www.mozilla.org/images/feed-icon-28x28.png" alt="Title" title="Title" />DISPLAYED TITLE</a></h3></div>

    For the categories in the sidebar I use this:

    <h2><a href="http://www.site.com/wp-rss2.php?cat=2" title="Title 2">
    <img align="left" border="0" hspace="3" src="http://www.mozilla.org/images/feed-icon-14x14.png" alt="Title 2" title="Title 2" /></a>Title 2</h2>
    <ul>
    <?php
    global $post;
    $myposts = get_posts('numberposts=5&offset=1&category=139');
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    The problem with that code is twofold. 1) It does not display the most recent post (I cannot find any arguments to display all posts) and 2) if you click on the link, the category posts appear under the title created by the code in the loop.

    What I want, if I can babble longer, is the main page to show the posts from category 1 in the loop with the title appearing in an <h3> above the posts. Then have specified categories appear in the sidebars (not using widgets). I want the title for the categories to appear in a <h2> in the sidebar and be a link to that categories posts. When that link is clicked the full posts appear in the “maincol” with the categories’ title. Presently, if you click the link it displays the fixed title used in the code above.

    Does this make sense? I am not a full time coder … 😉

    thanks

    Why are there always follow-up questions?!?!

    ;)

    1. puzder, your query has little to do with the topic of this thread (and so starting a new one would have been prudent).

    2. A recent topic covering how to restrict posts on the main (or home) page to one category:

    http://wordpress.org/support/topic/156069

    (See my reply here for a howto.)

    3. get_posts() can only set one category for display, and cannot be used to exclude one (in this case, that being the category for your main posts). For that you’ll want to look up the info on query_posts():

    http://codex.wordpress.org/Template_Tags/query_posts

    With that you can use 'cat=-1' (in human, category equals negative (category ID) 1) as your parameter to exclude the #1 category. However! Note it’s better to create a new WP_Query() class for additional post loops, which fortunately accepts the same arguments as query_posts(). It’s referenced here:

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

    And I discuss its use in a little detail here:

    http://wordpress.org/support/topic/152651#post-679282

    I guess I did sort of ramble off topic – except that my follow-up does involve cat title and ID 🙂

    Ill give your suggestions a try when I get home from my real job.

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Category Title from ID’ is closed to new replies.