サポート » 使い方全般 » 特定のカテゴリの記事内容を表示させたい

  • 解決済 sasasa24

    (@sasasa24)


    投稿にて、columnというカテゴリを作り、single-column.phpにて
    以下のように記述をしています。

    <?php /* The loop */ ?>
    <?php  while ( have_posts() ) : the_post(); $postID = $post->ID; $title = $post->post_title; ?>
    
    <?php if ( in_category( array( 'column' ) )) : ?>
    <?php $randimg_posts =  get_posts('category_name=column&numberposts=1');
          global $post;
          foreach( $randimg_posts as $post ) : setup_postdata($post); ?>
    
    <h2><?php echo $title; ?></h2>
    <span class="press_date"><?php echo get_the_date(); ?></span>
    <?php echo get_the_post_thumbnail($postID, 'full'); ?>
    <?php the_content(); ?>
    
    <?php endforeach; ?>
    <?php else: ?>
    <?php endif; ?>
    <?php endwhile; ?>

    <?php echo $title; ?>

    <?php echo get_the_post_thumbnail($postID, 'full'); ?>
    の部分で、それぞれの記事のタイトルとアイキャッチ画像はきちんと取得できたのですが、
    <?php echo get_the_date(); ?>

    <?php the_content(); ?>
    にて投稿内容と日付がきちんと取得できず、どのページに移動しても最新記事の投稿内容と日付が表示されてしまいます。

    <?php while ( have_posts() ) : the_post(); $postID = $post->ID; $title = $post->post_title; ?>

    $post->post_date;
    などと追記してみたり、
    <?php echo get_the_post_content($postID); ?>
    などと記述してみましたが何も表示できませんでした。

    どのように訂正すればいいか、ご教授頂けないでしょうか?
    どうぞ宜しくお願い致します。

15件の返信を表示中 - 1 - 15件目 (全18件中)
  • sasasa24 さん

    <?php echo apply_filters( 'the_content', $post -> post_content ); ?>

    でどうでしょうか?

    トピック投稿者 sasasa24

    (@sasasa24)

    mura0403様
    教えて頂いて有難うございます。
    試してみたのですが、表示内容は変わらず最新記事のままでした。
    何か別の問題があるのでしょうか・・・。

    その他の取得は

    //記事を取得
    $post = get_post();//投稿のミックス値
    $post_id = $post->ID;//ID
    $post_slug = $post->post_name;//スラグ
    $post_title = $post->post_title;//タイトル
    $post_content = $post->post_content;//記事
    $post_excerpt = $post->post_excerpt;//概要文
    $post_date = $post->post_date;//投稿日
    $post_date_gmt = $post->post_date_gmt;//投稿日(GMT)
    $post_modified = $post->post_modified;//更新日
    $post_modified_gmt = $post->post_modified_gmt;//更新日(GMT)
    $post_status = $post->post_status;//公開状態 ( publish | pending | draft | private | static | object | attachment | inherit | future )
    $post_password  = $post->post_password;//投稿パスワード
    $post_text_count = $post->post_content_filtered;//記事の文字数
    $post_parent_id = $post->post_parent;//親ID
    $guid = $post->guid;//投稿の絶対URL
    $menu_order = $post->menu_order;//順番
    $post_type = $post->post_type;//投稿タイプ
    $post_mime_type = $post->post_mime_type;//添付ファイルのMIME タイプ
    $comment_count = $post->comment_count;//コメント数
    $post_author  = $post->post_author;//投稿者
    $post_field  = get_post_meta($post_id, 'sub-title', true);//カスタムフィールド取得

    こんな感じでしょうか。

    最新記事の取得は

    global $post;
    これでリセットされてるのが原因ではないでしょうか?

    トピック投稿者 sasasa24

    (@sasasa24)

    mura0403様
    有難うございます。
    global $post;を消してみましたが、変わらず最新記事でした…
    消すだけでは意味がないのでしょうか…?
    質問ばかりで申し訳ございません。

    sasasa24 さん

    ちょっと確認させてください。
    single内に「column」というカテゴリーの記事を表示させたい感じでしょうか?
    このforeachの書き方ですと、
    「column」カテゴリーの最新1件を表示

    という感じだと思うのですが

    現在の記事を表示したいという意味合いでしょうか?

    トピック投稿者 sasasa24

    (@sasasa24)

    mura0403様

    説明不足で申し訳ございません。
    single内(single-column.php)に「column」というカテゴリーの記事を表示させたいです。
    mura0403様の仰る通り、現在の記事を表示したい、ということです。
    宜しくお願い致します。

    sasasa24さん

    foreachを外して、
    get_the_ID();
    で渡してみてはいかがでしょうか。

    もう1つ教えてください
    single-column.php
    にのテンプレート分岐はsingle.phpからIFで決めてる感じでしょうか?

    わたくし的な発想ですが
    single.phpで分岐すれば
    あえて、single-column.php内で
    if ( in_category( array( ‘column’ ) )) :
    をする必要がない気がします

    わたくしの場合
    1.single.phpをコピーし
     single-def.phpを作っておきます。

    2.sigle.phpを以下のソースに書き換えます
    <?php
    if (in_category(column)) {
    include(TEMPLATEPATH . ‘/single-column.php’);
    }

    else {
    include(TEMPLATEPATH . ‘/single-def.php’);
    }
    ?>

    3.single-def.phpをさらにコピーしカスタマイズしたものを「single-column.php」ってしたほうが素直な気がします。

    この辺は結構、作り方次第になってしまいますが
    上記の方法で行えば、基本的に$postから通常指定で取得できると思いますよ

    トピック投稿者 sasasa24

    (@sasasa24)

    有難うございます!
    $nowpage = get_the_ID();
    として
    <?php echo $nowpage; ?>
    と出力したらきちんと取得されたIDが表示されました。
    これを投稿内容にするには、どうしたら良いでしょうか。
    最初に教えて頂いた<?php echo apply_filters( ‘the_content’, $post -> post_content ); ?>ではダメなのでしょうか?

    それと、分岐の件は仰る通りです。
    single.phpで分岐しているので、余計な記述になってしまっていました。
    試行錯誤しながら記述していたので、そこにも気付けませんでした。
    教えて頂いてどうも有難うございます。

    トピック投稿者 sasasa24

    (@sasasa24)

    返信が重なってしまい、申し訳ありません。
    mura0403様の言う方法が確かに簡単ですね。。
    自分でより難しくしてしまってたのですね。。
    今回この記述で統一してしまったのですが、
    徐々にmura0403様の方式へ変えていこうと思います。
    ご指導有難うございます。

    sasasa24 さん

    多分foreach内で取得したデータが全部最新記事になってしまっていますので

    <?php
    while ( have_posts() ) :
    the_post();
    $post = get_post();//現在投稿のミックス値
    $postID = $post->ID;
    $title = $post->post_title;
    $post_date = $post->post_date;//投稿日
    $post_content = apply_filters( 'the_content', $post -> post_content );
    ?>
    
    <?php if ( in_category( array( 'column' ) )) : ?>
    <h2><?php echo $title; ?></h2>
    <span class="press_date"><?php echo $post_date; ?></span>
    <?php echo get_the_post_thumbnail($postID, 'full'); ?>
    <?php $post_content; ?>
    <?php else: ?>
    
    <?php endif; ?>
    
    <?php
    endwhile;
    ?>

    って感じで、単純にforeachを外してみるのがとりあえず、早いですかね。
    すみません、色々と書き込んでしまいまして

    トピック投稿者 sasasa24

    (@sasasa24)

    mura0403様

    すみません、本当にすみませんが
    ここまで細かく教えて頂いたのに表示されませんでした。。

    <?php
    /**
     * The template for displaying all single posts
     *
     * @package WordPress
     * @subpackage Twenty_Thirteen
     * @since Twenty Thirteen 1.0
     */
    
    get_header(); ?>
    
    <div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
    
    <div id="custom_post">
    <div class="container">
    <div class="row">
    <?php /* The loop */ ?>
    <?php while ( have_posts() ) : the_post();
     $post = get_post();//現在投稿のミックス値
     $postID = $post->ID;
     $title = $post->post_title;
     $post_date = $post->post_date;//投稿日
     $post_content = apply_filters( 'the_content', $post -> post_content );
    ?>                            
    
    <?php if ( in_category( array( 'column' ) )) : ?>
    <!-- カスタムフィールド開始-->
    <div class="main-img">
    <span class="cont_title">Column</span>
    <p class="cont_title">コラム</p>
    <div class="col-md-3 col-md-push-9 col-xs-12">
    <div class="left_side">
    <?php $randimg_posts =  get_posts('category_name=column&numberposts=10');
                             global $post;
                             foreach( $randimg_posts as $post ) : ?>
    
    <a href="<?php the_permalink() ?>">
    <?php the_title(); ?></span></a><br/><hr>
    <?php endforeach; ?>
    
    <?php dynamic_sidebar(); ?> 
    
    </div><!--.left_side-->
    </div>
    
    <div class="col-md-9 col-md-pull-3 col-xs-12">
    <div class="right_main column_text">
    <h2><?php echo $title; ?></h2>
    <span class="press_date"><?php echo $post_date; ?></span><br/>
    <?php echo get_the_post_thumbnail($postID, 'full'); ?><br /><br/>
    <?php echo $post_content; ?>
    </div><!--.right_main-->
    </div>
    
    </div><!--main-img-->
    </div><!--row-->
    </div><!--container-->
    </div><!--custom_post-->
    <!-- カスタムフィールド終了-->
    
    <?php else: ?>
    <?php endif; ?>
    <?php endwhile; ?>
    
    </div><!-- #content -->
    </div><!-- #primary -->
    <?php get_footer(); ?>

    ページをbootstrapで2カラムにしているのですが、サイドバー(left_side)にforeachを使用しているのがいけないのでしょうか。
    何か間違いがあれば教えて頂けないでしょうか…。
    何度も本当にすみません。

    トピック投稿者 sasasa24

    (@sasasa24)

    追記です、
    日付はきちんと表示されるようになりました。
    有難うございます。

    sasasa24さん

    foreachのクエリーをリセットさせてみたのですが
    如何でしょうか。

    これでうまく行かない場合はforeachをwhileの外に出してしまってはどうでしょうか。

    <?php
    /**
     * The template for displaying all single posts
     *
     * @package WordPress
     * @subpackage Twenty_Thirteen
     * @since Twenty Thirteen 1.0
     */
    
    get_header(); ?>
    
    <div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
    
    <div id="custom_post">
    <div class="container">
    <div class="row">
    <?php /* The loop */ ?>
    <?php while ( have_posts() ) : the_post();
     $post = get_post();//現在投稿のミックス値
     $postID = $post->ID;
     $title = $post->post_title;
     $post_date = $post->post_date;//投稿日
     $post_content = apply_filters( 'the_content', $post -> post_content );
    ?>                            
    
    <?php if ( in_category( array( 'column' ) )) : ?>
    <!-- カスタムフィールド開始-->
    <div class="main-img">
    <span class="cont_title">Column</span>
    <p class="cont_title">コラム</p>
    <div class="col-md-3 col-md-push-9 col-xs-12">
    <div class="left_side">
    <?php
    //ここから下は最新記事が表示される
    $randimg_posts = get_posts('category_name=column&numberposts=10');
    global $post;
    foreach( $randimg_posts as $post ) :
    ?>
    
    <a href="<?php the_permalink() ?>">
    <?php the_title(); ?></span></a><br/><hr>
    <?php
    endforeach;
    wp_reset_postdata(); //クエリのリセット
    //ENDここから↑は最新記事が表示される
    ?>
    
    <?php dynamic_sidebar(); ?> 
    
    </div><!--.left_side-->
    </div>
    
    <div class="col-md-9 col-md-pull-3 col-xs-12">
    <div class="right_main column_text">
    <h2><?php echo $title; ?></h2>
    <span class="press_date"><?php echo $post_date; ?></span><br/>
    <?php echo get_the_post_thumbnail($postID, 'full'); ?><br /><br/>
    <?php the_content(get_the_ID()) ?>
    </div><!--.right_main-->
    </div>
    
    </div><!--main-img-->
    </div><!--row-->
    </div><!--container-->
    </div><!--custom_post-->
    <!-- カスタムフィールド終了-->
    
    <?php else: ?>
    <?php endif; ?>
    <?php endwhile; ?>
    
    </div><!-- #content -->
    </div><!-- #primary -->
    <?php get_footer(); ?>
15件の返信を表示中 - 1 - 15件目 (全18件中)
  • トピック「特定のカテゴリの記事内容を表示させたい」には新たに返信することはできません。