WordPressのカスタムフィールドで値を判定して表示

WordPressのカスタムフィールドで値を判定して表示するタグは以下の通り

$sample = post_custom('カスタムフィールド');
if ( $sample )  {
	echo $sample;
} else {
	//値が入っていません
}

WordPressの投稿一覧画面にカスタムフィールドの値を表示

WordPressの投稿一覧画面にカスタムフィールドの値を表示する方法

ひとつ追加する場合は、
下記コードをfunction.phpに追加する

/**
 * 投稿一覧に商品コード列追加
 */

function manage_posts_columns($columns) {
	$columns['Goods'] = "商品コード";
	return $columns;
}
function add_column($column_name, $post_id) {
	if( $column_name == 'Goods' ) {
		$stitle = get_post_meta($post_id, 'Goods', true);
	}
	if ( isset($stitle) && $stitle ) {
		echo attribute_escape($stitle);
	} else {
		echo __('None');
	}
}
add_filter( 'manage_posts_columns', 'manage_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 );

2つ表示させる場合は、以下のコードを追加。

/**
 * 投稿一覧に商品コード列追加
 */

function manage_posts_columns($columns) {
	$columns['Goods'] = "商品コード";
	$columns['Area'] = "産地";
	return $columns;
}
function add_column($column_name, $post_id) {
	if( $column_name == 'Goods' ) {
		$stitle = get_post_meta($post_id, 'Goods', true);
	}
	if( $column_name == 'Area' ) {
		$stitle = get_post_meta($post_id, 'Area', true);
	}
	if ( isset($stitle) && $stitle ) {
		echo attribute_escape($stitle);
	} else {
		echo __('None');
	}
}
add_filter( 'manage_posts_columns', 'manage_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 );

WordPressの基本タグまとめ

カテゴリーページへ使う記事へのリンクタグ

<?php the_permalink(); ?>
/*example*/
<a href="<?php the_permalink(); ?>">テキストテキスト</a>

カスタムフィールドを改行ありで表示

<?php echo nl2br(post_custom('wpcf-work_time')); ?>

WordPressで投稿ページや固定ページでコメントアウトする方法

WordPressでコメントアウトタグを挿入すると、
終わりのタグがおかしくなってしまい、
うまくできない。

そのため、以下の様な方法にて回避すると便利。

まずは、function.phpに下記コードを追記。

//コメントアウトのショートコード
function overrule_shortcode( $atts, $content = null ) {
return null;
}
add_shortcode('overrule', 'overrule_shortcode');

その後、以下コードでページ内の隠したいところを囲えば良い。

[overrule]テキストテキスト・・・・・。[/overrule]

WordPressで全記事数を表示

WordPressで全記事数を表示するには、
下記タグをテンプレートファイルに書き込めばいい。

<?php $numposts = $wpdb->get_var("SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'");
if (0 < $numposts)
$numposts = number_format($numposts);
echo $numposts.' 商品販売中です。';
?>

カスタムフィールドやカスタム投稿・タクソノミーが簡単に設定できるWordPressのプラグインTypes

ダウンロードはこちら。
http://wp-types.com/home/types-manage-post-types-taxonomy-and-custom-fields/

設定方法は下記ページで詳しく解説してあります。
http://www.imaginationdesign.jp/blog/wordpress/2278/

表示する際は、以下のコードで表示できる。

<?php echo post_custom('wpcf-work_year'); ?>

wordpressのAdvanced Custom Fieldsでフィールドの値を取得して出力するテンプレート。

【基本的な方法】

Advanced Custom Fields

フィールドの値を取得して表示するには、「the_field()」を使います。

post_customなどのタグを使うと、プレビューなどが反映されないため、
Advanced Custom Fields特有のタグを使う必要がある。

the_field('field_name_01');

フィールドの値を取得して変数に代入して使う場合などは、「get_field()」を使います。

$hoge = get_field('field_name_01');

【フィールドタイプ:テキスト】

取得したフィールド(フィールド名:field_name_01)に何か入力されていたら、値を表示する場合の例。

$text = get_field('field_name_01');
if ( $text )  {
    echo $text;
}

【フィールドタイプ:テキストエリア】

改行で区切られたデータが入っているフィールド(フィールド名:field_name_01)の値を取得し、リスト形式に成形して表示する場合の例。

$fieldData = explode("\n",get_post_meta($post->ID,'field_name_01',true) );
$i = 0;
echo "<ul>";
foreach ($fieldData as $value){
    if ( $value ){
        echo '<li>' . $value . '</li>';
    }
    $i++;
echo "</ul>";
}

【フィールドタイプ:数値】
取得したフィールド(フィールド名:field_name_01)の値を、千位毎の区切り及び小数点以下第一位まで表示させる場合の例。

echo number_format( get_post_meta($post->ID,'field_name_01',true) , 1 );

【フィールドタイプ:Wysiwyg エディタ】

取得したフィールド(フィールド名:field_name_01)の値に画像にリンクを貼っている箇所が存在したら、aタグに「rel=”lightbox”」を付加する場合の例。

$pattern = "/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="lightbox"$6>';

$dataText = get_field("field_name_01", $post->ID);
$dataText = preg_replace ( $pattern , $replacement , $dataText);

echo $dataText;

【フィールドタイプ:画像】

取得したフィールド(フィールド名:field_name_01)の画像をlargeサイズで表示する場合の例。(lightboxを使う前提で、aタグには「rel=”lightbox”」をつけています。)

※ フィールド名「field_name_01」を定義する時に、「返り値」は「画像 ID」を指定しておいてください。

<?php
    $attachment_id = get_field('field_name_01');
    $size = "large"; // (thumbnail, medium, large, full or custom size)
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    $attachment = get_post( get_field('field_name_01') );
    $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    $image_title = $attachment->post_title;
?>

<a href="<?php echo wp_get_attachment_url( get_field( 'field_name_01',$post->ID) ); ?>" rel="lightbox"><img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php echo $alt; ?>" title="<?php echo $image_title; ?>" /></a>

【フィールドタイプ:画像IDを取得して表示】

//画像IDの取得
$image_id = post_custom('[画像アップロードのカスタムフィールド名]');
//画像IDから画像を表示 $sizeに表示サイズを設定(デフォルトは'thumbnail')
echo wp_get_attachment_image($image_id, $size); 

【フィールドの値をチェックして表示】

$text = get_field('my_custom_01');
if($text) :
echo $text;
endif;

WordPressの不要なリンクを削除する

テーマファイルの「function.php」を使うのが最も手軽でしょう。以下の記述を追記すればOKです。

if (function_exists(‘register_sidebar’))
register_sidebar();
remove_action(‘wp_head’,'index_rel_link’);
remove_action(‘wp_head’,'parent_post_rel_link’,10);
remove_action(‘wp_head’,'start_post_rel_link’,10);
remove_action(‘wp_head’,'adjacent_posts_rel_link_wp_head’,10);

WordPressのフィードを削除する

functions.php の

add_theme_support( 'automatic-feed-links' );

を削除し

remove_action('wp_head', 'feed_links_extra', 3);

を追加する。

その後 header.php に

<link rel="alternate" type="application/rss+xml" title="RSS" href="<?php bloginfo('rss2_url'); ?>" />

を追加する。

wp_list_categories()が出力するHTMLを変更したい。

従来のカテゴリーの場合

<?php
	$categories = get_categories('parent=0&hide_empty=0&orderby=id');
	if (is_array($categories)) {
		foreach($categories as $category):
			$v_cat_id = $category->cat_ID;
			$v_cat_Parent_title = $category->cat_name;
			$v_cat_Parent_t_a = wp_specialchars($v_cat_Parent_title);
			$v_cat_Parent_t_u = attribute_escape($v_cat_Parent_title);
			$v_cat_Parent_url = get_category_link($v_cat_id);
			$v_cat_child_list = wp_list_categories("orderby=id&hide_empty=0&title_li=&use_desc_for_title=0&child_of=$v_cat_id&depth=1&echo=0");
			echo "<h2><a href=\"$v_cat_Parent_url\" title=\"$v_cat_Parent_t_u\">$v_cat_Parent_t_a</a></h2>\n";
			if ( strpos($v_cat_child_list, __('No categories')) === false) {
				echo "<ul>\n$v_cat_child_list\n</ul>\n";
			}
		endforeach;
	}
?>

タクソノミーの場合
※コードはかなり汚い。

//	$categories = get_categories('parent=0&hide_empty=0&orderby=id');
//	$categories = get_terms( 'category' , 'parent=0&hide_empty=0&orderby=id');
//echo '<br /><br /><br /><br /><br />';
$get_terms_args_id = $taxonomy_id->term_id;
echo $get_terms_args_id;
$get_terms_args = 'parent=' . $get_terms_args_id . '&hide_empty=0&orderby=id';
//$get_terms_args = 'parent=128&hide_empty=0&orderby=id';
	$categories = get_terms( 'goods-cate' , $get_terms_args);
	$count = count($categories);
if ( $count > 0 ) {
	if (is_array($categories)) {
echo '<ul class="goodsTabs clear">';
		foreach($categories as $category):
//echo '<br /><br />';
			$v_cat_id = $category->term_id;
//echo $v_cat_id . '<br />';
			$v_cat_Parent_title = $category->name;
//echo $v_cat_Parent_title . '<br />';
			$v_cat_Parent_t_a = wp_specialchars($v_cat_Parent_title);
//echo $v_cat_Parent_t_a . '<br />';
			$v_cat_Parent_t_u = attribute_escape($v_cat_Parent_title);
//echo $v_cat_Parent_t_u . '<br />';
//			$v_cat_Parent_url = get_category_link($v_cat_id);
			$v_cat_Parent_slug = $category->slug;
//			$v_cat_Parent_url = get_term_link($v_cat_Parent_slug , 'category' );
//echo $v_cat_Parent_url . '<br />';
			$homeurl = home_url();
			$v_cat_child_list = wp_list_categories('orderby=id&hide_empty=0&title_li=&use_desc_for_title=0&child_of=' . $v_cat_id . '&depth=1&echo=0');
			echo '<li><a href="' .$homeurl . '/goods-cate/' .$v_cat_Parent_slug . '/">' . $v_cat_Parent_title . '</a></li>';
			if ( strpos($v_cat_child_list, __('No categories')) === false) {
//				echo "<ul>\n$v_cat_child_list\n</ul>\n";
			}
		endforeach;
echo '</ul>';
	}
}

WordPressでカスタム投稿もタクソノミーなどで絞り込みが出来る様にする。

WordPressでカスタム投稿もタクソノミーなどで絞り込みが出来る様にするには、
function.phpに下記タグを表記する。

// カスタムタクソノミーでフィルター (絞り込み機能)
add_action( 'restrict_manage_posts', 'add_post_taxonomy_restrict_filter' ); 
function add_post_taxonomy_restrict_filter() { 
    global $post_type; 
    if ( '●投稿タイプ英語●' == $post_type ) { 
        ?> 
        <select name="●タクソノミー英語●"> 
            <option value="">カテゴリー指定なし</option> 
            <?php 
            $terms = get_terms('●タクソノミー英語●'); 
            foreach ($terms as $term) { ?> 
                <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option> 
            <?php } ?> 
        </select> 
        <?php 
    } 
} 

WordPressで、カスタム分類ごとにカスタム投稿を表示する方法

WordPressで、カスタム分類ごとにカスタム投稿を表示する際のサンプルコード。
<?php
    $taxonomys = get_terms( "ここにtaxonomyを入力","orderby=slug&order=DESC&parent=ここに親カテゴリを指定する場合は入力");//"frontiercat"からタームを取り出す。数字が小さいものから大きいものの順に取り出す
    foreach($taxonomys as $value)://繰り返しその一つを$valueに代入
 ?>
<div id="<?php echo $value->slug; ?>" class="area">カテゴリ名:<?php echo $value->name;?> スラッグ名:<?php echo $value->slug; ?></div>
<?php query_posts( array( 'menu-cate' => $value->slug, 'order'=>'DESC' ) ); //記事を読み込む ?>
<?php if(have_posts()):
while(have_posts()): the_post(); ?>
<h1>タイトル<?php the_title(); ?></h1>
<p>抜粋:<?php the_excerpt(); ?></p>
<p>カスタムフィールド:<?php echo post_custom('menu-price'); ?></p>
<p>本文<?php the_content(); ?></p>

<?php endwhile; endif; ?>
 </dl>

<?php endforeach; ?>

WordPressでホームページのタイトルを表示

WordPressでホームページのタイトルを表示するたぐは以下。

<?php bloginfo('name'); ?>

wordpressのwp_list_pagesについて

wordpressのwp_list_pagesについて、
カスタマイズ方法メモ

通常の場合、『固定ページ』といったタイトルが表示されるが、
それを表示したくない場合は、
以下の様にする。

<ul><?php wp_list_pages('title_li='); ?></ul>

タイトルを変更する際は、
下記の様に値を入れてあげればよい。

<ul><?php wp_list_pages('title_li=ショッピングガイド'); ?></ul>

表示する階層の深さを指定する場合は、

<?php wp_list_pages('depth=2'); ?>

表示順序を管理画面で決めた順番にするには、

<?php wp_list_pages('sort_column=menu_order'); ?>

不動産プラグインでフィールドを非表示にする方法

WordPressのプラグイン「不動産プラグイン」でフィールドを非表示にする方法のメモ。
不動産プラグイン

fudo-functions.phpの

add_action('○○○', '○○○') ;

の部分をコメントアウトする。

WordPressで特定のカテゴリの最新記事を表示

WordPressで特定のカテゴリの最新記事を表示する方法

		<ul class="sidemenu">
			<?php
			$my_query = new WP_Query('cat=1&showposts=10');
			while ($my_query->have_posts()) : $my_query->the_post(); ?>
			<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
			<?php endwhile; ?>
		</ul>

WordPressでに入る»やスペースを消す方法

WordPressの専用タグ

<?php wp_title(); ?>

を使用する際、なぜか»やスペースが自動的に入ってしまう。

これは、デフォルトで決まっているためです。
公式サイト参照

これを解決するためには、以下の様なタグを記述すると良い。

<?php echo trim(wp_title('', false)); if (wp_title('', false)) ?>

WordPressでカテゴリーを表示する

WordPressでカテゴリーを表示する基本のタグは下記の通り。

<?php wp_list_categories(); ?>

また、下記の様に詳細を設定することもできる

<?php wp_list_categories('show_count=TRUE&child_of=10&title_li='); ?>

設定詳細

WordPressで更新順にする方法

例をメモしておきます。

<?php
$posts = query_posts(array(
    'posts_per_page' => 3,
    'orderby' => 'modified',
));?>
			<?php if ( have_posts() ) : ?>
				<ul class="indexNews">
				<?php while ( have_posts() ) : the_post(); ?>
					<?php
						get_template_part( 'content-index', get_post_format() );
					?>
				<?php endwhile; ?>
				</ul>
			<?php else : ?>
				<p style="margin: 50px auto 0 40px;">ただ今準備中です。</p>
			<?php endif; ?>
			<?php wp_reset_query(); ?>

WordPress カスタムフィールドの表示方法

WordPressのカスタムフィールドの表示タグは以下の通り。


<?php
$field = get_post_meta($post--->ID, 'カスタムフィールドの名前', true);
if($field != '') {
echo get_post_meta($post->ID, 'カスタムフィールドの名前', true);
} else {
echo '値が空欄の表示文言';
}
?>

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress