我不是一个编码员,所以我可能犯了一些“编码”罪行,抱歉。我买了一个主题,它有最后一篇文章的短代码,但没有最后一篇“自定义文章类型”。因此,我尝试创建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 = \' <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 = \' <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" );
当然,这个函数不起作用。。。有人能帮我吗?我想修改这个函数,而不是安装插件,因为我想使用相同的类,所以这两个短代码的设计都相同。提前感谢!
最合适的回答,由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 = \' <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