Still hoping someone can give some direction on this. I am trying to insert categories for a post (before the post title). I’m sure I’m doing something wrong with syntax but not sure yet where the error is. Here was my most recent attempt (categories still not displaying). Any help would be appreciated.
function recent_posts_function($atts){
extract(shortcode_atts(array(
'posts' => 5,
), $atts));
$return_string = '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<li>
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . " ";
}
?>
<a href="'.get_permalink().'">'.get_the_title().'</a><br/><img src="/wp-content/uploads/2015/01/post-date.jpg" border="0" style="float: left; padding-right:5px;">'.get_the_date().'<br/><br/>
</li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
function register_shortcodes(){
add_shortcode('kapitall-recent-posts', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
Okay, I have gotten this far now and am able to display one category but am still unsure how to display all categories for a post that has several categories. If I am posting in the wrong area please let me know. I thought this would be the best fit since I’m basically creating a my own plugin (shortcode) to add to the site.
This code works now but only for 1 category. Can anyone help me figure out how to make it work so that multiple categories will show the same information (category link with its associated category slug as id, category title). Thanks in advance.
function recent_posts_function($atts){
extract(shortcode_atts(array(
'posts' => 5,
), $atts));
$return_string = '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
global $post;
$categories = get_the_category($post->ID);
$cat_link = get_category_link($categories[0]->cat_ID);
$return_string .= '<li><a href="'.$cat_link.'" id="'.$categories[0]->slug.'">'.$categories[0]->cat_name.'</a>
<br/><a href="'.get_permalink().'">'.get_the_title().'</a><br/><img src="/wp-content/uploads/2015/01/post-date.jpg" border="0" style="float: left; padding-right:5px;">'.get_the_date().'<br/><br/>
</li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
function register_shortcodes(){
add_shortcode('kapitall-recent-posts', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
Latest code:
function recent_posts_function($atts){
extract(shortcode_atts(array(
'posts' => 5,
), $atts));
$return_string = '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
global $post;
$categories = get_the_category($post->ID);
$separator = ' | ';
$output = '';
$cat_link = get_category_link($categories[0]->cat_ID);
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category->term_id ).'" id="'.$category->slug.'">'.$category->cat_name.'</a>'.$separator;
}
}
$return_string .= '<li>'.$output.'
<br/><a href="'.get_permalink().'">'.get_the_title().'</a><br/><img src="/wp-content/uploads/2015/01/post-date.jpg" border="0" style="float: left; padding-right:5px;">'.get_the_date().'<br/><br/>
</li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
function register_shortcodes(){
add_shortcode('kapitall-recent-posts', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');