本文を入力するほどではない場合に「本文がなければ、記事タイトルにリンクを貼らない」ようにする条件分岐です。新着情報など記事一覧に利用できて便利です。
カスタムフィールド値を利用している記事一覧に対して判定させると、本文があってもリンクが貼れませんでした。そこで、カスタムフィールド値を利用していないカテゴリー記事一覧に対してのみ「本文がなければ・・・」を判定するように条件分岐しました。
下記コードでktai styleでも動作しました
<ul> <?php while (have_posts()) : the_post(); ?> <!--「カテゴリーID=1」の時の記事一覧を判定する--> <?php if(is_category(1)): ?> <!--本文がなければ--> <?php if( !get_the_content() ) : ?> <!--リンクを貼らないコード--> <li><?php the_title(); ?></li> <!--本文があれば--> <?php else : ?> <!--リンクを貼るコード--> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endif; ?> <!--それ以外のカテゴリーの記事一覧では--> <?php else : ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endif; ?> <?php endwhile; ?> </ul>
<?php if( !get_the_content() ) : ?> で動作しない場合のコード(3行目)
ktai styleで動作しませんでした。(原因不明)
<ul> <?php while (have_posts()) : the_post(); ?> <?php if(empty($post->post_content)) : ?> <li><?php the_title(); ?></li> <?php else : ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endif; ?> <?php wp_reset_query(); ?> <?php endwhile; ?> </ul>
複数のカテゴリー記事一覧を判定したい場合
<ul> <?php while (have_posts()) : the_post(); ?> <!--複数のカテゴリー記事一覧に対して判定--> <?php if( is_category( 'a' ) || is_category( 'b' ) || is_category( 'c' )): ?> <?php if( !get_the_content() ) : ?> <li><?php the_title(); ?></li> <?php else : ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endif; ?> <!--それ以外のカテゴリーの記事一覧では--> <?php else : ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endif; ?> <?php endwhile; ?> </ul>