Skip to content

Instantly share code, notes, and snippets.

@Ikaring
Ikaring / CustomWalkerPage
Created September 5, 2016 03:38
Custom walker class to display ACF image field to wp_list_pages
<?php
class My_Walker_Page extends Walker_Page {
public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
if ( $depth ) {
$indent = str_repeat( "\t", $depth );
} else {
$indent = '';
}
$css_class = array( 'page_item', 'page-item-' . $page->ID );
@Ikaring
Ikaring / gist:4a760193a17c78235c08
Last active August 29, 2015 14:23 — forked from tkc49/gist:5625795
記事毎に所属しているカスタム分類を表示した場合、カスタム分類の一覧表示の並び順と一緒の並びにする ※別途カスタム分類の並び順を制御するプラグインが必要
<?php
// functions.php に追記
function terms($a, $b){
if ( intval($a->term_order) == intval($b->term_order)) {
return 0;
}
return (intval($a->term_order) < intval($b->term_order)) ? -1 : 1;
}
@Ikaring
Ikaring / enqueue_script.php
Created January 29, 2015 15:55
enqueue script template
<?php
/**
* JavascriptとCSSの設定
*/
function my_scripts() {
global $post;
// テーマ全体で必要なJavascriptとCSS
wp_enqueue_style( 'theme-style', get_stylesheet_uri() );
wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/js/script.js', array( 'jquery' ), '', false );
@Ikaring
Ikaring / changeGalleryShortcodeDefaultColumns
Last active August 29, 2015 14:13
Change gallery shortcode default columns ( WordPress 4.1 as of writing )
<?php
/**
* Custom default column setting for gallery shortcode.
*
* Original galleryDefaults settings is in wp-includes/js/media-editor.js
* Need this setting for admin panel
*
*/
@Ikaring
Ikaring / set_thumbnail
Last active September 29, 2015 05:10
アイキャッチ画像がないときに記事内の最初の画像からサムネイルを取得する[WordPress]
<?php
//ループ内で使用
//アイキャッチ画像を$thumbに格納
if( has_post_thumbnail() ) {
//アイキャッチ画像が設定されているとき
$thumb = get_the_post_thumbnail( $post->ID, 'thumbnail' );
} else {
//記事に配置した画像にはwp-image-{id}というクラスが付くのを利用して記事に画像があるかどうか判断
preg_match('/wp-image-(\d+)/i' , $post->post_content, $thumbs);
if( empty( $thumbs ) ){
@Ikaring
Ikaring / wp-custom-post-type-archive.php
Last active August 29, 2015 13:58
WordPress Custom Post Type Archive Widget Plugin Fix
<?php
/*
Plugin Name: WordPress Custom Post Type Archive
Author URI: http://www.gingerlime.com
Description: Displays a monthly or yearly archive of posts for one specific custom post type.
Version: 1.0
Requires at least: 3.1
Author: Yoav Aner (based on Wordpress Category Archive by Hugh Mandeville)
License: GPL
/*
* fixHeight - jQuery Plugin
* http://www.starryworks.co.jp/blog/tips/javascript/fixheightjs.html
*
* Author Koji Kimura @ STARRYWORKS inc.
* http://www.starryworks.co.jp/
*
* Licensed under the MIT License
*
*/
@Ikaring
Ikaring / Fetch Permalink Shortcode
Last active December 26, 2015 09:39
Shortcode to fetch permalink by slug for page, category, post
@Ikaring
Ikaring / Get File Size Shortcode
Last active December 26, 2015 09:39
WordPress shortcode for retrieving (pdf or any) file size.
<?php
// [filesize file="filename.pdf"]
function get_file_size($atts) {
extract(shortcode_atts(array(
'file' => '',
), $atts));
//pdfの置き場所にあわせてパスを設定
$mfile=WP_CONTENT_DIR."/pdf/".$file;
if ( is_file($mfile) ){
@Ikaring
Ikaring / gist:6398310
Created August 31, 2013 13:50
Display post-meta key-value list using Custom Field Suite
<?php if ( in_category( array('category1','category2','category3')) ): ?>
<?php
$fields = $cfs->get(false, $post->ID, array('format' => 'raw'));
$list = '';
if( $fields ) {
$list = '<ul class="post-meta">';
foreach ($fields as $key => $value){
$list .= '<li>'. $key .':'. $value[0] .'</li>';
}
$list .= '</ul>';