if ( isset( $additional_loop ) ){
此检查是不必要的,因为$additional_loop
是始终存在的查询变量这是您的代码。我做了一些改变,其他的我刚刚评论过。有关更多详细信息,请参阅我在代码中的注释
/*
* Your function name is wrong, should be something like recent_blog
*/
function RecentBlog($atts, $content = null) {
/*
* This is the proper set up of attributes.
* Use your attributes as follow: $attributes[\'comments\']
*/
$attributes = shortcode_atts(array(
"comments" => \'true\',
"date" => \'true\',
"columns" => \'4\',
"limit" => \'-1\',
"title" => \'true\',
"description" => \'true\',
"cat_slug" => \'\',
"post_type" => \'\',
"excerpt_length" => \'15\',
"readmore_text" => \'\',
"pagination" => \'false\'
), $atts);
// I don\'t know about this, does not make sense
global $post;
// This is a bug, will produce a undefined variable notice. Turn debug on
$postformat = get_post_format();
....
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
// Your query here is wrong. Use WP_Query. Something like below
$args = array(
\'posts_per_page\' => $attributes[\'limit\'],
\'category_name\' => $attributes[\'cat_slug\'],
\'paged\' => $paged,
//etc
);
$additional_loop = new WP_Query( $args );
/*
* Give each condition its own line. Also use curlies
* Do not use `query_posts`. Set your loop to your query variable which seems
* to be $additional_loop
*/
if ($additional_loop->have_posts()) {
while ($additional_loop->have_posts()) {
$additional_loop->the_post();
$postformat = get_post_format();
if( $postformat == "" ) $postformat="standard";
$protected = "";
$portfoliogrid .= \'<div class="\' . $column_no . \' post grid-post post-\' . $postformat . \'">\';
.....////
$portfoliogrid .= \'<div class="summary-info">\';
.......
$portfoliogrid .=\'</div>\';
/*
* Change all your attributes accordingly like I did below
* $title becomes $attributes[\'title\']
* $description becomes $attributes[\'description\']
*/
// If either of title and description needs to be displayed.
if ( $attributes[\'title\'] == "true" || $attributes[\'description\'] == "true" ) {
$portfoliogrid .=\'<div class="work-details">\';
......
$portfoliogrid .=\'</div>\';
}
$portfoliogrid .=\'</div>\';
}
}
/*
* Change attributes accordingly to new attribute syntax without extract()
* $pagination becomes $attributes[\'pagination\']
*/
if ( $attributes[\'pagination\'] == "true" ){
/*
* Do you really need this condition. $additional_loop will always exists as this is your
* set query variable
*/
if ( isset( $additional_loop ) ){
/*
* Do not echo your output. Concatenate it to your variable to be returned later
*/
$portfoliogrid .= suareztheme_pagination( $additional_loop->max_num_pages );
} else {
$portfoliogrid .= suareztheme_pagination();
}
/*
* This seems all wrong. Don\'t know what is your intention here
*/
if ( function_exists("suareztheme_pagination") ) {
} else {
/*
* use the get_* prefix functions here and return them
*/
$portfoliogrid .= get_next_posts_link(\'«« Older Posts\');
$portfoliogrid .= get_previous_posts_link(\'Newer Posts »»\');
}
}
/*
* This should be wp_reset_postdata(). wp_reset_query() is used with query_posts
* which you should never use*/
wp_reset_query();
return $portfoliogrid;
}
/*
* Change function call back name according to your new function name
*/
add_shortcode( "recentblog", "RecentBlog" );
编辑2下面是一个完整的短码示例。在粘贴代码之前,只需做几点注释我还没有测试代码。您不应该直接将其粘贴到活动站点中,首先在本地安装上测试它
我已经对我所做的一切进行了评论,在测试代码之前,请比较我的代码和您的代码,确保您知道我所做的一切。完成后,如果愿意,可以删除注释
一个非常重要的注意事项,comments_popup_link
回显其输出,该输出在短代码中不起作用。Wordpress中还没有返回其输出的替代函数。站点成员之一@Otto编写了一个自定义函数get_comments_popup_link
(您可以在his personal site) 来克服这个问题。您需要在函数中复制并粘贴该函数。在添加短代码之前使用php。这是来自@Otto的函数
/**
* Modifies WordPress\'s built-in comments_popup_link() function to return a string instead of echo comment results
*/
function get_comments_popup_link( $zero = false, $one = false, $more = false, $css_class = \'\', $none = false ) {
global $wpcommentspopupfile, $wpcommentsjavascript;
$id = get_the_ID();
if ( false === $zero ) $zero = __( \'No Comments\' );
if ( false === $one ) $one = __( \'1 Comment\' );
if ( false === $more ) $more = __( \'% Comments\' );
if ( false === $none ) $none = __( \'Comments Off\' );
$number = get_comments_number( $id );
$str = \'\';
if ( 0 == $number && !comments_open() && !pings_open() ) {
$str = \'<span\' . ((!empty($css_class)) ? \' class="\' . esc_attr( $css_class ) . \'"\' : \'\') . \'>\' . $none . \'</span>\';
return $str;
}
if ( post_password_required() ) {
$str = __(\'Enter your password to view comments.\');
return $str;
}
$str = \'<a href="\';
if ( $wpcommentsjavascript ) {
if ( empty( $wpcommentspopupfile ) )
$home = home_url();
else
$home = get_option(\'siteurl\');
$str .= $home . \'/\' . $wpcommentspopupfile . \'?comments_popup=\' . $id;
$str .= \'" onclick="wpopen(this.href); return false"\';
} else { // if comments_popup_script() is not in the template, display simple comment link
if ( 0 == $number )
$str .= get_permalink() . \'#respond\';
else
$str .= get_comments_link();
$str .= \'"\';
}
if ( !empty( $css_class ) ) {
$str .= \' class="\'.$css_class.\'" \';
}
$title = the_title_attribute( array(\'echo\' => 0 ) );
$str .= apply_filters( \'comments_popup_link_attributes\', \'\' );
$str .= \' title="\' . esc_attr( sprintf( __(\'Comment on %s\'), $title ) ) . \'">\';
$str .= get_comments_number_str( $zero, $one, $more );
$str .= \'</a>\';
return $str;
}
最后,这里是您的新短代码(希望可以使用)。我希望这能解决您的一些主要问题:-)function recent_blog_shortcode($atts, $content = null) {
/*
* Do not wrap booleans and integers in quotes. If they are wrapped in quotes
* they are interpreted as string values. \'true\' !== true. First one is a string
* while the second is a boolean
*
* Also, stick with single quotes (or double quotes), don\'t mix them, it is confusing
* when it comes to debugging
*
* Do not use extract() as I have already explained
*/
$attributes = shortcode_atts(array(
\'comments\' => true,
\'date\' => true,
\'vote\' => true,
\'columns\' => 4,
\'limit\' => -1,
\'title\' => true,
\'description\' => true,
\'cat_slug\' => \'\',
\'post_type\' => \'\',
\'excerpt_length\' => 15,
\'readmore_text\' => \'\',
\'pagination\' => false
), $atts);
/*
* Stay out of the global scope. It is not very good practise.
*/
global $post;
//$portfoliogrid, <-- Bad name for a global variable, too easy
//$column_no; <-- Bad name for a global variable, too easy
/*
* As precaution, check if $attributes[\'columns\'] is not empty. Also set the $columns variable from
* our attributes
*/
$columns = $attributes[\'columns\'];
/*
* Sets the variables to make sure your two variables is not undefined (bug)
* if $columns are empty
*/
$column_no = \'\';
$portfolioImage_type = \'\',
if ( $columns ) {
if ( $columns == 4 ) {
$column_no = \'col-md-3\';
$portfolioImage_type = \'gridblock-medium\';
}
if ( $columns == 3 ) {
$column_no = \'col-md-4\';
$portfolioImage_type = \'gridblock-large\';
}
if ( $columns == 2 ) {
$column_no = \'col-md-6\';
$portfolioImage_type = \'gridblock-large\';
}
if ( $columns == 1 ) {
$column_no = $columns;
$portfolioImage_type = \'gridblock-full\';
}
}
/*
* Again, stay with single quotes OR double quotes, going with single quotes
*/
$postformats = \'\';
$terms = \'\';
$portfoliogrid = \'\';
/*
* Great job here
*/
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
/*
* Change $post_type to our new attribute system, $attributes[\'post_type\']
* For ease, just set the variable $post_type to the attribute
*/
$post_type = $attributes[\'post_type\'];
if ( $post_type = \'\' ) {
$type_explode = explode( \',\', $post_type );
foreach ( $type_explode as $postformat ) {
$count++;
$postformat_slug = "post-format-" . $postformat;
$terms[] .= $postformat_slug;
}
/*
* Never ever use query_posts. It breaks page functionalities, pagination
* and the main query. Very bad function to use, please avoid it. Use WP_Query
* instead.
*
* Make your arguments conditional, not your query. Change our variables here to use the
* new attribute system without extract()
*/
$args = array(
\'category_name\' => $attributes[\'cat_slug\'],
\'posts_per_page\' => $attributes[\'limit\'],
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => $terms
)
)
);
} else {
/*
* Never ever use query_posts. It breaks page functionalities, pagination
* and the main query. Very bad function to use, please avoid it. Use WP_Query
* instead.
*
* Make your arguments conditional, not your query. Change our variables here to use the
* new attribute system without extract()
*/
$args = array(
\'category_name\' => $attributes[\'cat_slug\'],
\'paged\' => $paged,
\'posts_per_page\' => $attributes[\'limit\'],
);
}
/*
* Create our query with WP_Query, very good practice
*/
$q = new WP_Query( $args );
$suareztheme_pagestyle = get_post_meta( $post->ID, \'pagestyle\', true );
/*
* Each condition on a new line, and use curlies
* Set our loop to our custom query. VERY IMPORTANT
*/
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$postformat = get_post_format();
if( $postformat == \'\' )
$postformat = \'standard\';
$protected = \'\';
$portfoliogrid .= \'<div class="\' . $column_no . \' post grid-post post-\' . $postformat . \'">\';
//if Password Required
/*
* Where is MTHEME_PATH defined, this is a bug if MTHEME_PATH does not exists
*/
if ( post_password_required() ) {
$protected = \' portfolio-protected\';
$iconclass = \'\';
$portfoliogrid .= \'<a class="grid-blank-element \'. $protected .\' gridblock-image-link gridblock-columns" title="\'. get_the_title() .\'" href="\'.get_permalink().\'" >\';
$portfoliogrid .= \'<span class="grid-blank-status"><i class="icon-lock icon-2x"></i></span>\';
$portfoliogrid .= \'<div class="portfolio-protected"><img src="\'. MTHEME_PATH .\'/images/icons/blank-grid.png" /></div>\';
} else {
// if ( ! has_post_thumbnail() ) {
// $portfoliogrid .= \'<a class="grid-blank-element \'.$protected.\' gridblock-image-link gridblock-columns" title="\'.get_the_title().\'" href="\'.get_permalink().\'" >\';
// $portfoliogrid .= \'<span class="grid-blank-status"><i class="\'.$postformat_icon.\' icon-2x"></i></span>\';
// $portfoliogrid .= \'<div class="gridblock-protected"><img src="\'.MTHEME_PATH.\'/images/icons/blank-grid.png" /></div>\';
// }
if ( has_post_thumbnail() ) {
//Make sure it\'s not a slideshow
//Switch check for Linked Type
$portfoliogrid .= \'<a class="post-image" href="\' . get_permalink() . \'" rel="bookmark" title="\' . get_the_title(). \'">\';
// If it aint slideshow then display a background. Otherwise one is active in slideshow thumbnails.
// Custom Thumbnail
// Display Image
/*
* suareztheme_display_post_image is a custom function. You should first check
* if it exists, because if it does not, you will crash your site-content
*/
if ( function_exists( \'suareztheme_display_post_image\' ) {
$portfoliogrid .= suareztheme_display_post_image (
get_the_ID(),
$have_image_url = \'\',
$link = false,
$type = $portfolioImage_type,
$imagetitle = \'\',
$class = \'preload-image displayed-image\'
);
}
/*
* else if should be elseif. else if !== elseif
*/
} elseif ( $postformat == \'gallery\' ){
/*
* the_post_format_gallery_content() is a custom function. You should first check
* if it exists, because if it does not, you will crash your site-content
*/
if ( function_exists( \'the_post_format_gallery_content\' )
$portfoliogrid .= the_post_format_gallery_content();
} else if ( $postformat == \'video\' || $postformat == \'audio\' ){
$embed_audio_code = get_post_meta($post->ID, \'audio_embed_code\', true);
$embed_video_code = get_post_meta($post->ID, \'video_embed_code\', true);
if ( $embed_audio_code ){
$portfoliogrid .= \'<div>\' . $embed_audio_code . \'</div>\';
} else {
$portfoliogrid .= \'<div>\' . $embed_video_code . \'</div>\';
}
} else if ( $postformat == "quote" ){
$portfoliogrid .= \'<div class="quote">\';
$portfoliogrid .= \'<p class="quote-say">\' . get_post_meta($post->ID, \'meta_quote\', true) . \'</p>\';
if ( $quote_author != \'\' ){
$portfoliogrid .= \'<p class="quote-author">- \' . get_post_meta($post->ID, \'meta_quote_author\', true) . \'</p>\';
}
$portfoliogrid .= \'</div>\';
} else {
$portfoliogrid .= \'<a class="\'. $protected .\' gridblock-image-link gridblock-columns" title="\'. get_the_title() .\'" href="\'. get_permalink() .\'" >\';
$portfoliogrid .= \'<div class="post-nothumbnail"></div>\';
}
}
$portfoliogrid .= \'</a>\';
/*
* Don\'t wrap booleans in quotes as described earlier
* Also set our new attribute scheme
*/
if ( $attribute[\'date\'] == true ){
$portfoliogrid .= \'<span class="date">\' . esc_html( get_the_date(\'F jS, Y\') ) . \'</span>\';
}
// If either of title and description needs to be displayed.
if ( $title == "true" ) {
$portfoliogrid .=\'<div class="entry-header">\';
$hreflink = esc_url( get_permalink() );
/*
* Don\'t wrap booleans in quotes as described earlier
* Also set our new attribute scheme
*/
if ( $attribute[\'title\'] == true && $postformat == \'link\' ) {
$portfoliogrid .=\'<h4><a href="\' . esc_url( get_post_meta( $post->ID, \'meta_link\', true ) ) . \'" rel="bookmark" title="\' . get_the_title() . \'">\'. get_the_title() .\'</a></h4>\';
} else {
$portfoliogrid .=\'<h4><a href="\' . $hreflink . \'" rel="bookmark" title="\' . get_the_title() . \'">\'. get_the_title() .\'</a></h4>\';
}
$portfoliogrid .=\'</div>\';
}
/*
* Don\'t wrap booleans in quotes as described earlier
* Also set our new attribute scheme
*/
if ( $attribute[\'description\'] == true ) {
/*
* Set our new attribute scheme
*
* suareztheme_excerpt_limit is a custom function. You should first check
* if it exists, because if it does not, you will crash your site-content
*/
if ( function_exists( \'suareztheme_excerpt_limit\' ) {
$summary_content = esc_attr( suareztheme_excerpt_limit($attribute[\'excerpt_length\']) );
$portfoliogrid .= \'<div class="entry-content"><p>\'. $summary_content .\'</p></div>\';
}
}
/*
* Don\'t wrap booleans in quotes as described earlier
* Set our new attribute scheme
*/
if ( $attribute[\'readmore_text\'] != \'\'
|| $attribute[\'comments\'] == true
|| $attribute[\'vote\'] == true
) {
$portfoliogrid .= \'<div class="entry-links">\';
$portfoliogrid .= \'<div class="entry-links-left">\';
/*
* dot_irecommendthis() is a custom function. You should first check
* if it exists, because if it does not, you will crash your site-content
*
* Make sure dot_irecommendthis() returns its output, and not echo
*/
if ( function_exists( \'dot_irecommendthis\' )
$portfoliogrid .= dot_irecommendthis();
/*
* comments_popup_link echos its output. There is no version that returns it output
* Needs a workaround. See this article by Otto and use that function as a workaround
* Link to article http://www.thescubageek.com/code/wordpress-code/add-get_comments_popup_link-to-wordpress/
* Replace comments_popup_link with get_comments_popup_link and first check if the function
* exists. Remember to copy the function from the link from Otto to functions.php
*/
if ( function_exists( \'get_comments_popup_link\' )
$portfoliogrid .= \'<span class="entry-comments"><i class="fa fa-comments"></i>\' . get_comments_popup_link(\'Leave a Reply\', \'1\', \'%\') . \'</span>\';
$portfoliogrid .= \'</div>\';
$portfoliogrid .= \'<div class="entry-links-right">\';
$portfoliogrid .= \'<a href="\' . $hreflink . \'" class="btn-readmore">\' . $attribute[\'readmore_text\'] . \' <i class="fa fa-chevron-right"></i></a>\';
$portfoliogrid .= \'</div>\';
$portfoliogrid .= \'</div>\';
}
$portfoliogrid .=\'</div>\';
}
}
/*
* Don\'t wrap booleans in quotes as described earlier
* Set our new attribute scheme
*/
if ( $attribute[\'pagination\'] == true ){
$portfoliogrid .= \'<div class="col-md-12">\';
/*
* Remember to set our maximum pages for the custom query*/
if ( function_exists( \'suareztheme_pagination\' ) ) {
$portfoliogrid .= suareztheme_pagination( $q->max_num_pages );
} else {
$portfoliogrid .= get_next_posts_link( \'«« Older Posts\', $q->max_num_pages );
$portfoliogrid .= get_previous_posts_link( \'Newer Posts »»\' );
}
$portfoliogrid .= \'</div>\';
}
/*
* Do not use wp_reset_query(), it is used with query_posts which you MUST NEVER use
* Use wp_reset_postdata()
*/
wp_reset_postdata();
return $portfoliogrid;
}
add_shortcode( \'recentblog\', \'recent_blog_shortcode\' );