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 / functions.php
Last active August 29, 2015 13:57
特定の条件下で投稿の表示件数を変えたりするアクションフックpre_get_postsのスニペット
<?php
function customed_queries ( $query ) {
if ( is_feed() ) { // フィードにカスタム投稿を含める
$query->set( 'post_type', array( 'post', 'gallery' ) );
return $query;
}
if ( is_admin() || ! $query->is_main_query() ) { //管理画面とサブクエリは対象外
return;
}
if ( $query->is_main_query() ) {
@mypacecreator
mypacecreator / style.css
Last active August 29, 2015 13:57
自作WordPressテーマのお決まりCSS
/*------------------------------------------------------------------------------
WordPress required
-------------------------------------------------------------------------------*/
/*------ entry ------*/
.hentry {
margin-bottom: 20px;
overflow: hidden;
}
@mypacecreator
mypacecreator / base.css
Last active August 29, 2015 13:57
おれおれ初期値CSS(暫定版)
@charset "utf-8";
/*------------------------------------------------------------------------------
reset.css 2014.03.18
-------------------------------------------------------------------------------*/
html { overflow-y: scroll; }
article, aside, canvas, details, figcaption, figure, header, footer, hgroup, menu, nav, section, summary { display: block; }
body, div, dl, dt, dd, ul, ol, li, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td { margin: 0; padding: 0; }
address, caption, cite, code, dfn, em, strong, th, var { font-style: normal; }
img, abbr, acronym, fieldset, a img { border: none; }
@mypacecreator
mypacecreator / functions.php
Created July 2, 2014 16:24
カテゴリーページにnoindexなど用カスタムフィールド追加(仮)
<?php
//add extra fields to category edit form hook
add_action ( 'edit_category_form_fields', 'extra_category_fields');
//add extra fields to category edit form callback function
function extra_category_fields( $tag ) { //check for existing featured ID
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1">meta robots指定</label></th>
@mypacecreator
mypacecreator / archive.php
Created July 2, 2014 17:47
アーカイプページで、カテゴリースラッグを元にタイトル画像を表示する(子カテゴリーの場合、自カテゴリーの画像を使う)
<?php if(is_category()): ?>
<h1><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php
$cat_id = get_query_var('cat');
$cat = get_category($cat_id);
echo $cat->category_nicename;
?>.jpg" alt="<?php single_cat_title(); ?>" /></h1>
<?php endif; ?>
@mypacecreator
mypacecreator / page.php
Last active August 29, 2015 14:03
page.phpで、子ページの場合も親ページの画像を使う
<?php
global $post;
if ( is_page() && $post->post_parent ): //子ページの場合
?>
<h1><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo get_page_uri($post->post_parent); ?>.jpg" alt="<?php echo esc_attr( get_the_title($post->post_parent) ); ?>" /></h1>
<?php else: //親ページの場合 ?>
<h1><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php echo get_page_uri($post->ID); ?>.jpg" alt="<?php the_title_attribute(); ?>" /></h1>
<?php endif; ?>
@mypacecreator
mypacecreator / header.php
Last active August 29, 2015 14:08
「初心者でもプラグインを使わずにtitle,meta keyword,descriptionを投稿ごとに変える」の修正版(ブログ用)
<?php if ( $post->my_description ): //meta descriptionの設定 ?>
<meta name="description" content="<?php echo esc_attr( $post->my_description ); ?>" />
<?php else: ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php endif; ?>
<?php if ( $post->my_keywords ): //meta keywordsの設定 ?>
<meta name="keywords" content="<?php echo esc_attr( $post->my_keywords ); ?>" />
<?php else: ?>
<meta name="keywords" content="デフォルトワード,デフォルトワード,デフォルトワード" />
@mypacecreator
mypacecreator / functions.php
Last active October 15, 2015 11:54
WordPress4.1以降でカスタムフィールドを使い、投稿ごとにタイトルタグを自由に編集するフィルターフックその2(サイト名はそのまま使う)
<?php
//コピペするなら3行目から↓
function mypace_custom_title( $title, $sep ){
if( is_singular() ){ //タイトルタグカスタマイズの範囲を条件分岐で指定
$post_id = get_the_ID(); //投稿IDを取得
$my_title = get_post_meta( $post_id, 'my_title', true ); //カスタムフィールドの値を取得
if( $my_title ){ //カスタムフィールドに値がある時
$blog_name = get_bloginfo('name');
$title = esc_html( $my_title . " $sep " . $blog_name ); //フィールドの値 | サイト名 の形式に
return $title;
@mypacecreator
mypacecreator / table.php
Last active December 21, 2015 06:18
get_postsで投稿を取得してtableで出力し、奇数列偶数列にclassをつけてCSSでしましまにできるように
<table>
<tr>
<th>日付</th>
<th>タイトル</th>
</tr>
<?php
$postcount = 1; //奇数偶数をカウント
$news = get_posts( array(
'post_type' => 'type',
'posts_per_page' => -1,