Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm not a coder so I may have done some "coding" crimes, apologies. I've bought a theme who has shortcodes for last posts but not for last "custom post type". So I tried to create last "custom post type" function modifying the Last post function provided. Last post funtion:

function get_custom_posts( $params ) {
extract( shortcode_atts( array (
    'number' => '1',
    'excerpt' => 290,
    'readmore' => 'no',
    'readmoretext' => 'Read more'
), $params ) );

//$latest_posts = get_posts( 'category=0&numberposts=' . $number . '&suppress_filters=false');     OLD
$latest_posts = query_posts( 'cat=0&posts_per_page=' . $number );
wp_reset_query();

$result = '<div class="latest-posts">';
$count = count($latest_posts);
foreach ($latest_posts as $key => $latest_post) {
  $author = get_the_author_meta('nickname', $latest_post->post_author );
  $post_link = get_permalink( $latest_post->ID );
  $date = mysql2date(get_option('date_format'), $latest_post->post_date);
  $category = get_the_category_list( ', ', $parents = '', $latest_post->ID );

  $result .= '<div class="sc-page"><div class="item clearfix">';
  // POST THUMBNAIL
  if (get_the_post_thumbnail( $latest_post->ID, 'thumbnail' )) {
      $result .= '<div class="image">';
      $result .= '<a href="' . $post_link . '" class="greyscale">';
      $result .= get_the_post_thumbnail( $latest_post->ID, 'thumbnail' );
      $result .= '</a>';
      $result .= '</div>';
  }

  // POST BODY
  $result .= '<div class="text">';
  $result .= '<div class="title"><h3><a href="' . $post_link. '">' . $latest_post->post_title . '</a></h3></div>';
  if ( $latest_post->post_excerpt ) {
      $result .= '<p>' . $latest_post->post_excerpt . '</p>';
  }
  else {
      $limit = $excerpt;
      $my_text = substr($latest_post->post_content, 0, $limit);
      $pos = strrpos($my_text, " ");
      $my_post_text = substr($my_text, 0, ($pos ? $pos : -1)) . "...";
      $read = "";
  if($readmore == 'yes'){
    $read = '&nbsp;<a href="' . $post_link. '">'.$readmoretext.'</a>';
  }
      $result .= '<p>' . strip_tags($my_post_text) . $read . '</p>';
      //$result .= '<p>' . substr_replace( $latest_post['0']->post_content, '...', 350 ) . '</p>';
  }

  $result .= '</div><!-- /.text -->';

  if ($count - 1 != $key)
  $result .= do_shortcode('[rule]');

  $result .= '</div></div>';
}
$result .= '</div>';

return $result;
}
add_shortcode( "get_posts", "get_custom_posts" );

Then I tried to modify it like this, (my custom post type is ait-dir-item):

function get_custom_items( $params ) {
extract( shortcode_atts( array (
    'number' => '1',
    'excerpt' => 290,
    'readmore' => 'no',
    'readmoretext' => 'Read more'
), $params ) );

//$latest_posts = get_posts( 'category=0&numberposts=' . $number . '&suppress_filters=false');     OLD
$latest_items = WP_query( 'post_type' = 'ait-dir-item', 'cat=0&posts_per_page=' . $number );
wp_reset_query();

$result = '<div class="latest-items">';
$count = count($latest_items);
foreach ($latest_items as $key => $latest_item) {
  $author = get_the_author_meta('nickname', $latest_item->post_author );
  $post_link = get_permalink( $latest_item->ID );
  $date = mysql2date(get_option('date_format'), $latest_item->post_date);
  $category = get_the_category_list( ', ', $parents = '', $latest_item->ID );

  $result .= '<div class="sc-page"><div class="item clearfix">';
  // POST THUMBNAIL
  if (get_the_post_thumbnail( $latest_item->ID, 'thumbnail' )) {
      $result .= '<div class="image">';
      $result .= '<a href="' . $post_link . '" class="greyscale">';
      $result .= get_the_post_thumbnail( $latest_post->ID, 'thumbnail' );
      $result .= '</a>';
      $result .= '</div>';
  }

  // POST BODY
  $result .= '<div class="text">';
  $result .= '<div class="title"><h3><a href="' . $post_link. '">' . $latest_item->post_title . '</a></h3></div>';
  if ( $latest_item->post_excerpt ) {
      $result .= '<p>' . $latest_item->post_excerpt . '</p>';
  }
  else {
      $limit = $excerpt;
      $my_text = substr($latest_item->post_content, 0, $limit);
      $pos = strrpos($my_text, " ");
      $my_post_text = substr($my_text, 0, ($pos ? $pos : -1)) . "...";
      $read = "";
  if($readmore == 'yes'){
    $read = '&nbsp;<a href="' . $post_link. '">'.$readmoretext.'</a>';
  }
      $result .= '<p>' . strip_tags($my_post_text) . $read . '</p>';
      //$result .= '<p>' . substr_replace( $latest_item['0']->post_content, '...', 350 ) . '</p>';
  }

  $result .= '</div><!-- /.text -->';

  if ($count - 1 != $key)
  $result .= do_shortcode('[rule]');

  $result .= '</div></div>';
}
$result .= '</div>';

return $result;
}
add_shortcode( "get_items", "get_custom_items" );

Of course, this function doesn't work... Can someone help me? I want to modify this function instead of installing a plugin because I want to use the same classes so I will have the same design for both shortcodes. Thanks in advance!

share|improve this question
    
You do not need to create another function. Just add another parameter to the shortcode which could be $post_type –  JMau Jun 11 '13 at 12:43

3 Answers 3

up vote 0 down vote accepted

This seems to work with custom post type :

function get_custom_posts( $params ) {
extract( shortcode_atts( array (
'number' => '1',
'excerpt' => 290,
'readmore' => 'no',
'cpt' => 'post',
'readmoretext' => 'Read more'
), $params ) );

//$latest_posts = get_posts( 'category=0&numberposts=' . $number .      '&suppress_filters=false');     OLD
$latest_posts = query_posts( 'post_type='.$cpt.'&posts_per_page=' . $number );
wp_reset_query();

$result = '<div class="latest-posts">';
$count = count($latest_posts);
foreach ($latest_posts as $key => $latest_post) {
  $author = get_the_author_meta('nickname', $latest_post->post_author );
  $post_link = get_permalink( $latest_post->ID );
  $date = mysql2date(get_option('date_format'), $latest_post->post_date);
  $category = get_the_category_list( ', ', $parents = '', $latest_post->ID );

  $result .= '<div class="sc-page"><div class="item clearfix">';
 // POST THUMBNAIL
  if (get_the_post_thumbnail( $latest_post->ID, 'thumbnail' )) {
      $result .= '<div class="image">';
      $result .= '<a href="' . $post_link . '" class="greyscale">';
      $result .= get_the_post_thumbnail( $latest_post->ID, 'thumbnail' );
      $result .= '</a>';
      $result .= '</div>';
  }

  // POST BODY
  $result .= '<div class="text">';
  $result .= '<div class="title"><h3><a href="' . $post_link. '">' . $latest_post->post_title . '</a></h3></div>';
  if ( $latest_post->post_excerpt ) {
      $result .= '<p>' . $latest_post->post_excerpt . '</p>';
  }
  else {
      $limit = $excerpt;
      $my_text = substr($latest_post->post_content, 0, $limit);
      $pos = strrpos($my_text, " ");
      $my_post_text = substr($my_text, 0, ($pos ? $pos : -1)) . "...";
      $read = "";
  if($readmore == 'yes'){
    $read = '&nbsp;<a href="' . $post_link. '">'.$readmoretext.'</a>';
  }
      $result .= '<p>' . strip_tags($my_post_text) . $read . '</p>';
      //$result .= '<p>' . substr_replace( $latest_post['0']->post_content, '...', 350 ) . '</p>';
  }

  $result .= '</div><!-- /.text -->';

  if ($count - 1 != $key)
  $result .= do_shortcode('[rule]');

  $result .= '</div></div>';
}
$result .= '</div>';

return $result;
}
add_shortcode( "get_posts", "get_custom_posts" );

To use it just write [get_posts cpt="ait-dir-item"] in your posts

EDIT: it's bad practise to use query_post() in this context

share|improve this answer
2  
You should not be using query_posts for a secondary Loop. –  s_ha_dum Jun 11 '13 at 14:00
    
Thanks @TweetPressFr! It works replacing query_post) and deleting "new". It's just what I needed! –  Charlyem Jun 12 '13 at 16:04
    
Sorry @TweetPressFr I meant that It works only with query_post and deleting "new". Why should I use new WP_query instead? –  Charlyem Jun 12 '13 at 16:18
    
because it's bad practise to use query_post in this context. I might not work with WP_query because object is not returned. –  JMau Jun 12 '13 at 16:28

The problem looks to me to be this line:

$latest_items = WP_query( 'post_type' = 'ait-dir-item', 'cat=0&posts_per_page=' . $number );

That is very odd. First, you are missing the new keyword. Without it you get an "undefined function" error.

Second, with WP_Query, and a number of other functions, you can use a query-string-like function argument, or an array. You have (broken) elements of both in there, which isn't going to work.

For a query-string-like parameter, you'd need:

$latest_items = new WP_query('post_type=ait-dir-item&cat=0&posts_per_page='.$number);

See how that is all one string, and yours isn't? You have three distinct strings, an = floating loose, and a comma that is going to indicate that you are actually using two parameters and not just one. If you have debugging enabled you'd see errors.

But use an array. Those query-string-like parameters are hard to read, hard to edit, and error prone because of it. At least that is my take.

$latest_items = new WP_query( 
  array(
    'post_type' => 'ait-dir-item', 
    'cat' => 0,
    'posts_per_page' => $number,
  )
);

Also, the cat=0 is unnecessary. As near as I can tell, the query is identical with or without it.

share|improve this answer

I'm review your code throughly but the problem is in the wp_query parameter.so please replace this line to my new one

$latest_items = WP_query( 'post_type=ait-dir-item&cat=0&posts_per_page=' . $number )

this will help.I'm sure about that.

share|improve this answer
    
no it won't -> $latest_items = new WP_query( 'post_type=ait-dir-item&cat=0&posts_per_page=' . $number ); –  JMau Jun 11 '13 at 12:26
    
make sure your custom post type has posts. and please use this one after $latest_items echo "<pre>";print_r($latest_items);echo "</pre>"; –  Pradipta Sarkar Jun 11 '13 at 12:31
    
I seriously doubt WP_query will work in this context, it's better using query_posts() plus Charlyem is not a coder so why should he print the query? –  JMau Jun 11 '13 at 12:37
2  
It is certainly not better to use query_posts, @TweetPressFr. You might benefit from reading this Q&A. And in the direction of Pradipta: WP_Query is a class and as such needs to be instantiated using the new keyword. –  Johannes Pille Jun 11 '13 at 14:05
    
I doubt Charlyem knows queries, he's not a coder. So this answer wasn't good enough to me. But still you're right for query_post(), I should have been more careful. –  JMau Jun 11 '13 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.