Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #175453

    ryan b
    Participant

    Hi

    I’m trying to make the recent posts section on the home page display the category (along with the title and date that it currently shows).

    I’m working in the short codes.php atm and my current code for the recent posts section is:

    function x_shortcode_recent_posts( $atts ) {
    extract( shortcode_atts( array(
    ‘id’ => ”,
    ‘class’ => ”,
    ‘style’ => ”,
    ‘type’ => ”,
    ‘count’ => ”,
    ‘category’ => ”,
    ‘offset’ => ”,
    ‘orientation’ => ”,
    ‘no_image’ => ”,
    ‘fade’ => ”
    ), $atts ) );

    $id = ( $id != ” ) ? ‘id=”‘ . esc_attr( $id ) . ‘”‘ : ”;
    $class = ( $class != ” ) ? ‘x-recent-posts cf ‘ . esc_attr( $class ) : ‘x-recent-posts cf’;
    $style = ( $style != ” ) ? ‘style=”‘ . $style . ‘”‘ : ”;
    $type = ( $type == ‘portfolio’ ) ? ‘x-portfolio’ : ‘post’;
    $count = ( $count != ” ) ? $count : 3;
    $category = ( $category != ” ) ? $category : ”;
    $category_type = ( $type == ‘post’ ) ? ‘category_name’ : ‘portfolio-category’;
    $offset = ( $offset != ” ) ? $offset : 0;
    $orientation = ( $orientation != ” ) ? ‘ ‘ . $orientation : ‘ horizontal’;
    $no_image = ( $no_image == ‘true’ ) ? $no_image : ”;
    $fade = ( $fade == ‘true’ ) ? $fade : ‘false’;

    $output = “<div {$id} class=\”{$class}{$orientation}\” {$style} data-fade=\”{$fade}\”>”;

    $q = new WP_Query( array(
    ‘orderby’ => ‘date’,
    ‘post_type’ => “{$type}”,
    ‘posts_per_page’ => “{$count}”,
    ‘offset’ => “{$offset}”,
    “{$category_type}” => “{$category}”
    ) );

    if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();

    if ( $no_image == ‘true’ ) {
    $image_output = ”;
    $image_output_class = ‘no-image’;
    } else {
    $image_output = ‘<div class=”x-recent-posts-img”>’ . get_the_post_thumbnail( get_the_ID(), ‘entry-‘ . x_get_option( ‘x_stack’, ‘integrity’ ) . ‘-cropped’, NULL ) . ‘</div>’;
    $image_output_class = ‘with-image’;
    }

    $output .= ‘
    . ‘<article id=”post-‘ . get_the_ID() . ‘” class=”‘ . implode( ‘ ‘, get_post_class() ) . ‘”>’
    . ‘<div class=”entry-wrap”>’
    . $image_output
    . ‘<div class=”x-recent-posts-content”>’
    . ‘<h3 class=”h-recent-posts”>’ . get_the_title() . ‘</h3>’
    . ‘<span class=”x-recent-posts-date”>’ . get_the_date() . ‘</span>’
    . ‘<span class=”x-recent-posts-cat”>’ . get_the_category( $category ) . ‘</span>’

    . ‘</div>’
    . ‘</div>’
    . ‘</article>’
    . ‘
    ‘;

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    As you can see I added the

    . ‘<span class=”x-recent-posts-cat”>’ . get_the_category( $category ) . ‘</span>’

    at the bottom of the code but it is only returning the word “array” instead of the actual categories, can you help?

    cheers
    ryan

    #175758

    Rad
    Moderator

    Hi there,

    Thanks for posting in.

    You can check this http://codex.wordpress.org/Function_Reference/get_the_category

    You can just copy the code from that link, add this code

    $categories = get_the_category();
    $separator = ' ';
    $output_cats = '';
    if($categories){
    	foreach($categories as $category) {
    		$output_cats .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
    	}
    }

    right after the line if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();

    Then change the line . '<span class="x-recent-posts-cat">' . get_the_category( $category ) . '</span>' into

    . '<span class="x-recent-posts-cat">' . trim($output_cats, $separator) . '</span>'

    Hope this help.

    #175840

    ryan b
    Participant
    This reply has been marked as private.
    #175851

    ryan b
    Participant
    This reply has been marked as private.
    #175868

    ryan b
    Participant
    This reply has been marked as private.
    #176040

    Christopher
    Moderator

    Thanks for writing in! To assist you with this issue, we’ll first need you to provide us with your URL as stated on the forum entrance page. This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you with everything.

    #176146

    ryan b
    Participant
    This reply has been marked as private.
    #176242

    Christopher
    Moderator

    Hi there,

    the provided URL is not available.
    Please check it.

    Thank you.

    #176254

    ryan b
    Participant
    This reply has been marked as private.
    #176495

    Christian
    Moderator

    Hey Ryan,

    The URL is still not available even after several reloads. Please check.

    Thanks.