1

So this is an extremely simple issue I'm sure, but I've spent hours trying to sort it out and my lack of programming skills is starting to drive me crazy.

I have a page on my blog that displays a random post, thanks to the shortcode that follows. I want the categories and tags to be displayed underneath the post text, with links to access other posts from those taxonomies.

Right now, my shortcode setup is this:

add_shortcode('random-post', 'random_post_shortcode');
function random_post_shortcode( $atts ) {

$args = array(
'numberposts'     => 1,
'category_name'  => $atts['category_name'], 
'orderby'         => 'rand',
'post_type'       => 'post',
'post_status'     => 'publish');
$post = get_posts($args);
foreach ($post as $p) {
    $categories = get_the_category($p->ID);
    $categories_string = '';
    $separator = ', ';
    if($categories) {
        foreach($categories as $category){
            $categories_string .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
        }
        $categories_string = trim($categories_string, $separator);
    }


    $post_tags = get_the_tags($p->ID);
    if ( $post_tags ) {
        foreach($post_tags as $tag) { 

        ?>
            <span class="entry-meta" style="font-size:8px;"><a href="<?php bloginfo('url');?>/tag/<?php print_r($tag->slug);?>">
              <?php print_r($tag->name); ?>
     </a></span>
     <?php
        }
    }


    $content =  apply_filters('the_content', $p->post_content);
}

return '<br><br>'.$content.'<div class="entry-meta">'.$categories_string.'</div>';  
}

What this does, of course, is display the post tags ABOVE the content. Try as I might, I just can't figure out how to only echo/print them AFTER the post text...!

Any advice?

1

You had it right with categories, just do the same for tags (using get_term_link instead) - better yet, use the core WordPress functions that automatically generate a list of category/tag links:

function random_post_shortcode( $atts ) {
    $posts = get_posts( array(
        'numberposts'   => 1,
        'category_name' => ! empty( $atts['category_name'] ) ? $atts['category_name'] : null, 
        'orderby'       => 'rand',
        'post_type'     => 'post',
        'post_status'   => 'publish',
    ));

    if ( ! $posts ) {
        return;
    }

    $post    = array_shift( $posts );
    $content = apply_filters( 'the_content', $post->post_content );

    // get_the_category_list() automatically does what your foreach was doing
    // https://developer.wordpress.org/reference/functions/get_the_category_list/
    if ( $categories = get_the_category_list( ', ', '', $post->ID ) ) {
        $content .= "<br />$categories";
    }

    // get_the_tag_list() does the same for post tags
    // https://developer.wordpress.org/reference/functions/get_the_tag_list/
    if ( $tags = get_the_tag_list( '', ', ', '', $post->ID ) ) {
        $content .= "<span class='entry-meta' style='font-size: 8px;'>$tags</span>";
    }

    return $content;
}
  • This is so clean and beautiful. Amazing. Thank you so much. – killer_rabbit Dec 12 '16 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.