最近自定义类型帖子的快捷代码

时间:2013-06-11 作者:Charlyem

我不是一个编码员,所以我可能犯了一些“编码”罪行,抱歉。我买了一个主题,它有最后一篇文章的短代码,但没有最后一篇“自定义文章类型”。因此,我尝试创建last“custom post type”函数,修改提供的last post函数。上次发布功能:

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" );
然后我尝试这样修改它,(我的自定义帖子类型是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" );
当然,这个函数不起作用。。。有人能帮我吗?我想修改这个函数,而不是安装插件,因为我想使用相同的类,所以这两个短代码的设计都相同。提前感谢!

3 个回复
最合适的回答,由SO网友:JMau 整理而成

这似乎适用于自定义帖子类型:

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" );
要使用它,只需写[get_posts cpt="ait-dir-item"] 在您的帖子中

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

SO网友:s_ha_dum

在我看来,问题在于这一行:

$latest_items = WP_query( \'post_type\' = \'ait-dir-item\', \'cat=0&posts_per_page=\' . $number );
这很奇怪。首先,你错过了new 关键字。如果没有它,就会出现“未定义函数”错误。

第二,使用WP_Query, 和许多其他功能,您可以使用query-string-like function argument, 或数组。你有(破坏)两者的元素,这是行不通的。

对于类似查询字符串的参数,您需要:

$latest_items = new WP_query(\'post_type=ait-dir-item&cat=0&posts_per_page=\'.$number);
看看这是怎么一个字符串,而你的不是?有三个不同的字符串= 松散浮动,以及一个逗号,表示您实际使用的是两个参数,而不仅仅是一个参数。如果你有debugging enabled 您将看到错误。

但使用数组。这些类似于查询字符串的参数很难读取、编辑,因此容易出错。至少这是我的看法。

$latest_items = new WP_query( 
  array(
    \'post_type\' => \'ait-dir-item\', 
    \'cat\' => 0,
    \'posts_per_page\' => $number,
  )
);
此外cat=0 是不必要的。就我所知,无论有没有它,查询都是相同的。

SO网友:Pradipta Sarkar

我正在彻底检查您的代码,但问题出在wp\\u查询参数中。所以请把这一行换成我的新行

$latest_items = WP_query( \'post_type=ait-dir-item&cat=0&posts_per_page=\' . $number )
这将有所帮助。我确信这一点。

结束

相关推荐

Shortcode of a function

我得到了这个函数,我想将“foreach”的contents divs作为return。我该怎么做?<?php $args = array( \'numberposts\' => 6, \'post_status\'=>\"publish\",\'post_type\'=>\"post\",\'orderby\'=>\"post_date\"); $postslist = get_posts( $args ); fo