サポート » 使い方全般 » 親ページに属する全子ページのタイトルと本文とmoraを表示

  • 解決済 nakahito

    (@nakahito)


    失礼します。 初心者です。

    [解決済み]
    親ページに属する全子ページのタイトルと本文を表示
    http://ja.forums.wordpress.org/topic/1269?replies=14

    に近いのですが、

    上記[解決済み] の記事
    親ページに、属する全ての子ページのタイトルとmore以上の内容
    プラス、
    moreが有るときにのみ、「続きを見る」のリンクを表示したいのです。

    単純に解決済のソースに
    <a href="<?php echo $c_pagelink; ?>">続きを見る</a>
    を追加した↓のこのソースでは、
    moreを使っていないページにも「続きを見る」が出てしまいます。

    moreの有無を判別して、有るときのみに「続きを見る」をつけたいのです。

    <!--  子一覧-->
    <?php
    if ( $post->post_parent == 0 ) {
    	$child_posts = get_posts( 'numberposts=-1&order=ASC&orderby=post_title&post_type=page&post_parent=' . $post->ID );
    	if ( $child_posts ) {
    		foreach ( $child_posts as $child ) {
    $c_pagelink = get_page_uri( $child->ID );
    			$c_title = apply_filters( 'the_title', $child->post_title );
    			$c_content = get_extended( $child->post_content );
    			$c_content = apply_filters( 'the_content', $c_content['main'] );
    ?>
    
    	<div class="child_page_excerpt">
    		<h3><a href="<?php echo $c_pagelink; ?>"><?php echo $c_title; ?></a></h3>
    		<div class="child_page_in"><?php echo $c_content; ?>
    		<a href="<?php echo $c_pagelink; ?>">続きを見る</a>
           </div>
    
    	</div>
    <?php
    		}
    	}
    }
    ?><!-- end 子一覧-->

    以上、コードなど教えていただけると助かります。
    よろしくお願いいたします。

7件の返信を表示中 - 1 - 7件目 (全7件中)
  • $c_content = apply_filters( 'the_content', $c_content['main'] );
    の後で

    moreの有無を判別して、有るときのみに「続きを見る」をつけ

    ます。

    if ( '' != $c_content['extended'] )
      $c_content .= '<a href="' . $c_pagelink . '">続きを見る</a>';

    トピック投稿者 nakahito

    (@nakahito)

    Kzさま

    ありがとうございます。

    if ( '' != $c_content['extended'] )
      $c_content .= '<a href="' . $c_pagelink . '">続きを見る</a>';

    を入れてましたが、
    残念ながら、
    more を入れていないページにも 続きを見る が表示されています。

    確かに、記事のソースコードを見ると、more 以降の部分には、
    id=”extended
    がついていますので、 extended のあるなしを判断する形でもんだいはないのでしょうが、
    なぜ、id=”extended がないページまで 続きを見る がでてしまうのでしょうか?

    phpの文法が分からず、申し訳ございません。

    現在のうまくいっていないソースを全部記述したほうがいいかもしれません。
    予測するに余計なコードが残っているような気がします。

    nakahitoさん、こんにちは。

    元々存在した
    <a href="<?php echo $c_pagelink; ?>">続きを見る</a>

    という部分がそのまま残っていませんか?
    これが残っていると、more を入れていないページにも 続きを見る が表示されてしまいます。

    トピック投稿者 nakahito

    (@nakahito)

    shokun0803さま

    ありがとうございます。

    現在のうまくいっていないソースを全部記述したほうがいいかもしれません。
    予測するに余計なコードが残っているような気がします。

    ↓になります。

    <!--  子一覧-->
    <?php
    if ( $post->post_parent == 0 ) {
    	$child_posts = get_posts( 'numberposts=-1&order=ASC&orderby=post_title&post_type=page&post_parent=' . $post->ID );
    	if ( $child_posts ) {
    		foreach ( $child_posts as $child ) {
    $c_pagelink = get_page_uri( $child->ID );
    			$c_title = apply_filters( 'the_title', $child->post_title );
    			$c_content = get_extended( $child->post_content );
    			$c_content = apply_filters( 'the_content', $c_content['main'] );
    if ( '' != $c_content['extended'] )
      $c_content .= '<a href="' . $c_pagelink . '">続きを見る</a>';
    ?>
    
    	<div class="child_page_excerpt">
    		<h3><a href="<?php echo $c_pagelink; ?>"><?php echo $c_title; ?></a></h3>
    		<div class="child_page_in"><?php echo $c_content; ?>
           </div>
    
    	</div>
    <?php
    		}
    	}
    }
    ?><!-- end 子一覧-->

    kurosquareさま

    ありがとございます。

    元々存在した

    <a>">続きを見る</a>

    という部分がそのまま残っていませんか?

    こちらは削除しています。

    アドバイスいただけると、助かります。

    あ。
    $c_content = get_extended( $child->post_content );
    ここで取得した <!--more--> 前後の内容を
    $c_content = apply_filters( 'the_content', $c_content['main'] );
    で破壊してますね。

    以下で OK なはずです。

    <!--子一覧-->
    <?php
    if ( $post->post_parent == 0 ) {
      $child_posts = get_posts( 'numberposts=-1&order=ASC&orderby=post_title&post_type=page&post_parent=' . $post->ID );
      if ( $child_posts ) {
        foreach ( $child_posts as $child ) {
          $c_pagelink = get_page_uri( $child->ID );
          $c_title = apply_filters( 'the_title', $child->post_title );
          $c_contents = get_extended( $child->post_content );
          $c_content = apply_filters( 'the_content', $c_contents['main'] );
    
          if ('' != $c_contents['extended'] )
            $c_content .= '<a href="' . $c_pagelink . '">続きを見る</a>';
    ?>
          <div class="child_page_excerpt">
            <h3><a href="<?php echo $c_pagelink; ?>"><?php echo $c_title; ?></a></h3>
            <div class="child_page_in"><?php echo $c_content; ?></div>
          </div>
    <?php
        }
      }
    }
    ?><!-- end 子一覧-->

    トピック投稿者 nakahito

    (@nakahito)

    kzさま

    OKでした。
    大変ありがとうございました。 
    迅速はお返事を感謝いたします。

7件の返信を表示中 - 1 - 7件目 (全7件中)
  • トピック「親ページに属する全子ページのタイトルと本文とmoraを表示」には新たに返信することはできません。