• Hi there,

    I’m trying to add a few sentences before posts of a certain category. To be precise, what I’d like to do is to add it before each post and on the page displaying posts of a certain category (accessible from the menu).

    What I’ve done so far is copied the index.php from the twentyeleven theme, edited it and saved as category.php and uploaded back. This doesn’t work though. My modification was as follows:

    <?php if (is_category('ads')) : ?><p>custommmmmmm</p><?php endif; ?>

    Just before starting the loop. Full code https://pastebin.com/KESTHtJS

    I know it’s recommended to use child themes for any modifications but first want to check if I can achieve what I want before creating a child theme.

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • The is_category() page at https://codex.www.ads-software.com/Function_Reference/is_category has a great working example, I

    <?php
    global $post;
    if ( is_category( 'ads' ) ) {
    ?>
    <p>MY CUSTOM TEXT</p>
    <?php } ?>

    Also, you might want to consider making your code a little more flexible and calling the category_description(), which would allow you to set the text within your category page.

    <?php
    if ( category_description( get_category_by_slug( 'category-slug' )->term_id ) )
         echo category_description( get_category_by_slug( 'category-slug' )->term_id );
    ?>
    Thread Starter portia79

    (@portia79)

    Ok, After a few hours of playing with it I managed to do it in a child theme. I don’t know why the above didn’t work.

    Thread Starter portia79

    (@portia79)

    Hi Christopher,

    I didn’t see your post. Thanks. I’ll change it. One more question. How would I also add a custom sidebar to that conditional statement. I’ve got a few sidebars created.

    Thank you.

    Adding a custom sidebar to your category.php page is pretty easy really, and it’ll let you fall back to the default sidebar if there isn’t one present.

    <?php
    global $post;
    if ( is_category() ) {
         $current_category = get_category( get_query_var( 'cat' ) );
         get_sidebar( $current_category->slug );
    }
    ?>

    Now, it’ll try to load sidebar-ads.php for example but if if can’t be found you’ll automatically revert to sidebar.php

    Thread Starter portia79

    (@portia79)

    Thanks. When I said I have a few sidebars, I meant I created them from the dashboard (using the WP Custom Sidebar plugin). I can’t find the relevant files.

    The plugin is good for individual post/pages. It doesn’t really work for categories.

    How would the sidebar-ads.php look like? Let’s say it’d only want text there(eg. <p>Some other custom text</p>). I opened the sidebar.php from the theme directory but I can’t say I understand much.

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘displaying short text for one category’ is closed to new replies.