[resolved] 'get_the_category_list' within 'the_content' (4 posts)

  1. yvojolie
    Posted 4 years ago #


    I use WordPress for quite some time now and whenever I had something it wouldn't do I just come to these forums and find my answer in notime!

    Today, however, I stumbled upon a problem I just can't understand that good and I've been searching for over 2 hours now to find a solution for.

    Ok, here's the thing…
    I want to show a blogpost like this:
    "Category Name - Blablabla this is the content of the post or page, blablalba."

    In my HTML file, it looks like this:
    <p><strong><a href="/category-name/" title="Category Name">Category Name</a></strong>- Blablabla this is the content of the post or page, blablalba.</p>

    But in WordPress, the only thing I can do is place <?php echo get_the_category_list(', '); ?> before <?php the_content( __( 'Read more »', 'mythemename' ) ); ?> but then the category link show up before the paragraph-tag of the_content, while I want to place it inside that paragraph-tag.

    Does anyone know how to make this work?


  2. Michael
    Forum Moderator
    Posted 4 years ago #

    possibly with a filter for 'the_content', added into functions.php of the theme;


    add_filter('the_content', 'add_category_into_content');
    function add_category_into_content($text) {
    global $post;
    $text = get_the_category_list(', ', '', $post->ID) . $text;
    return $text;
  3. yvojolie
    Posted 4 years ago #

    Thanks @alchymyth!
    Works like a charm. ;)

  4. ErikAtLarge
    Posted 4 years ago #

    Hey @alchymyth and @yvojolie this is great stuff.

    I am trying to insert the "category list" anywhere I want inside the actual content. When I follow your example it posts it before all of the_content.

    I followed the answer provided on this forum by @vtxyzzy and it worked well with allowing me to post the_title inside the_content using a shortcode. Here is the code from that forum (I especially enjoy the ability to add html tags before and after!)

    function mm_insert_title_func($atts) {
       // Insert the post title, with optional before and after strings.
       // [mm-insert-title before='<h3>' after='</h3>']
       global $post;
       extract(shortcode_atts(array('before' => '', 'after' => '' ), $atts));
       $output = "{$before}{$post->post_title}{$after}";
       return $output;
    add_shortcode('mm-insert-title', 'mm_insert_title_func');

    But trying to hack it together to work with get_the_category_list hasn't worked yet.

    To sum up, I want to be able to place all categories inside my content with the same functionality it usually has everywhere else in wordpress.

Topic Closed

This topic has been closed to new replies.

About this Topic