Feed Short Code

For anyone who comes across the need for a post feed or a page feed. I just wrote a small short-code plugin for a client that can make this easy. It’s a feed short-code. Just paste the short-code onto the page you want the feed to appear. You can edit the parameters to get the feed to pull the way you want. Copy the short-code below and paste into your WordPress editor.

This is the shortcode

[post feed]

This is the short-code with ALL the possible parameters. It’s very long. Copy the short-code and then delete the parameters you don’t need. I just put them in there for “copy paste” convenience.

[post_feed display_title ="true" content_or_excerpt ="content" excerpt_word_limit = "36" excerpt_end = "…more" display_featured_image ="false" hyperlink_what = "title" link_featured ="true" post_type = "post" post_id ="" post_name ="" page_id ="" pagename ="" year ="" meta_key ="" meta_value ="" posts_per_page ="" category_id ="" category_slug ="" multiple_category_ids ="" order = "DESC"]
//
//

This is the function. Place all this code below in your themes “functions.php” file.

function post_feed($atts) { //831AM
 
   extract( shortcode_atts( array(
    'display_title'            => true,
    'content_or_excerpt'       => 'content',
    'excerpt_word_limit'       => 36,
    'excerpt_end'              => '...more',
    'display_featured_image'   => false,
    'hyperlink_what'           => 'title',
    'link_featured'            => true,
    'post_type'                => 'post',
    'post_id'                  => '',
    'post_name'                => '',
    'page_id'                  => '',
    'pagename'                 => '',
    'year'                     => '',
    'meta_key'                 => '',
    'meta_value'               => '',
    'posts_per_page'           => '-1',
    'category_id'              => '',
    'category_slug'            => '',
    'multiple_category_ids'    => '',
    'order'                    => 'DESC'
    ), $atts ) );
    
 
    $new_args = array(
        'post_type'            => $post_type,
        'p'                    => $post_id,
        'page_id'              => $page_id,
        'pagename'             => $pagename,
        'name'                 => $post_name,
        'year'                 => $year,
        'meta_key'             => $meta_key,
        'meta_value'           => $meta_value,
        'posts_per_page'       => $posts_per_page,
        'category_name'        => $category_slug,
        'cat'                  => $category_id,
        'category__and'        => $multiple_category_ids,
        'order'                => $order
    );
    
    $nest_query = new WP_Query( $new_args );
    
    
    
                     $output .= '<div id="all_posts_container">';
                     
                           if ( $nest_query->have_posts() ) : while ( $nest_query->have_posts() ) : $nest_query->the_post();
                            $this_post_id          = get_the_ID();
                            $this_post_title       = get_the_title();
                            $this_post_excerpt     = get_the_excerpt();
                            $this_post_excerpt     = string_limit_words($this_post_excerpt,$excerpt_word_limit);
                            if (!empty($this_post_excerpt)) { $this_post_excerpt .= $excerpt_end; }
                            $this_post_content     = get_the_content();
                            $this_post_link        = get_permalink($this_post_id);
                            
                            if ($hyperlink_what == 'all')     { $link_all_begining       ="<a href=\"$this_post_link\" >"; $link_all_end="</a>"; }
                            if ($link_featured == true)       { $link_featured_begining  ="<a href=\"$this_post_link\" >"; $link_featured_end="</a>"; }
                            if ($hyperlink_what == 'title')   { $link_title_begining     ="<a href=\"$this_post_link\" >"; $link_title_end="</a>"; }
                            if ($hyperlink_what == 'excerpt') { $link_excerpt_begining   ="<a href=\"$this_post_link\" >"; $link_excerpt_end="</a>"; }
                            if ($hyperlink_what == 'content') { $link_content_begining   ="<a href=\"$this_post_link\" >"; $link_content_end="</a>"; }
                            
                           $count++;
                           
                           
                           $output .= "$link_all_begining <div class=\"single_post_container\" >";
                            
                               $featured_images  = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
                               $featured_image   = $featured_images[0];
                               
                                   if ($display_featured_image == true && !empty($featured_image)) { // 852AM                      
                                       $output .= "<div class=\"featured_wrapper_$count\">";       
                                       $output .= "<img src=\"$featured_image\" class=\"featured_image\" id=\"featured_$count\" />";
                                       $output .= "</div><!-- featured_wrapper -->";
                                   } // 852AM
                                   
                                   if ($display_title == true) { // 853AM                  
                                       $output .= "$link_title_begining<div class=\"post_title\">";
                                       $output .= $this_post_title;
                                       $output .="</div>$link_title_end";
                                   } // 853AM
                                  
                                   if ($content_or_excerpt == 'content') { // 854AM                  
                                       $output .= "$link_content_begining<div class=\"post_content\">";
                                       $output .= "$this_post_content";
                                       $output .="</div>$link_content_end";
                                   } // 854AM
                                   
                                   if ($content_or_excerpt == 'excerpt') { // 852AM                  
                                       $output .= "$link_excerpt_begining<div class=\"post_excerpt\">";
                                       $output .= $this_post_excerpt;
                                       $output .="</div>$link_excerpt_end";
                                   } // 855AM
                                  
                              
                          $output .= "</div>$link_all_end<!-- single_post_container -->";
                          
                          endwhile;
                          endif;
                          
                          $output .= "<br style=\"clear:both;\" />";
                            
                  $output .= "</div><!-- all_posts_container -->";
                  
                  
$all_data .="<div class='pagenation_wrap'>";
 
    if (!empty($_GET['offset'])) { 
     
    $all_data .="<a href='?offset=0' class='pagenation'><< First</a>";
     
    }
  
   $call_args['offset'] = $_GET['offset'];
   
   
   $categories      = get_term_by('slug',$cat_slug_1,'category');
   $number_in_cat   = $categories->count; 
   
   if (!empty($cat_slug_2)) {
	   $categories2      = get_term_by('slug',$cat_slug_2,'category');
	   $number_in_cat2   = $categories2->count;
       $number_in_cat    = $number_in_cat + $number_in_cat2;
   
   }
 
   $num_of_pages        = ceil($number_in_cat/$posts_per_page);
   $offset              = $_GET['offset'];
   $current_offset      = 0;
   $display_max_count   = $_GET['offset']/$posts_per_page+$posts_per_page;
   $display_min_count   = $_GET['offset']/$posts_per_page-$posts_per_page;
   $current_count       = 0;
      
   for($c=1;$c<=$num_of_pages;$c++) {
       $current_count++;
       if (empty($current_offset))  { $current="current_pagenation"; }else{ $current=""; }
       if (!empty($offset)) { if ($current_offset/$offset == 1 ) { $current="current_pagenation"; }else{ $current=""; } }
       if ($current_count > $display_min_count && $current_count < $display_max_count) {
       $all_data .="<a href='?offset=$current_offset' class='pagenation $current'>$c</a> | ";
       }
       $current_offset = $current_offset + $posts_per_page;
       $last_offset = $current_offset-$posts_per_page;
       $max_offset = $current_offset - $posts_per_page;
   }


    if ($number_in_cat > $offset && $offset != $max_offset) {
    $all_data .="<a href='?offset=$last_offset' class='pagenation $current'>Last >></a>";
    }
     
$all_data .="$number_in_cat - $posts_per_page</div>";
 
  if ($number_in_cat > $posts_per_page) {
	  $output .= $all_data;
  }
                  
                  
   wp_reset_query();  
   
   
   
                
   return $output;  
                  

                                      
} //831AM
 
add_shortcode( 'post_feed', 'post_feed' );


Comments