【WordPressプラグイン】Smart Custom Fieldsの出力

基本の出力

読み込んだ記事のループ内で使う基本的な使い方です。

テキスト、テキストエリア

<?php $txt = scf::get('txt'); echo $txt; ?>

改行を反映するときは、

<?php $txt = scf::get('txt'); echo nl2br( $txt ); ?>

画像/ファイル

画像はidを取得します。

<?php $img = scf::get('img'); ?>
<img src="<?php echo wp_get_attachment_url( $img ); ?>" alt="">

WYSIWYG

基本はテキストと一緒

<?php $txt = scf::get('txt'); echo $txt; ?>

ショートコードを反映したいときは、

<?php $txt = scf::get('txt'); echo do_shortcode($txt); ?>

他のページで設定したものの出力

カテゴリページで設定したもの

投稿に紐づいたカテゴリーの画像を出力したいとき。
下記だと「fashion」というタクソノミーのタームに設定した「fashion-img」というカスタムフィールドの値を出力します。(これ以前にタクソノミーを呼び出す必要があります)

<?php
    $image = SCF::get_term_meta( $term->term_id, 'fashion', 'fashion-img' );
    echo wp_get_attachment_image($image, 'medium');
?>

オプションページの出力

オプションページの作成は下記をfunction.phpに記述。

/**
 * @param string $page_title ページのtitle属性値
 * @param string $menu_title 管理画面のメニューに表示するタイトル
 * @param string $capability メニューを操作できる権限(maange_options とか)
 * @param string $menu_slug オプションページのスラッグ。ユニークな値にすること。
 * @param string|null $icon_url メニューに表示するアイコンの URL
 * @param int $position メニューの位置
 */
SCF::add_options_page( 'ページタイトル', 'メニュータイトル', 'manage_options', 'theme-options' );

出力は下記

SCF::get_option_meta( 'メニュースラッグ', 'フィールド名' );

参考