6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Advanced Custom FieldsのRepeater Fieldを別のページに表示する

Last updated at Posted at 2018-08-24

WordPressプラグイン「Advanced Custom Fields」を
トップページや他の固定ページで使いたい場合の解決コードです。

Advanced Custom Fieldsを表示

パターン1 ページidを指定して表示

通常のカスタムフィールドの表示はこれでいける。
フィールド名と固定ページidを以下のように記述します。

<?php echo get_field ('★フィールド名★',固定ページid));?>

パターン2 ページスラッグを指定して表示

ページスラッグを指定する場合はこちら。

$page_id = get_page_by_path('固定ページスラッグ');
$page_id = $page_id->ID;
echo get_post_meta($page_id, 'フィールド名', true);

Repeater Fieldを表示

Repeater Field(繰り返しフィールド)を表示する場合は、
以下のように固定ページスラッグを記載します。

<?php
$page_id = get_page_by_path('★固定ページスラッグ★');
$page_id = $page_id->ID;
?>
<?php if(have_rows('★フィールドグループ★', $page_id)): ?>
<?php $page_counter = 2; ?>//表示件数
<?php while(have_rows('★フィールドグループ★', $page_id)): the_row(); ?>
<?php $page_counter--; ?>
//ループ内容
<?php the_sub_field('★サブフィールド★'); ?>

<?php if ($page_counter === 0) break;?>
<?php endwhile; ?>
<?php endif; ?>

参考URL

カスタムフィールドの値をトップページなどで表示する – ma-ya's CREATE / WEB DESIGN

Advanced Custom Fields のアドオン Repeater Field(リピーターフィールド)の表示方法 - by Takumi Hirashima

Special Thanks kuninori ogino

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?