-
AuthorPosts
-
January 2, 2015 at 8:43 pm #175453
ryan bParticipantHi
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’;
}. ‘</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
ryanJanuary 3, 2015 at 1:10 pm #175758
RadModeratorHi 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.
January 3, 2015 at 6:17 pm #175840
ryan bParticipantThis reply has been marked as private.January 3, 2015 at 6:47 pm #175851
ryan bParticipantThis reply has been marked as private.January 3, 2015 at 7:34 pm #175868
ryan bParticipantThis reply has been marked as private.January 4, 2015 at 2:36 am #176040
ChristopherModeratorThanks 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.
January 4, 2015 at 6:54 am #176146
ryan bParticipantThis reply has been marked as private.January 4, 2015 at 10:57 am #176242
ChristopherModeratorHi there,
the provided URL is not available.
Please check it.Thank you.
January 4, 2015 at 11:48 am #176254
ryan bParticipantThis reply has been marked as private.January 4, 2015 at 11:39 pm #176495
ChristianModeratorHey Ryan,
The URL is still not available even after several reloads. Please check.
Thanks.
-
AuthorPosts
- <script> jQuery(function($){ $("#no-reply-175453 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>
