WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Category Descriptions usage question (36 posts)

  1. jamesisin
    Member
    Posted 5 years ago #

    For every category there is a Description text field. Innitially I was under the impression that this description was used when a category archive page was accessed, but that has proven untrue. In fact, I am totally unclear as to the actual purpose of the description field as it seems its only function is to advise me, the category creator, what the category is about.

    So my question is: is it possible to use that Description text as a introduction or sub-header to the category archive page?

    Here is one of my category archive pages for reference:

    http://www.soundunreason.com/InkWell/?cat=50

    As you can see, this page begins with the text "Archive for the ‘Darkly Glasses’ Category". I suppose following that with something like ": which is all about photography and such blah blah blah..." would be perfect.

  2. t31os
    Member
    Posted 5 years ago #

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

    You just need to call the function i think....

    Not used it myself, but post if you get stuck....

  3. jamesisin
    Member
    Posted 5 years ago #

    Well, I can see that those are lines of code. And I understand just enough PHP to make out the basics of what those lines are intended to do. I am not at all clear what I would do with those lines of code.

    Anybody?

  4. iridiax
    Member
    Posted 5 years ago #

    You add it to whatever theme file is used for your category pages.

    Possibilities (themes differ):
    1. category.php
    2. archive.php
    3. index.php

  5. t31os
    Member
    Posted 5 years ago #

    I'd say the next question (as iridiax suggests) is does your theme have a category.php file?

    If so, post the code here inside backticks.

    And i/we can give an example...

  6. jamesisin
    Member
    Posted 5 years ago #

    I am using the ever-exciting default theme. I suppose that means index.php then? Do I just add those lines to the end of the document (before the get sidebar and footer lines)?

  7. jamesisin
    Member
    Posted 5 years ago #

    The default theme also has an archive.php file.

  8. t31os
    Member
    Posted 5 years ago #

    Well since you don't have a category file, let's make one...

    Start by plonking this into the category.php file.

    <?php
    /*
    Template Name: Category
    */
    ?>
    
    <?php get_header(); ?>
    
    		<div id="content" class="narrowcolumn">
    			<h2><?php the_category(); ?></h2>
                            <p class="someclassfordescription"><?php echo category_description(); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    			<div <?php post_class() ?>>
    				<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    				<small><?php the_time('l, F jS, Y') ?></small>
    
    				<div class="entry">
    					<?php the_content() ?>
    				</div>
    
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', ''); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    
    			</div>
    <?php endwhile; ?>
    
    		</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Untested, but it should work fine.... give it a test, and i'll check back tomorrow (sleep time soon) and see how you got on...

  9. jamesisin
    Member
    Posted 5 years ago #

    Thanks for that.

    Two questions...

    First, once I have created this file and placed it into my defaul theme folder (assuming that's the best location) how will I test to see if this file is actually doing anything?

    Second, assuming this file is created, saved, and functioning as expected where will I place the lines of code from the link provided by t31os?

  10. jamesisin
    Member
    Posted 5 years ago #

    Nope. That file breaks all of my category links.

  11. t31os
    Member
    Posted 5 years ago #

    I'll do a test later on for you, should be easy enough, it was untested...

  12. jamesisin
    Member
    Posted 5 years ago #

    Thanks for working on that other issue with me, t31os. I'm going to put that on hold until any new information comes to light.

    Any luck with this issue?

    (I'm sorry to be such a bother. If you can recommend a good book on either WP or especially PHP, I'm in the market.)

  13. t31os
    Member
    Posted 5 years ago #

    I did have a play around but couldn't find a simple way to grab the description.

    I think get_the_categories(); can pull up description in place of title, but that's one or the other, so not sure if that's suited.

    I'm sure there's a straight-forward way to grab it but i'm still getting use to the usage of WP functions so it's a little beyond what i've tried thus far..

    In regard to PHP, i'm still a novice admittedly, and i never could quite wrap by head round it, until one day after much fiddling something just clicked, or a light bulb came on and it made much more sense all of a sudden.

    I tend to work on something and learn what i need as i need it, and as time goes on i tend to pick new bits up...

    Of course if you like reading a pal recommends the O'Reily books highly, however different strokes for different folks, what book suits one guy may not suit the next...

    I'd say avoid the dummies books... i have a PHP and MYSQL one, and i found it awful to learn from.....

    Sitepoint.com have the best tutorials i've ever followed.

  14. t31os
    Member
    Posted 5 years ago #

    Turns out it is simple... this is from a file i created to test (category.php)..

    <?php get_header(); ?>
    
    <?php if (have_posts()) : ?>
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    echo '<br />'.$category[0]->category_description;
    ?>
      <ul>
      <?php while (have_posts()) : the_post(); ?>
      <li class="someclass-<?php the_ID();?>">
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
          <?php the_title(); ?> on <?php the_time('M j Y'); ?>
        </a>
      </li>
      <?php endwhile; ?>
      </ul>
    
      <div class="navigation">
        <div class="next"><?php next_posts_link(__('&laquo; Older Entries', 'elegant-grunge')) ?></div>
        <div class="previous"><?php previous_posts_link(__('Newer Entries &raquo;', 'elegant-grunge')) ?></div>
      </div>
    
    <?php else : ?>
    
      If no posts for the category do whatever is here...
    
    <?php endif; ?>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Obviously adjust the HTML as required...

    You can use your index.php as a basis, and adjust as required, that way you have the right DIVs and classes etc...

  15. jamesisin
    Member
    Posted 5 years ago #

    I have been poking through the default theme. I believe it might be best to place the category description code into the archive.php file. Here is the related code and where I think the description code ought to go:

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header();
    ?>
    
    	<div id="content" class="narrowcolumn">
    
    		<?php if (have_posts()) : ?>
    
     	  <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
     	  <?php /* If this is a category archive */ if (is_category()) { ?>
    		<h2 class="pagetitle">Category: <?php single_cat_title(); ?> </h2>
    
    <!--  Place category description code here  -->
    
     	  <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    		<h2 class="pagetitle">Posts Tagged ‘<?php single_tag_title(); ?>’</h2>
     	  <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
    		<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>
     	  <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
    		<h2 class="pagetitle">Archive for <?php the_time('F, Y'); ?></h2>
     	  <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
    		<h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>
    	  <?php /* If this is an author archive */ } elseif (is_author()) { ?>
    		<h2 class="pagetitle">Author Archive</h2>
     	  <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
    		<h2 class="pagetitle">Blog Archives</h2>
     	  <?php } ?>

    I just need to know what code to add to place the appropriate category description into those pages.

  16. t31os
    Member
    Posted 5 years ago #

    If you create a cateogry.php file you won't need half the above...

    category.php is only in effect when on a category page, as appose to an archive file which handles multiple cases...

    This part does the title and decription..

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    echo '<br />'.$category[0]->category_description;
    ?>

    Which goes in place of..

    <?php the_category() ;?>
    or
    <?php single_cat_title(); ?>

    depending where you place it...

    Not sure how reliable it is with multiple depth categories, ie. sub sub categories etc...

    Used the example from here, and just added description.
    http://codex.wordpress.org/Template_Tags/get_the_category

    Create a category.php, that way if anything if it doesnt work you can edit or remove the file where needed without effecting the archive.php functionality...

    I'd suggest a test install somewhere when testing code, whether in a sub-directory on your site or installed locally using WAMP, Uniform Server or any one of the other popular apps...

    Any code being tested does not effect the live site, once it works you upload the new file(s)...

  17. jamesisin
    Member
    Posted 5 years ago #

    I inserted this code into the arvchive.php file:

    <?php /* If this is a category archive */ if (is_category()) { ?>
    
    <!--  Default category title code
    <!--		<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>
    
    <!--  Modified category title and added description code
    
    		<h2 class="pagetitle"> <?php single_cat_title(); ?> </h2>
    <center>
    <?php
    $category = get_the_category();
    echo '<br />'.$category[0]->category_description;
    ?>
    </center>
    <br />
    
    <!--  Back to default

    I think it works well. You can see it in action here:

    http://www.soundunreason.com/InkWell/?cat=50

    My descriptions need work, but they are being displayed as I want them.

    Thanks for all your help on this one.

  18. t31os
    Member
    Posted 5 years ago #

    Assuming you only give posts a single category you should be fine, if you use multiple categories for each post and utilise parent categories you may get different results...

    The above returns the first category only (parent gets priority over child cats i think), which i think should be fine in most cases, but may not be reliable in all cases...

    Glad it's working as you like anyhow... ;-)

  19. jamesisin
    Member
    Posted 5 years ago #

    Well, Darkly Glasses is a sub-category. However, none of my right-pane category links are now working. And posts with multiple categories are also not working correctly (the category links), so this won't work.

  20. t31os
    Member
    Posted 5 years ago #

    When i viewed your page the description is shown and links are working as they should.

    Can you expand on the problem please...

  21. jamesisin
    Member
    Posted 5 years ago #

    Sorry, I fixed a stupidity on my part. Thought I posted about it. Not sure where that ended up.

    I didn't close my comments. Oops.

    <!--  Default category title code  -->
    <!--		<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>  -->
    
    <!--  Modified category title and added description code  -->
    
    		<h2 class="pagetitle"> <?php single_cat_title(); ?> </h2>
    <center>
    <?php
    $category = get_the_category();
    echo '<br />'.$category[0]->category_description;
    ?>
    </center>
    <br />
    
    <!--  Back to default  -->

    This works almost.

    Now the only problem remains that when a post has more than one category, the description of the first category of the first post over-rides the main category. Very close.

    Compare:

    http://www.soundunreason.com/InkWell/?cat=3 (Correct)

    http://www.soundunreason.com/InkWell/?cat=53 (Using Ah, Sophia!'s description)

    I wonder if it's the [0] that's doing that?

  22. t31os
    Member
    Posted 5 years ago #

    Yes unfortunately that's what i was getting at previously, it's correct to a degree until you have several categories assigned to a post.

    Perhaps someone can offer some further assistance, it's not something i've spent a great deal tackling, since i literally have 3 categories on my own installs.

  23. jamesisin
    Member
    Posted 5 years ago #

    Cool. I'll start another thread for this last bit.

  24. jamesisin
    Member
    Posted 5 years ago #

    If I use <?php echo category_description(5); > I get a specific description. Surely there is a way to ask my site which (x) is being called through single_cat_title.

  25. t31os
    Member
    Posted 5 years ago #

    I would imagine you get the description for the category with the ID of 5...

    I'm not sure that really helps...

    All i do is read the functions the same as you do, difference being i perhpas have a better grasp of PHP. All this if/else loop business can be just as confusing to me still though, you better believe it... lol..

    I'll have another go tomorrow on the local install with some more complex categories... should be good for a few hours of hair pulling...

    No promises... i'm still learning to.. ;-)

  26. jamesisin
    Member
    Posted 5 years ago #

    Exactly. What ever hard-number I enter does return that numbered-category description. Is there a way to create a variable, for instance called cat_num, which is the category being currently viewed? If so we could then create something like <?php echo category_description($cat_num); >. Sorry for my syntax butchery. I'm rather just guessing.

  27. t31os
    Member
    Posted 5 years ago #

    Problem is, what happens when a post has more then 1 cat (say 2 or 3). Then there's 2/3 results not one, and which one should it use...

    That puts us into the same situation as before...

    $category[0] is the first result out of how many there are, which is great when there's only 1 category, or even sometimes 2...

    The issues arises in how WP decides which one is the first result...

    The same problem arises in both cases...

    What's the relationship between categories on your site, 1 parent category and 1 sub-cat? ... how many levels deep are your categories...

  28. jamesisin
    Member
    Posted 5 years ago #

    I think this is working correctly:

    <!--  Default category title code  -->
    <!--		<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>  -->
    
    <!--  Modified category title and added description code  -->
    
    		<h2 class="pagetitle"> <?php single_cat_title(); ?> </h2>
    <center>
    <?php
    $category = get_the_category();
    echo category_description();
    ?>
    </center>
    
    <!--  Back to default  -->
  29. jamesisin
    Member
    Posted 5 years ago #

    Feel free to poke around, but I think that's doing the job.

    One thing I'd like to do for esthetics is narrow the area covered by the description to about 50% of the main column. I think I can do that by adding some more html with the center tag containing width restraint.

  30. t31os
    Member
    Posted 5 years ago #

    I'd be happy for you if it is....

    What happens when there's 2 or 3 cats?

    Does it work as expected?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.