カスタムメニューを画像にする

以下をテーマ内のfunction.phpに挿入してください。

add_filter( 'the_title', 'img_nav_menu_title' );
function img_nav_menu_title($title) {
	if( preg_match( "/\[\[(.*)\]\]/", $title, $match ) ) {
		$get_title = $match[1];
		$get_img_url = clean_url($get_title);
		if($get_title == 'none') {
			$title = '';
		} else {
			$get_img_dir = str_replace( get_option('home') . '/', ABSPATH, $get_img_url );
			$get_size    = getimagesize( $get_img_dir );
			$get_alt     = preg_replace( "/\[\[(.*)\]\]/", '', $title );
			$title = '<img src="' . $get_img_url . '" alt="' . $get_alt . '" />';
		}
		return $title;
	} else {
		return $title;
	}
}

ナビゲーションラベルに
ラベル名[[画像のURL]]
を入力すると画像が表示されます。
ラベル名[[none]]
とするとタイトルは表示されません。
前回のウィジェットタイトルに画像を入れるをしている場合は、

add_filter( 'the_title', 'img_widget_title' );

を追加してもらえれば使用できます。

後はCSSを調整して終了です。

This entry was posted in 未分類. Bookmark the permalink.

One Response to カスタムメニューを画像にする

  1. Webnist より: Twitter ID Webnist

    コメントテスト

コメントを残す

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

*

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