Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
mypacecreator / customfield.php
Last active August 29, 2015 13:57
WordPressのカスタムフィールド基本スニペット( 3.5 or higher)
※keyname の部分は作成したカスタムフィールドの名前に置き換える
そのまま出力
<?php echo $post->keyname; ?>
または
<?php echo $post->{keyname}; ?>
HTMLをエスケープしたい場合
<?php echo esc_html( $post->keyname ); ?>
@mypacecreator
mypacecreator / googlefeed.js
Created January 27, 2014 03:52
Google AJAX Feed APIでアメブロの新着記事を取得しPRを除外するJS
/*
別途HTML側で以下のスクリプトを読み込む。
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/googlefeed.js"></script>
また、フィードを表示させたい箇所に
<div id="feed"><p>Now Loading...</p></div>
を置く。
*/
@mypacecreator
mypacecreator / page.php
Last active December 28, 2015 11:58
ページスラッグを元にタイトル画像を表示する(子カテゴリーでもそのページのスラッグを取る)
<h1><img src="<?php echo esc_url( get_template_directory_uri() ); ?>/images/title_<?php echo esc_attr( $post->post_name ); ?>.jpg" alt="<?php the_title_attribute(); ?>" /></h1>
@mypacecreator
mypacecreator / archive.php
Last active December 28, 2015 11:58
アーカイプページで、カテゴリースラッグを元にタイトル画像を表示する(子カテゴリーの場合も、親カテゴリーの画像を使う)
<?php if(is_category()): ?>
<?php
$cat_id = get_query_var('cat');
$cat = get_category($cat_id);
$cat_parent_id = $cat->category_parent;
$cat_parent = get_category($cat_parent_id);
if( $cat_parent_id == 0 ): //親カテゴリの場合
$cat_parent = get_category($cat_parent_id);
?>
<p class="title"><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo $cat->category_nicename; ?>.jpg" alt="<?php single_cat_title(); ?>" /></p>
@mypacecreator
mypacecreator / single.php
Last active December 28, 2015 11:58
single.phpで、カテゴリースラッグを元にタイトル画像を表示する(子カテゴリーの場合も、親カテゴリーの画像を使う)
<?php
$cat = get_the_category();
if ($cat[0]->category_parent): //子カテゴリの場合
$parent_cat = get_category($cat[0]->category_parent);
?>
<p class="title"><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo $parent_cat->slug; ?>.jpg" alt="<?php echo $parent_cat->name; ?>" /></p>
<p class="subtitle"><?php echo $cat[0]->cat_name; ?></p>
<?php else: //親カテゴリの場合 ?>
<p class="title"><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo $cat[0]->category_nicename; ?>.jpg" alt="<?php echo $cat[0]->cat_name; ?>" /></p>
<?php endif; ?>
@mypacecreator
mypacecreator / single.php
Last active December 28, 2015 11:58
アイキャッチ画像をポップアップに
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php
$popupimg_id = get_post_thumbnail_id();
$popupimg_url = wp_get_attachment_image_src($popupimg_id,'full', true);
echo esc_url($popupimg_url[0]);
?>"><?php the_post_thumbnail(); ?></a>
<?php endif; ?>
@mypacecreator
mypacecreator / ameblo-feed.php
Last active May 24, 2016 05:23
アメブロRSSから[PR]を外してfetch_feedするやーつ
<?php
//以下3行の項目を任意に変更
$display_posts_count = 5; //実際に表示したい記事件数
$get_posts_count = 10; //取得する記事件数(PR記事を含むので$display_posts_countより少し多めに設定)
$feed = fetch_feed('http://feedblog.ameba.jp/rss/ameblo/**blogname**/rss20.xml'); //取得したいRSS
//
$counter = 0; //ループ回数カウンター
include_once(ABSPATH . WPINC . '/feed.php');
if (!is_wp_error( $feed ) ) : //エラーがなければ
@mypacecreator
mypacecreator / functions.php
Last active December 22, 2015 03:48
カスタム投稿タイプ+カスタムタクソノミー作成でよく使うやつ
<?php
//カスタム投稿タイプ作成
function mypace_custom_post_type() {
register_post_type( 'voice', array(
'label' => 'お客様の声',
'public' => true,
'exclude_from_search' => false,
'hierarchical' => false,
'menu_position' => 5,
'has_archive' => true,
@mypacecreator
mypacecreator / page-faq.php
Last active December 21, 2015 06:19
get_termsとget_postsの入れ子例
<?php
$taxonomies = get_terms(
'faqcategory',array(
'orderby' => 'slug',
'order' => 'ASC'
));
foreach($taxonomies as $taxonomy) :
?>
<div class="parentBox">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
@mypacecreator
mypacecreator / gist:6263118
Created August 18, 2013 18:16
メールフォームの都道府県選ぶアレ
<select name="pref">
<option value="">お選びください</option>
<optgroup label="北海道・東北">
<option value="北海道">北海道</option>
<option value="青森県">青森県</option>
<option value="岩手県">岩手県</option>
<option value="宮城県">宮城県</option>
<option value="秋田県">秋田県</option>
<option value="山形県">山形県</option>
<option value="福島県">福島県</option>