子カテゴリを非表示に

default-widgets.phpの533行目当たりから始まる部分が
カテゴリ一覧ウィジェットを生成する部分みたいです。

/**
* Categories widget class
*
* @since 2.8.0
*/
class WP_Widget_Categories extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories." ) );
parent::__construct('categories', __('Categories'), $widget_ops);
}
public function widget( $args, $instance ) {
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
$c = ! empty( $instance['count'] ) ? '1' : '0';
$h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
$d = ! empty( $instance['dropdown'] ) ? '1' : '0';
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$cat_args = array( 'orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
if ( $d ) {
$cat_args['show_option_none'] = __('Select Category');
/**
* Filter the arguments for the Categories widget drop-down.
*
* @since 2.8.0
*
* @see wp_dropdown_categories()
*
* @param array $cat_args An array of Categories widget drop-down arguments.
*/
wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) );
?>
<script type='text/javascript'>
/* <![CDATA[ */
	var dropdown = document.getElementById("cat");
	function onCatChange() {
		if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
			location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
		}
	}
	dropdown.onchange = onCatChange;
/* ]]> */
</script>
<?php
} else {
?>
<div id="right_categories">
<ul>
<?php
$cat_args['title_li'] = '';
/**
		 * Filter the arguments for the Categories widget.
		 *
		 * @since 2.8.0
		 *
		 * @param array $cat_args An array of Categories widget options.
		 */
wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) );
?>
</ul>
</div>
<?php
}
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
return $instance;
}
public function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = esc_attr( $instance['title'] );
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
<?php
}
}

559行目からの部分がカテゴリ一覧を抽出する部分の様で、ここに depth という引数を記入します。 depth => 1 が’第一階層のみを表示’のようです。なので

$cat_args = array('depth' => '1', 'orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);

で親カテゴリだけが表示されます。

分かりやすくなってました。

前回xcodeをアップデートしたらiPhoneシミュレータの場所が変わってて焦りました。

/Users/[User]/Library/Developer/CoreSimulator/Devices/[DeviceID]/data/Containers/Data/Application/[App]

ということでしたが、Containers というディレクトリが無くたどり着けません。
いろいろ探した結果、

Applications/Xcode.paa/Contents/Developer/Applications/

に入ってました。
な〜んだ。

あと普通にXcodeをDockから右クリックして”Open Developer Tool”から開けました。

Posted in mac

really simple csv importer を使うために csv をutf-8に変換するために nkf をインストールするために homebrew をインストールするために xcode をアップデート中。。。

wordpressでECサイトを構築する案件。
300件近くある商品情報を一括で記事として投稿する方法を探したところ、
really simple csv importer が良いらしいのですが、
試してみたところ、なぜかタイトルも記事も読み込まれません。


全部”タイトルなし”に。。。

どうやら文字コードの問題のようですので、csvファイルをutf-8に変換しようとするもlibreoffice のスプレッドシートでは無理な模様。
windowsだったら秀丸エディタでできてたのに。。。

フリーの良いエディタは無いかなと思って探していると nkf というコマンドがあるそうな。

コマンドラインから
nkf -w [入力ファイル名] > [出力ファイル名]
でutf-8に変換できるそうなのですが、インストールされておらず。

macのパッケージ管理システムも入ってないみたいで homebrew というのをインストールすることにする。

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /usr/local
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 203632, done.
remote: Compressing objects: 100% (54818/54818), done.
remote: Total 203632 (delta 147629), reused 203632 (delta 147629)
Receiving objects: 100% (203632/203632), 43.29 MiB | 90.00 KiB/s, done.
Resolving deltas: 100% (147629/147629), done.
From https://github.com/Homebrew/homebrew
* [new branch]      master     -> origin/master
HEAD is now at 61eb490 hashpump: add 1.1.0_1 bottle.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started

で言われた通りbrew doctorすると、

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: Your Xcode (5.1.1) is outdated
Please update to Xcode 6.0.1.
Xcode can be updated from the App Store.
$ 

なんかXcodeバージョンが古いそうなので今アップデート中。。。

再訪問に行って帰って来ると、

$ brew doctor
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: Git could not be found in your PATH.
Homebrew uses Git for several internal functions, and some formulae use Git
checkouts instead of stable tarballs. You may want to install Git:
brew install git
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license
$ sudo brew help
Password:
Example usage:
brew [info | home | options ] [FORMULA...]
brew install FORMULA...
brew uninstall FORMULA...
brew search [foo]
brew list [FORMULA...]
brew update
brew upgrade [FORMULA...]
brew pin/unpin [FORMULA...]
Troubleshooting:
brew doctor
brew install -vd FORMULA
brew [--env | config]
Brewing:
brew create [URL [--no-fetch]]
brew edit [FORMULA...]
open https://github.com/Homebrew/homebrew/wiki/Formula-Cookbook
Further help:
man brew
brew home
$

と、どうやらbrew はインストールされた模様。
さっそくnkfをインストールしようとすると、

$ brew install nkf
Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.

なんかXcode利用規約にagreeしないといけないとか。
Xcodeを立ち上げる→利用規約に同意する→閉じる
でもう一回インストール

$ brew install nkf
==> Downloading http://dl.sourceforge.jp/nkf/59912/nkf-2.1.3.tar.gz
Already downloaded: /Library/Caches/Homebrew/nkf-2.1.3.tar.gz
==> make CC=clang
==> make install prefix=/usr/local/Cellar/nkf/2.1.3 MKDIR=mkdir -p
/usr/local/Cellar/nkf/2.1.3: 4 files, 352K, built in 2 seconds
$ nkf --version
Network Kanji Filter Version 2.1.3 (2013-11-22)
Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).
Copyright (C) 1996-2013, The nkf Project.

と、どうやらインストールされた感じ。
nkf は Network Kanji Filter の略なんですね〜

で、今回のcsvファイルの文字コードを見ると

$ nkf -g testdata1_50.csv
Shift_JIS

これが原因?
さっそく変換してみます。

$ nkf -w testdata1_50.csv > testdata1_50utf8.csv
$ nkf -g testdata1_50.csv
Shift_JIS
$ nkf -g testdata1_50utf8.csv
UTF-8

再度 Really Simple CSV Importer から読み込ませると。。。
ブジ登録されてました!!
よかった〜

オプションは

-s Shift_JISへ変換
-w UTF-8へ変換
-e EUCへ変換

ちなみにEUC-JPは確認するとCP51932と表示されます 

Posted in mac

WordPress サイドバーの最新の投稿へアイキャッチ画像を表示する

widgetを編集するには default-widget.php を編集する

ほとんどのテンプレートでサイドバ-には「最近の投稿」を表示する機能がついてるみたいです。
ただ、投稿のタイトルしか表示されないみたいなので、今回はそこにサムネイルを表示させます。

wp-includes の中に default-widget.php というファイルがあり、これがウィジェットを生成してるみたいです。

645行目当たりから最近の投稿ウィジェットをどうかしてるみたいです。
こんな感じです↓

/**
* Recent_Posts widget class
*
* @since 2.8.0
*/
class WP_Widget_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "Your site&#8217;s most recent Posts.") );
parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_entries';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
public function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filter the arguments for the Recent Posts widget.
*
* @since 3.4.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the recent posts.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page'      => $number,
'no_found_rows'       => true,
'post_status'         => 'publish',
'ignore_sticky_posts' => true
) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<div id='right_new'>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php echo $args['after_widget']; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
<?php
}
}

717行目あたりの次の部分がリストを出力している部分になるようです。

<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>

レイアウト調整しやすいようにクラスを挿入して、だいたいこんな感じになりました。

<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li class='widgetNewItem'>
<div class='widgetNewItemImg'>
/*↓アイキャッチ画像を出力*/
<?php echo get_the_post_thumbnail(); ?>
</div>
<div class='widgetNewItemName'>
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
</div>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>

Really simple CSV importer を使う

WordPressECサイト的に使う案件がありまして、いろいろ参考になった記事をまとめておきます。

https://wordpress.org/plugins/really-simple-csv-importer/ (開発者さま)
http://learn-from.net/csvimport/
http://blog.gti.jp/post-4033/
http://memocarilog.info/wordpress/theme-custom/3200

Custom Field Templateについて
http://make.mapmap.biz/plugins/37/
http://mypacecreator.net/blog/archives/642/2

ありがとうございました〜