警告:未测试)public function renderPosts( $atts )
{
$attributes = shortcode_atts( array(
\'foo\' => 5, //default of 5
\'include\' => \'news-views\', // Currently news and views.
\'exclude\' => \'game-library\' // Exclude this category
), $atts);
/*
* For getting the Query variable on a static front page, you have
* to use \'page\' and not \'paged\'. Weird.
*/
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
/*
* Args for the custom query
*/
$query_args = array(
\'posts_per_page\' => intval($attributes[\'foo\']),
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => explode( \',\', str_replace( \' \', \'\', $attributes[\'include\'] ) ),
\'include_children\' => false
),
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => explode( \',\', str_replace( \' \', \'\', $attributes[\'exclude\'] ) ),
\'operator\' => \'NOT IN\'
)
),
\'paged\' => $paged
);
$custom_query = new WP_Query($query_args);
$output = \'\';
if ( $custom_query->have_posts() ) {
$output .= \'<div id="blogroll">\';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
$output .= "<div class=\'home_post col span_12 clear-both\'>";
$output .= "<div class=\'col span_3\'><a href=\'" . get_the_permalink() . "\'>" . get_the_post_thumbnail(get_the_ID(), \'home_post_thumb\') . "</a></div>";
$output .= "<div class=\'col span_9 col_last right-edge\'>";
$output .= "<h2 class=\'home_post_header\'>";
$output .= \'<a href="\' . get_the_permalink() . \'">\' . get_the_title() . "</a>";
$output .= "</h2>";
$output .= get_the_excerpt();
$output .= \'<a class="home-more-link" href="\' . get_the_permalink() . \'"><span class="continue-reading">Read More</span></a>\';
$output .= "</div>";
$output .= "</div>";
}
wp_reset_postdata();
$output .= \'<div id="pagination" class="blogroll-pagination">\' . home_pagination( $custom_query ) . \'</div></div>\';
}
return $output;
}
请记住,我已经更改了可读性的属性,include
采用逗号分隔的类别字符串slugs (include=\'slug-1, slug-2\'
)。此属性将用于包括类别。exclude
工作原理相同(exclude=\'slug-1, slug-2\'
),但它采用逗号分隔的类别字符串slugs编辑
我已经测试了我的代码并修复了几个小错误。如果我只是从它创建一个普通的短代码,它就会像预期的那样工作。概念验证-短代码
add_shortcode( \'testcode\', function ( $atts )
{
$attributes = shortcode_atts( array(
\'foo\' => 5, //default of 5
\'include\' => \'news-views\', // Currently news and views.
\'exclude\' => \'game-library\' // Exclude this category
), $atts);
/*
* For getting the Query variable on a static front page, you have
* to use \'page\' and not \'paged\'. Weird.
*/
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
/*
* Args for the custom query
*/
$query_args = array(
\'posts_per_page\' => intval($attributes[\'foo\']),
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => explode( \',\', str_replace( \' \', \'\', $attributes[\'include\'] ) ),
\'include_children\' => false
),
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => explode( \',\', str_replace( \' \', \'\', $attributes[\'exclude\'] ) ),
\'operator\' => \'NOT IN\'
)
),
\'paged\' => $paged
);
$custom_query = new WP_Query($query_args);
$output = \'\';
if ( $custom_query->have_posts() ) {
$output .= \'<div id="blogroll">\';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
$output .= "<div class=\'home_post col span_12 clear-both\'>";
$output .= "<div class=\'col span_3\'><a href=\'" . get_the_permalink() . "\'>" . get_the_post_thumbnail(get_the_ID(), \'home_post_thumb\') . "</a></div>";
$output .= "<div class=\'col span_9 col_last right-edge\'>";
$output .= "<h2 class=\'home_post_header\'>";
$output .= \'<a href="\' . get_the_permalink() . \'">\' . get_the_title() . "</a>";
$output .= "</h2>";
$output .= get_the_excerpt();
$output .= \'<a class="home-more-link" href="\' . get_the_permalink() . \'"><span class="continue-reading">Read More</span></a>\';
$output .= "</div>";
$output .= "</div>";
}
wp_reset_postdata();
$output .= \'<div id="pagination" class="blogroll-pagination">\' . home_pagination( $custom_query ) . \'</div></div>\';
}
return $output;
});
我使用如下[testcode include=\'testslug-1, testslug-2\' exclude=\'testslug-3, testslug-4\']
我也测试了您的分页功能,它也按预期工作。function home_pagination( $query = null )
{
$big = 999999999; // need an unlikely integer
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
$pagination = paginate_links(
array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => $paged,
\'total\' => $query->max_num_pages,
\'prev_text\' => \'« Previous\',
\'next_text\' => \'Next »\',
)
);
return $pagination;
}
编辑你的评论,正如我已经说过的,静态首页和单页使用get_query_var( \'page\' )
用于分页,而所有其他用途get_query_var( \'paged\' )
. 我已经用以下代码更新了上面的所有代码if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
这将对问题进行排序page
和paged
并使您的短代码和分页可以在所有页面上工作,而无需对其进行任何特定更改编辑3
Here is a sligtly modified version of the code by @ChipBennet 这将解决以下问题/page/2
function home_pagination( $query = null )
{
global $wp_rewrite;
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
$pagination = array(
\'base\' => @add_query_arg( \'paged\', \'%#%\' ),
\'format\' => \'\',
\'current\' => $paged,
\'total\' => $query->max_num_pages,
\'prev_text\' => \'« Previous\',
\'next_text\' => \'Next »\',
);
if ( $wp_rewrite->using_permalinks() )
$pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg( \'s\', get_pagenum_link( 1 ) ) ).\'/%#%/\', \'\' );
if ( ! empty( $wp_query->query_vars[\'s\'] ) )
$pagination[\'add_args\'] = array( \'s\' => get_query_var( \'s\' ) );
return paginate_links( $pagination );
}