この記事は公開または最後に更新されてから2494日が経過しています。情報が古くなっている可能性があるのでご注意下さい。

さて、『カスタムフィールドの投稿入力フォームへの表示』においてカスタムフィールド入力フォームを投稿入力フォームに表示することはできました。
あと、投稿一覧へのカスタムフィールドのリスト表示と、クイック編集フォームにおいても入力フォームが表示出来ればプラグインがやってくれていた必要なことはすべて出来るようになります。

と、まずは投稿一覧へのリスト表示から。
フィルターフック "manage_posts_columns" を使って一覧のカラムにカスタムフィールドの項目を追加しておいて、アクションフック "manage_posts_custom_column" において実際に表示するhtml を生成するという手順ということです。
一覧のカラムの項目の配列 $columuns は、キーにカラムID (codexにはcolumn name となっていて説明はThe name is passed to functions to identify the column. です)、値に見出しとして表示させるタイトルの連想配列となっています。一覧のカラムにカスタムフィールドの項目を追加するのはキーにID、タイトルを値にして配列に追加すれば良いわけです。
で、追加出来るということは削除も出来るということではないかと。"manage_posts_columns"のcodex を見ると $columuns のキーがリストしてあります。

cbCheckbox for bulk actions.
titlePost title.
authorPost author.
categoriesCategories the post belongs to.
tagsTags for the post.
commentsNumber of pending comments.
date

たとえば自分だけが投稿しているブログであれば"投稿者"の項目などは必要ありません。ならば配列のリストから"author" の要素を削除してしまえば良いということです。
ここのサイトは通常投稿post にカスタム投稿diys でカスタムフィールドを使用しています。表示させる内容が全く同じであればカスタム投稿用を用意する必要は無いのですが、post においてはサムネイル画像を表示させようと思っていまして、カスタム投稿には画像は無いのでその項目は表示させたくありません。ゆえにカスタム投稿表示用も用意する必要があるようです。

<?php
function add_postlist_cf_column( $columns ) {
	$column_meta_third = array( 'firstimg'=>'first image' );
	$column_meta_sixth = array( 'customfield'=>'custom field' );
	$columns = array_slice( $columns, 0, 2, true ) + $column_meta_third + array_slice( $columns, 3, 2, true ) + $column_meta_sixth + array_slice( $columns, 5, NULL, true );
	return $columns;
}

function add_diylist_cf_column( $columns ) {
	unset( $columns['firstimg'] );
	return $columns;
}
add_filter( 'manage_posts_columns', 'add_postlist_cf_column' );
add_filter( 'manage_diys_posts_columns', 'add_diylist_cf_column' );
?>
PHP
CopyExpand

post 用の "add_postlist_cf_column()" ではあらかじめ追加する要素、サムネイル用の"firstimg"とカスタムフィールド用の"customfield"の配列をそれぞれ作成しておきます。別にしているのは表示させたい場所が右から3番目と6番目というように離れているからです。で、array_slice() で$columns を分解し、挿入したいところ(表示させたいところ)にそれぞれの配列を加えて、ついでに"author" の要素を抜いて配列を合成し直しています。
そして、カスタム投稿diys では追加したサムネイル用の要素"firstimg" は表示したくないので、配列からunset() 関数を使って削除しています。

次に実際にhtml を出力する"add_column()" ですが、パラメータの$column_name によって"customfield" 用と"firstimg" 用に処理を分岐させています。
カスタムフィールド用のデータは "get_post_custom($post_id)" で、その投稿に設定してあるカスタムフィールドのデータをまとめて取得しています。が、この時取得しているデータというのは wp_postmeta テーブルにあるフィールドpost_id の値が $post_id のデータを全てです。というのは、取得した値にはカスタムフィールドのデータだけではなくwordpress が処理用に作っている _edit_lock や_edit_lock、_pingme、_encloseme 等があり、それらのデータには先頭に"_ (アンダーライン)"があるのでそれを正規表現で判別してカスタムフィールドのデータだけを出力するようにしています。
で、そのままカスタムフィールド名では出力せずに、ラベル用のタイトルに変えるための連想配列を用意しておいてそれを出力しています。

サムネイル画像表示用においては、実は私はアイキャッチ画像の設定をしていなくてそれを使うことが出来ません。なのでその投稿にアップしてある画像の情報を "get_children()" で全て取得し、一番始めの画像を "wp_get_attachment_image_src()" で得て、表示するようにしています。

※そしてここでちょっと修正した内容のことを。
ブロックエディタが採用されてクラッシックエディタとの使い分けにおいて、それをカスタムフィールドで保存しているようになりました。各投稿にいつの間にかカスタムフィールドの値が増えてしまったんですね。
それによって、既存のカスタムフィールドの値をすべて取得すると当然のことながらそのエディタ設定値も入ってきてしまうので、それを分別するようにしないとエラーが出てしまうようになってしまいました。
で、それを回避するために加えたのが23行目のif文となります。カスタムフィールド名をいれた連想配列を使って、その配列にある値だけということで判断させているわけです。あれっ?でも元々それを使って分別させればよかったわけで、上述の頭に_(アンダーライン)のついたデータも正規表現なんぞを使用しなくても良かったわけですな。正規表現を使わない分だけ、軽くなるはずだし。なんで気が付かなかったのだろうか。と、いうわけで正規表現の箇所も不必要となります。消してもよいのですが、一応、わかるようにコメントアウトしてあります。

<?php
function add_column( $column_name, $post_id ) {
	if ( $column_name === 'customfield' ) {// カスタムフィールド用
		$cfield = get_post_custom( $post_id );
		if ( count( $cfield ) > 0 ) {
			 // $cfary:カスタムフィールド名をラベル用のタイトルにかえるための連想配列
			$cfary = array(
				'firstID'=>'First ID',
				'jpn'=>'Japanese',
				'rmn'=>'Romanized',
				'egn'=>'English',
				'bon'=>'Binomial',
				'take'=>'撮影日時',
				'bcg'=>'background image',
				'fee'=>'餌付け状態',
				'enc'=>'初見時状況',
				'up'=>'Latest update',
				'desc'=>'seo describe',
				'sky'=>'seo keywords'
				);
			$echostr = '<p style="color:#008080;">';
			foreach ( $cfield as $name => $value ) {
				if ( isset ( $cfary[ $name ] ) ) {
					//if ( ! ( preg_match( '/^_/', $name ) ) && $name !== '' ) {
						$echostr .= $cfary[ $name ] . ' : ' . esc_html( $value[0] ) . '<br>';
					//}
				}
			} 
			$echostr .= '</p>';
			echo $echostr;
		}
	} elseif ( $column_name === 'firstimg' ) {// サムネイル画像用
		$attachments = get_children( array( 'post_type'=>'attachment', 'post_mime_type'=>'image', 'post_parent'=>$post_id ), ARRAY_A );
		if ( count( $attachments ) > 0 ) {
			$keyarray = array_keys( $attachments );// キーだけを配列にする
			$firstimgarray = $attachments[ $keyarray[0] ];// そのキーにより最初の画像を得る
			$firstimg = wp_get_attachment_image_src( $firstimgarray['ID'] );
			echo '<img src="' . $firstimg[0] . '" width="150" height="150" alt="" />';
		}
	}
}
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 );
?>
PHP
CopyExpand

こんなかんじ!
post list

Leave a Reply!

JavaScript is necessary to send a comment.
You can edit and delete your comment if you input a edit key.
Edit key is necessary for attesting you when you edit and delete it.
The tag of HTML cannot be used in comment.
When you comment for the first time, it is displayed after the approval of the administrator.
Because I cannot speak English so much, it takes time to answer.
Required fields are marked *.

※Please enter more than 5 characters only alphabets.
※Edit or delete are possible for 2000 days after approval.

*

♠Simplistic Comment User Editable v4.0

♠When visitors leave comments on the site this site collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.
♠This site does not use cookie when visitors leave comments and commenter edit comment.
♠This site uses Akismet to reduce spam. Learn how your comment data is processed.

Comments feed

Trackback URL : https://strix.main.jp/wp-trackback.php?p=48237

Sanbanse Funabashi
2011.01.01 sunrise

Top

スクロールさせるか画像をクリックすると元に戻ります。