WordPress.org

Forums

Robust Category Description / Sticky Posts (8 posts)

  1. joshklein
    Member
    Posted 6 years ago #

    Hello,

    I was wondering your thoughts on the best way to have a "category introduction" unique to each category, sticky to the top of it's archive page.

    I see one way to do it as to use the category description field (the intended use of that functionality for sure), but the admin-side input is pretty weak there... I'd like to be able to use the full WYSIWYG editor.

    I see another way to do it as to use the sticky post functionality, but there must be all kinds of issues with what gets stuck on the front page versus category archive, etc.

    What are your thoughts on making that work?

  2. SimonJ
    Member
    Posted 6 years ago #

    Hi Josh

    OK. from my point of view, you can use two method to get a category description.

    The first one uses external files named with the category slugs that you store in your theme directory.

    The second one uses some posts you write via the WorpPress dashboard.

    WITH EXTERNAL FILE :

    The main idea is to include in the top of your category template a file named with the category slug.

    Let's say, for instance, that you have two categories : cat and dog.

    1- create a directory in your theme folder and name it : /cat_description.

    2- In this directory, create a file for each category :
    cat.php
    dog.php

    3- Edit this file and put any content you like for the description. This content can include some CSS (that you will define in your style.css), line breaks, paragraphs, links, etc.

    4- In the category template of your theme, i.e. "category.php", Put this code wherever you want the description to appear :

    <?php
    $category = get_the_category('');
    $cat_slug = $category[0]->cat_name;
    if (file_exists(TEMPLATEPATH. '/cat_description/'.$cat_slug.'.php') ) {
    include (TEMPLATEPATH . '/cat_description/'.$cat_slug.'.php');
    } else {
        echo '';
    }
    ?>

    This code get the category slug as a variable, it looks in the "cat_description" directory if a file of this name exist. If so, it display the content of this file. If there is no file of such a name, it display nothing.

    --------------------
    WITH POSTS YOU WRITE VIA WORDPRESS :

    1 - Create a category named "category description"

    2 - Write some posts to describe categories, and use the exact category name you describe for title. Of course, you choose "category descripton" as category for these posts.

    3 - In the category template of your theme, i.e. "category.php", Put this code wherever you want the description to appear :

    <?php
    $category = get_the_category('');
    $cat_slug = $category[0]->category_nicename;
    query_posts(array(
    'name'=> $cat_slug,
    ) );
    if (have_posts()) : while (have_posts()) : the_post();
    echo '<div class="category_description">';
    the_content();
    echo '</div>';
    endwhile;
    endif;
    wp_reset_query();
    ?>

    As you can see, I wrote a "div" in this code. You can define this div in your "style.css".

    This code get the category slug you are in, it looks if there is a post with the same name, if so, the content of the post is displayed, if no, nothing is displayed...

    --------

    Each method have his pro and cons.

    With the external file, you have to write the content yourself in an external text editor and upload files manually. You can use an html editor to get the code you like, or even wordpress (you write a post and copy/paste the html, without saving the post)

    With the posts via wordpress method, you have the advantage of the full WYSIWYG editor and you do not need to manually upload files, but you will have to edit your template files to hide the posts from category "category description" in your index, your archives, your category list, etc.

    From my point of view, the external files are more simple. And as you can use wordpress as an html editor, from wich you can cut and paste the result in the external file, you also have the benefit of the WYSIWYG editor.

    VoilĂ ... :-)

    S.

  3. joshklein
    Member
    Posted 6 years ago #

    Straight up brilliant. Going with the external file method. You're a life saver. Thanks!

  4. Luigino
    Member
    Posted 6 years ago #

    Hello Simonj,

    I used your suggest to try my sticky post... BUT if I create two different sticky posts in the same page because I want to show them separately... like:

    <div id="boxmodel-stickypost">
    <?php
    $category = get_the_category('');
    $cat_slug = $category[0]->category_nicename;
    query_posts(array('Post 1'=> $cat_slug,) );
    if (have_posts()) : while (have_posts()) : the_post();
    echo '<div class="category_description">'; the_content(); echo </div>';
    endwhile;
    endif; wp_reset_query();
    ?>
    </div>
    <div id="boxmodel-stickypost">
    <?php
    $category = get_the_category('');
    $cat_slug = $category[0]->category_nicename;
    query_posts(array('Post 2'=> $cat_slug,) );
    if (have_posts()) : while (have_posts()) : the_post();
    echo '<div class="category_description">'; the_content(); echo </div>';
    endwhile;
    endif; wp_reset_query();
    ?>
    </div>
    <div id="boxmodelposts">
    ... loop....
    </div>

    I get in both sticky posts both Post 1 and Post 2 which are the titles of posts I put in category "category description"... why?

  5. evilnrj
    Member
    Posted 6 years ago #

    Hey, I was wondering when using the external method:

    ?php
    $category = get_the_category('');
    $cat_slug = $category[0]->cat_name;
    if (file_exists(TEMPLATEPATH. '/cat_description/'.$cat_slug.'.php') ) {
    include (TEMPLATEPATH . '/cat_description/'.$cat_slug.'.php');
    } else {
    echo '';
    }
    ?>
    Is it possible to make it use the actual slug instead of the name? For Example Say I have a category named How's it Going?, I want to name the external file hows-it-going.php instead of How's it Going.php and add the ? inside the ftp program after.

    Other than that thanks for the great workaround!

  6. SimonJ
    Member
    Posted 6 years ago #

    @ evilnrj

    Sorry if I took a month to get back to you... Don't know if you will read this answer nor if you found the solution :

    To use the actual category slug, just use "category_nicename" instead of "cat_name"... Like this :

    <?php
    $category = get_the_category('');
    $cat_slug = $category[0]->category_nicename;
    if (file_exists(TEMPLATEPATH. '/cat_description/'.$cat_slug.'.php') ) {
    include (TEMPLATEPATH . '/cat_description/'.$cat_slug.'.php');
    } else {
    echo '';
    }
    ?>

    S.

  7. evilnrj
    Member
    Posted 6 years ago #

    Ah thanks a lot mate! One thing i noticed with the external is that if you have a post that's in two categories, you might get the description in the wrong category (Two "equal" categories, child categories work).

    Also another useful thing is to add if(!is_search()), otherwise you'll get them up on the search pages.

  8. Pierre_02
    Member
    Posted 6 years ago #

    Hi all,

    The way i use and that i suggest to you is to create a page named "category.php" that you place in your theme folder. Then place the following code in it. The value "11" in the first line of code is the length of my manually defined "Category base" name in the page Options->Permalink plus 2 slash /categorie/ -> length=11. You'll have to adapt it if you use false permalink (urls with index.php)

    $cat_en_cours = substr($_SERVER['REQUEST_URI'],11);
    $posPage=strpos($cat_en_cours,"page/");
    if($posPage>=1) $cat_en_cours = substr($cat_en_cours,0,$posPage);
    $sous_cat = explode("/", $cat_en_cours);
    $posSlash=strpos($cat_en_cours,"/");
    $cat_en_cours = substr($cat_en_cours,0,$posSlash);
    
    query_posts('pagename='.$cat_en_cours);
    
    if (have_posts()) :
    	while (have_posts()) : the_post(); update_post_caches($posts);
    		[...]
    	endwhile;
    endif;
    
        $args=array(
        'cat'=>$cat,
        'paged'=>$paged,
        'orderby'=> 'ID',
        'order'=> 'desc',
       );
    query_posts($args);
    
    if (have_posts()) :
    	while (have_posts()) : the_post(); update_post_caches($posts);
    		[...]
    	endwhile;
    endif;

    By the way if you want to create an introduction to describe your category the only thing you'll have to do is to create a page in WP with exactly the same name that your category. You'll be able to use the rich visual editor of WP, add pictures etc without any need to touch in code ;).

    Amicably,

    Pierre.

Topic Closed

This topic has been closed to new replies.

About this Topic