PHPの初心者で、ワードプレスのプラグイン「anspress」を使ってQ&Aサイトを立ち上げています。
タグのページネーションが上手く作れず困っています。
アドオンのタグの一覧ページを作成するコードが
$tag_args = apply_filters( 'ap_tags_shortcode_args', $tag_args ); $query = new \WP_Term_Query( $tag_args ); // Count terms. $tag_args['fields'] = 'count'; $found_query = new \WP_Term_Query( $tag_args ); $tags_rows_found = $found_query->get_terms();** $ap_max_num_pages = ceil( count( $tags_rows_found ) / $per_page ); $question_tags = $query->get_terms(); include ap_get_theme_location( 'addons/tag/tags.php' );
となっており、
「$ap_max_num_pages = ceil( count( $tags_rows_found ) / $per_page );」
の部分に以下のワーニングが出ていました。
Warning: count(): Parameter must be an array or an object that implements Countable in
そのため、ワーニング部分を以下に変更しました。
$total_terms = wp_count_terms( '$question_tags', array( 'hide_empty' => false, 'parent' => 0, ) ); $ap_max_num_pages = ceil( $total_terms / $per_page );
その結果、ワーニングは消え、ページネーションは作れたのですが、何も表示されないページが複数ページできました。
ページネーションを作る時のcount()のワーニングの対応方法や空欄のページの消し方などわかる方がいらっしゃいましたらアドバイスをお願いします。
---------------コードの全文は以下になります。------------
public function tags_page() { global $question_tags, $ap_max_num_pages, $ap_per_page, $tags_rows_found; $paged = max( 1, get_query_var( 'paged' ) ); $per_page = (int) ap_opt( 'tags_per_page' ); $per_page = 0 === $per_page ? 1 : $per_page; $offset = $per_page * ( $paged - 1 ); $tag_args = array( 'taxonomy' => 'question_tag', 'ap_tags_query' => true, 'parent' => 0, 'number' => $per_page, 'offset' => $offset, 'hide_empty' => false, 'order' => 'DESC', ); $ap_sort = ap_isset_post_value( 'tags_order', 'count' ); if ( 'new' === $ap_sort ) { $tag_args['orderby'] = 'id'; $tag_args['order'] = 'DESC'; } elseif ( 'name' === $ap_sort ) { $tag_args['orderby'] = 'name'; $tag_args['order'] = 'ASC'; } else { $tag_args['orderby'] = 'count'; } if ( ap_isset_post_value( 'ap_s' ) ) { $tag_args['search'] = ap_sanitize_unslash( 'ap_s', 'r' ); } /** * Filter applied before getting tags. * * @var array */ $tag_args = apply_filters( 'ap_tags_shortcode_args', $tag_args ); $query = new \WP_Term_Query( $tag_args ); // Count terms. $tag_args['fields'] = 'count'; $found_query = new \WP_Term_Query( $tag_args ); $tags_rows_found = $found_query->get_terms(); //$ap_max_num_pages = ceil( count( $tags_rows_found ) / $per_page ); // ← この行がエラーとなった //ここから変更 $total_terms = wp_count_terms( '$question_tags', array( 'hide_empty' => false, 'parent' => 0, ) ); $ap_max_num_pages = ceil( $total_terms / $per_page ); $order = ap_opt( 'tags_page_order' ) === 'ASC' ? 'ASC' : 'DESC'; $cat_args = array( 'parent' => 0, 'number' => $per_page, 'offset' => $offset, 'hide_empty' => false, 'orderby' => ap_opt( 'tags_page_orderby' ), 'order' => $order, ); //ここまで追記 $question_tags = $query->get_terms(); include ap_get_theme_location( 'addons/tag/tags.php' ); }
あなたの回答
tips
プレビュー