Skip to content

Instantly share code, notes, and snippets.

@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 17, 2017 14:53
WP SitemanagerまたはPrime Strategy Bread Crumbの拡張
<?php
//パンくずのリッチスニペット対応
function rich_bread_crumb($output, $args) {
if ($args['type'] == 'list') {
$output = preg_replace('|<li\s+(.*?)>|mi','<li ${1} itemscope="itemscope" itemtype="https://data-vocabulary.org/Breadcrumb">',$output);
$output = preg_replace('|<li\s+class="(.*?current.*?)".*?>|mi','<li class="${1}">',$output);
$output = preg_replace('|<a\s+(.*?)>|mi','<a ${1} itemprop="url"><span itemprop="title">',$output);
$output = str_replace('</a>','</span></a>',$output);
}
return $output;
@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 / 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 / single.php
Last active July 13, 2016 04:43
single.phpで、カテゴリースラッグを元にタイトル画像を表示する(子カテゴリーの場合も、自カテゴリーの画像を使う)
<h1><img src="<?php echo get_template_directory_uri(); ?>/images/title_<?php
$cat = get_the_category();
$cat = $cat[0]; {echo $cat->category_nicename;}
?>.jpg" alt="<?php echo $cat->cat_name; ?>" /></h1>
@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 / functions.php
Last active March 1, 2017 08:46
WordPress ログイン時にダッシュボードではなくサイトトップへ飛ばす
<?php
//管理者以外はログイン成功後ダッシュボードではなくトップページへ飛ばす
function redirect_login_front_page() {
if( !current_user_can('administrator') ){
$home_url = site_url('', 'http');
wp_safe_redirect($home_url);
exit();
}
}
@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 / style.css
Last active August 29, 2015 13:57
自作WordPressテーマのお決まりCSS
/*------------------------------------------------------------------------------
WordPress required
-------------------------------------------------------------------------------*/
/*------ entry ------*/
.hentry {
margin-bottom: 20px;
overflow: hidden;
}
@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() ) {