サポート » その他 » 記事一覧のリンク先が全部同じページ

  • 解決済 mayu0112

    (@mayu0112)


    記事一覧のパーマリンクは各々別々のurlを取得しているのですが、リンク先に行くと全ページ同じリンク先に飛びます。
    記述の間違いなのかがわからず困っています。

    home.php

    <?php get_header();?>
    
    <main class="main" id="main">
        <?php
        if ( have_posts() ) {
        while ( have_posts() ) {
                the_post(); ?>
        
        <div class="post">
            <div class="post_img">
            <a href="<?php the_permalink();?>">
                <?php the_post_thumbnail();?></a>
            </div>
            <div class="post_code">
                (<?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?>)
                <?php the_title();?>
            </div>
        </div>     
        
           
        <?php        } // end while
            } // end if
        ?>
    
    </main>
    <aside class="aside2">
        <?php if(is_active_sidebar('widget1')) : ?>
                <ul id="aside_widget">
                <?php dynamic_sidebar('widget1'); ?>
                </ul>
    	<?php endif; ?>
    </aside>
    
    <?php get_footer();?>

    index.php

    <?php get_header();?>
    <main class="main" id="main">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        
        <div class="post">
            <div class="post_img">
                <?php the_post_thumbnail();?>
            </div>
            <div class="post_code">
                (<?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?>)
                <?php the_title();?>
            </div>
            
        </div>
        <div class="post_content">
                <?php the_content();?>
            </div>     
        
           
    <?php endwhile; else : ?>
    <?php endif; ?>
    
    </main>
    <aside class="aside2">
        <?php if(is_active_sidebar('widget1')) : ?>
                <ul id="aside_widget">
                <?php dynamic_sidebar('widget1'); ?>
                </ul>
    	<?php endif; ?>
    </aside>
    
    <?php get_footer();?>

    ヘルプの必要なページ: [リンクを見るにはログイン]

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • こんにちは

    home.php、index.php より前に実行されるコード(header.php や functions.php、プラグインなど)のどこかに問題(wp_reset_postdata() の記入漏れとか・・・)があると思われます。
    header.php はどのようなコードでしょうか?

    トピック投稿者 mayu0112

    (@mayu0112)

    ありがとうございます。header。PHPは以下です。

    <!DOCTYPE html>
    <html lang="ja">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" >
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta property="og:title" content="<?php echo bloginfo( 'name' );?>" />
        <meta property="og:description" content="<?php
            if(is_home()){
                echo bloginfo('description'); //ブログ説明表示
            }else{
                if( is_page( 'memo , portfolio , contact' )){
                    echo category_description();//カテゴリ説明表示
                }else{
                    if(mb_strlen($post->post_content,'UTF-8')>120){
                        $content = mb_substr($post->post_content,0,120,'UTF-8');
                        echo $content;//120文字を記事から表示
                    }
                }
            }
        ?>" />
        <meta property="og:type" content="" />
        <meta property="og:url" content="<?php echo (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];?>" />
        <meta property="og:image" content="<?php 
            if(is_home()){
                echo get_template_directory_uri().'/2_img/home_img.png';//is_homeでhomeの時のアイキャッチurl
            }else if(is_page('memo')){
                echo get_template_directory_uri().'/2_img/page_memo.png';//is_page('memo')でカテゴリーmemoのアイキャッチurl
            }else if(is_page('portfolio')){
                echo get_template_directory_uri().'/2_img/page_portfolio.png';//is_page('portfolio')でカテゴリーportfolioのアイキャッチurl
            }else if(is_page('contact')){
                echo get_template_directory_uri().'/2_img/page_contact.png';//is_page('contact')でカテゴリーcotntactのアイキャッチurl
            }else{
                $args = array();
                $posts = get_posts( $args );
                foreach ( $posts as $post ) : setup_postdata( $post );
                    $image_id = get_post_thumbnail_id( $post->ID );
                    $image = wp_get_attachment_image_src( $image_id );
                    echo $image[0];
                endforeach; wp_reset_postdata();
            }
        ?>" />
        <meta property="og:site_name" content="<?php echo bloginfo( 'name' );?>" />
        <meta property="og:locale" content="ja_JP"  />
        <meta content="
        <?php
            if(is_home()){
                echo bloginfo('description'); //ブログ説明表示
            }else{
                if( is_page( 'memo , portfolio , contact' )){
                    echo category_description();//カテゴリ説明表示
                }else{
                    if(mb_strlen($post->post_content,'UTF-8')>120){
                        $content = mb_substr($post->post_content,0,120,'UTF-8');
                        echo $content;//120文字を記事から表示
                    }
                }
            }
        ?>
        " name="description">
        <title>
            <?php if(is_home()){
            //homeの時ブログタイトルを表示、ホーム以外は記事タイトルとブログ名を表示
                   echo bloginfo( 'name' );
                }else{
                    the_title(); echo '/' ; echo bloginfo( 'name' );
                }
            ?>
        </title>
        <?php wp_head();?>
    </head>
    <body>
    <header class="header">
        <div class="menu_button">menu</div>
        <div class="logo">
            <a href="<?php echo esc_url( home_url() ); ?>"><img src="<?php echo get_template_directory_uri().'/2_img/logo.svg'?>" alt=""></a>
        </div>
        <nav class="nav1">
        <?php
              wp_nav_menu(array (
                  'theme_location' => 'main-menu',
                  'items_wrap' => '<ul>%3$s</ul>',
                  'container' => false
              ) );
            ?>
            <aside class="aside">
                <?php if(is_active_sidebar('widget1')) : ?>
                <ul id="aside_widget1">
                <?php dynamic_sidebar('widget1'); ?>
                </ul>
                <?php endif; ?>
            </aside>
            <footer class="footer1">
                <p>© mayudesign  <?php echo date('Y');?></p>
            </footer>
        </nav>
        <nav class="nav2">
        <?php
              wp_nav_menu(array (
                  'theme_location' => 'main-menu',
                  'items_wrap' => '<ul>%3$s</ul>',
                  'container' => false
              ) );
            ?>
        </nav>
    </header>

    header.php で投稿情報配列が格納されているグローバル変数 $posts が書き換えられています。
    $posts は wp_reset_postdata() ではリセットされません。変数名を変更してみてください。

    $posts = get_posts( $args );
    foreach ( $posts as $post ) : setup_postdata( $post );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    トピック投稿者 mayu0112

    (@mayu0112)

    お教えいただいた箇所を直したtころ正常に表示されました。
    次回から気を付けたいとおもいます。
    ありがとうございます。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「記事一覧のリンク先が全部同じページ」には新たに返信することはできません。