• Resolved waterwalk

    (@waterwalk)


    Hi, I’m creating a custom display of posts because we need it in a specific order and we need to color code the categories and it was too hard trying to customize the plugins I found out there.

    I have everything working except the categories. I need to be able to list the categories before the post title and be able to assign each one an ID for color coding purposes (which I can do once I can figure out how to get the categories to display in the first place). I’ve tried wp_get_post_categories and wp_list_categories and at this point am back to square one with just the following that works (this works without categories):

    function recent_posts_function($atts){

    extract(shortcode_atts(array(
    ‘posts’ => 5,
    ), $atts));

    $return_string = ‘

      ‘;
      query_posts(array(‘orderby’ => ‘date’, ‘order’ => ‘DESC’ , ‘showposts’ => $posts));
      if (have_posts()) :
      while (have_posts()) : the_post();

    $return_string .= ‘

    • ‘.get_the_title().’
      <img src=”/wp-content/uploads/2015/01/post-date.jpg” border=”0″ style=”float: left; padding-right:5px;”>’.get_the_date().’

    • ‘;
      endwhile;
      endif;
      $return_string .= ‘
      ‘;

      wp_reset_query();
      return $return_string;
      }

      function register_shortcodes(){
      add_shortcode(‘kapitall-recent-posts’, ‘recent_posts_function’);
      }

      add_action( ‘init’, ‘register_shortcodes’);

      I’d appreciate any feedback on how to get categories pulled into this (there may be several categories for one post). Thanks in advance for any tips you can provide.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter waterwalk

    (@waterwalk)

    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');
    Thread Starter waterwalk

    (@waterwalk)

    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');
    Thread Starter waterwalk

    (@waterwalk)

    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');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get categories for a post?’ is closed to new replies.