@niigata_taro

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

ページネーションをクリックしても2ページ目以降1ページ目と同じコンテンツが表示される

Q&A

Closed

解決したいこと

Wordpress(バージョン6.6.1)、使用しているテーマ「SANGO」、パーマリンク設定「基本」
トップページ(index.php)にてカスタム投稿を記事一覧(post-grid.php)を使って表示しているが、ページネーションの2ページ目以降をクリックしても、1ページ目と同じコンテンツが表示されている問題の修正

phpスキル初心者のため、どこに問題があるのか特定できませんでした。よろしければご教示お願い致します。

該当するソースコード

index.php
<?php get_header(); ?>
<?php
if ( is_front_page() ) {
	get_template_part( 'parts/home/featured-header' );}
?>
	<?php get_template_part( 'parts/home/top-header' ); ?>
	<div id="content">
	<div id="inner-content" class="wrap">
		<main id="main">
		<?php
			do_action( 'sng_before_main_content' );
		if ( is_active_sidebar( 'home_top' ) ) {
			dynamic_sidebar( 'home_top' );
		}
		if ( get_option( 'activate_tab' ) ) {
			// タブありの記事一覧
			get_template_part( 'parts/home/post-tab' );
		} else {
    // タブなしの記事一覧(カスタム投稿タイプを表示)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
        'post_type' => 'company',
        'posts_per_page' => 6,
        'paged' => $paged, // ページネーション情報を明示的に設定
    );

    // グローバルな $wp_query を一時的に保存
    global $wp_query;
    $original_query = $wp_query;

    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        // グローバルな $wp_query を一時的に置き換え
        $wp_query = $query;

        get_template_part( 'parts/post-grid', null, array( 'paged' => $paged, 'query' => $query ) );

        // グローバルな $wp_query を復元
        $wp_query = $original_query;
        wp_reset_postdata();
    } else {
        // カスタム投稿タイプの記事なし
        echo '会社情報が見つかりません。'; // 必要に応じてメッセージを変更
    }
}
		if ( is_active_sidebar( 'home_bottom' ) ) {
			dynamic_sidebar( 'home_bottom' );
		}
		?>
		</main>
		<?php get_sidebar(); ?>
	</div>
	</div>
<?php get_footer(); ?>

post-grido.php
<?php
/**
 * 記事一覧表示は以下の2種類(カスタマイザーから設定)
 * 1)横長のタイプ
 * 2) 通常のカードタイプ
 */

// index.php から渡されたクエリ、またはグローバルな $wp_query を使用する
$query = isset( $args['query'] ) ? $args['query'] : $GLOBALS['wp_query'];

if ( $query->have_posts() ) { // クエリ結果をチェック
    $ad            = get_theme_mod( 'ad_infeed' );
    $ad2           = get_theme_mod( 'ad_infeed2' );
    $cat_fields    = sng_get_cat_fields();
    $ad_pos1       = get_theme_mod( 'ad_infeed_pos1', -1 );
    $ad_pos2       = get_theme_mod( 'ad_infeed_pos2', -1 );
    $ad_pos3       = get_theme_mod( 'ad_infeed_pos3', -1 );
    $hide_infeed   = $cat_fields['category_hide_infeed'];
    $ad_enabled    = get_theme_mod( 'enable_ad_infeed', false ) && $hide_infeed !== 'true';
    $ad_pos_array = $ad_enabled ? array( $ad_pos1, $ad_pos2, $ad_pos3 ) : array();
    $className     = 'catpost-cards catpost-cards--column-2';
    if ( get_option( 'mb_2column_layout' ) ) {
        $className .= ' catpost-cards--column-mobile-2';
    }
    $className = apply_filters( 'sng_post_grid_class', $className );

    if ( is_sidelong() ) : // 1)横長
        ?>
    <div class="sidelong">
        <?php
        $i            = 1;
        $shown_count = 0;
        while ( $query->have_posts() ) : // クエリ結果をループ
            if ( $ad2 && is_numeric( array_search( $i + $shown_count, $ad_pos_array ) ) ) {
                ++$shown_count;
                echo "<div class=\"sidelong__article\">$ad2</div>";
            }
            $query->the_post(); // 投稿データをセット
            sng_sidelong_card(); // 必要に応じてカスタム投稿タイプ用のテンプレートパーツを作成または修正
            ++$i;
        endwhile;
        ?>
    </div>
        <?php
    else : // 2)カードタイプ
        ?>
    <div class="<?php echo $className; ?>">
        <?php
        $i            = 1;
        $shown_count = 0;
        while ( $query->have_posts() ) : // クエリ結果をループ
            if ( $ad && is_numeric( array_search( $i + $shown_count, $ad_pos_array ) ) ) {
                ++$shown_count;
                echo "<div class=\"c_linkto\">$ad</div>";
            }
            $query->the_post(); // 投稿データをセット
            sng_normal_card(); // 必要に応じてカスタム投稿タイプ用のテンプレートパーツを作成または修正
            ++$i;
        endwhile;
        ?>
    </div>
        <?php
    endif;
// post-grid.php
sng_page_navi( $query ); // ページネーション (sng_page_navi 関数に $query を渡す)
} else {

} else {
    // 記事なし
    get_template_part( 'content', 'not-found' );
}
wp_reset_postdata(); // 投稿データをリセット
?>
sng_page_navi.php
function sng_page_navi( $query = null ) {
    // $query が渡された場合は、それを使用する
    if ( $query !== null ) {
        // 何もしない (既に $query が設定されている)
    } else {
        // $query が渡されなかった場合は、グローバルな $wp_query を使用する
        global $wp_query;
        $query = $wp_query;
    }

    if ( $wp_query->max_num_pages <= 1 ) {
        return;
    }

    $paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;

    // トップページの場合のベースURLを設定
    if ( is_front_page() ) {
        $base_url = trailingslashit( home_url() ) . '%_%'; 
    } else {
        // カスタム投稿タイプのアーカイブページの場合のベースURLを設定
        $archive_link = get_post_type_archive_link( 'company' ); 
        $base_url = trailingslashit( $archive_link ) . '%_%'; 
    }

    ob_start();
    echo '<nav class="pagination dfont" role="navigation" aria-label="ページネーション">';
    echo paginate_links(
        array(
            'base'      => $base_url,
            'format'    => '?page=%#%', 
            'total'     => $GLOBALS['wp_query']->max_num_pages,
            'current'   => $paged,
            'end_size'  => 1,
            'mid_size'  => 1,
            'prev_text' => '<i class="fa fa-chevron-left"></i>',
            'next_text' => '<i class="fa fa-chevron-right"></i>',
            'type'      => 'list',
            'add_args'  => array(), 
        )
    );
    echo '</nav>';
    $html = ob_get_clean();
    $html = str_replace( 'class="next page-numbers"', 'class="next page-numbers" aria-label="次へ"', $html );
    $html = str_replace( 'class="prev page-numbers"', 'class="prev page-numbers" aria-label="前へ"', $html );
    echo $html;
}

0 likes

No Answers yet.

Your answer might help someone💌