- 某自訂分類的文章在前台只能呈現9篇,其他則仍是後台設定的呈現篇數
查了半天,只要在theme中的functions.php(或其他需要處)加入
function modify_showcase_posts_per_page($wp_query) {
if( get_query_var('showcase_cat_1') == null ) return;
$wp_query->query_vars['posts_per_page'] = 9;
}
add_filter( 'pre_get_posts', 'modify_showcase_posts_per_page' );
註:pre_get_posts在Plugin API/Action Reference/pre get posts有提到,原文如下:
This hook is called after the query variable object is created, but before the actual query is run.
The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object - no return value is necessary).
沒有留言:
張貼留言