覚え書きφ(。。)

はてなブックマーク - 覚え書きφ(。。)
Post to Google Buzz
Livedoor Clip - 覚え書きφ(。。)
Share on FriendFeed
Bookmark this on FC2 Bookmark

get_stylesheet_directory_uri() . ‘/images〜〜〜〜
で、一回一回呼び出さなくても、header.phpに下記コードを定義、

header.php

define( 'IMAGES', get_stylesheet_directory_uri() . '/images/' );

その後でなら、

<img src="<?php echo IMAGES ?>top/title_info.png" alt="お知らせ" />

とういう略した書き方が可能。

カスタム投稿タイプにある記事の中から最大20件の新着記事をどこかに表示したい時は、

query_posts( 'posts_per_page=20&post_type=original'

とする。上記の場合、フィールド名は、「original」。

◆ ページのカテゴリ毎に見出しの画像が異なる時、

<img src="<?php echo IMAGES . get_query_var( 'pagename' ) ?>/main_title.png" alt="<?php the_title() ?>" />

◆ ページのカテゴリ毎に別のCSSを別途読ませる時、

header.php
のhead内に、

$names = bc_get_page_names( true );
$css = STYLESHEETPATH . '/css/page_' . $names['slug'] . '.css';
if ( file_exists( $css ) ) {
	printf(
		'<link type="text/css" href="%1$s/css/page_%2$s.css" rel="stylesheet" media="all" />',
		get_stylesheet_directory_uri(),
		$names['slug']
	);
}

スラッグ名に合わせてCSSのファイル名を決める。if分で、あれば読むになっている。

◆ index.phpじゃなく、別のファイルをトップページで読ませたりするやりかた (表現がおかしいかも)

function.php

function bc_get_page_names( $get_root_page=false ) {
	global $post;

	if ( is_front_page() ) {
		return array(
			'slug' => 'top',
			'title' => get_bloginfo( 'name' ),
		);
	} else if ( is_page() ) {
		$target = $post;
		$ancestor_ids = get_post_ancestors( $post );
		foreach ( $ancestor_ids as $ancestor_id ) {
			$ancestor = get_post( $ancestor_id );
			if ( ! $ancestor->post_parent ) {
				$target = $ancestor;
				break;
			}
		}
		return array(
			'slug' => $target->post_name,
			'title' => $target->post_title,
		);
	} else if ( is_post_type_archive() ) {
		$post_type = get_query_var( 'post_type' );
		$obj = get_post_type_object( $post_type );
		return array(
			'slug' => $post_type,
			'title' => $obj->label,
		);
	} else if ( is_home() ) {
		return array(
			'slug' => 'news',
			'title' => 'お知らせ',
		);
	} else if ( is_single() ) {
		return array(
			'slug' => 'news',
			'title' => 'お知らせ',
		);
	}

	return array(
		'slug' => '',
		'title' => '',
	);
}

空のtopという投稿と、空のお知らせの投稿があり、それを、1つのindex.phpで切り分けて使っている。

	if ( is_front_page() ) {
		return array(
			'slug' => 'top',
			'title' => get_bloginfo( 'name' ),
		);
	}

このへんとか、

else if ( is_home() ) {
		return array(
			'slug' => 'news',
			'title' => 'お知らせ',
		);
	} else if ( is_single() ) {
		return array(
			'slug' => 'news',
			'title' => 'お知らせ',
		);
	}

このへんとかが、そんな感じ。
もうちょっと、わかるようになったら、ちゃんとまとめなおします。
記憶なくなる前に、めもだけでも、しておかんと。

One Response to 覚え書きφ(。。)

  1. Pingback: query_posts() よりクエリ実行の無駄が無いbc_pre_get_posts | Wordpress Design b

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright