• 解決済 ikesu

    (@ikesu)


    ループ内で表示される画像のaltをカスタムフィールドから取得したいと思っています。

    使用しているプラグインはAdvanced Custom Fieldsです。

    現在は下記のコードを使用して記事のタイトルを取得しているのですが、この値をカスタムフィールドから取得したいです。

    <?php
      $post_title = get_the_title();
      the_post_thumbnail('thumbnail',array('alt' => $post_title,));
    ?>

    取得したいカスタムフィールドは下記になります。

    <?php if( get_field('st_title') ): ?>
    <?php the_field('st_title'); ?>
    <?php else:?>
    <?php the_title(); ?>
    <?php endif; ?>

    現在の全体コード

    <?php
    $post_id = get_the_ID();
    foreach((get_the_category()) as $cat) {
    $cat_id = $cat->cat_ID ;
    break ;
    }
    query_posts(
    array(
    'tag__in' => array( 1 ),
    'tag__not_in' => array( 2 ),
    'showposts' => 1,
    'orderby' => 'rand', 
    'post__not_in' => array($post_id)
    )
    );
    if(have_posts()) :
    ?>
    <ul class="hoge">
    <?php while (have_posts()) : the_post(); ?>
    <li class="nita-title02"><a href="<?php the_permalink(); ?>" title="<?php if( get_field('st_title') ): ?>
    <?php the_field('st_title'); ?>
    <?php else:?>
    <?php the_title(); ?>
    <?php endif; ?>"><?php
      $post_title = get_the_title();
      the_post_thumbnail('thumbnail',array('alt' => $post_title,));
    ?></a></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    よろしくお願いします。

    • このトピックはikesuが1年、 1ヶ月前に変更しました。
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • こんにちは

    値は get_field() などで取得できます。

    例:

    $post_title = get_the_title();

    $post_title = ( $title = get_field( 'st_title' ) ) ? $title : get_the_title();
    トピック投稿者 ikesu

    (@ikesu)

    作業に手間取り御返事が遅れてしまい申し訳ありません。

    無事にカスタムフィールドの値を取得することができました。

    ありがとうございました。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「画像のaltをカスタムフィールドから取得したい」には新たに返信することはできません。