コメントデータの取得を操作する comments_template_query_args

wp-inclludes/comment-template.phpのなかにコメントの取得とソートに関する変数 &comment_argsがあります。

$comment_args = array(
 'orderby'                   => 'comment_date_gmt',
 'order'                     => 'DSC',
 'status'                    => 'approve',
 'post_id'                   => $post->ID,
 'no_found_rows'             => false,
 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
);

このあといろいろごにょごにょして。
コメントのデータを取得しているようです。

フックの一覧を見るとcomments_template_query_argsというのがあるので(version4.5~)ここにフックしてorderbyやorderを変更できそうです。
あと未承認のコメントの表示とかもできそうです。

wp-inclludes/comment-template.phpの該当箇所が(↓)
現在のバージョンは5.1なので1444行目にありますね。

 $comment_args  = apply_filters( 'comments_template_query_args', $comment_args );
 $comment_query = new WP_Comment_Query( $comment_args );
 $_comments     = $comment_query->comments;

コメントを表示する場所で↑を変更します。

//コメント取得のための変数を納めた配列
function edit_comment_query( $comment_args ) {
 $comment_args['order'] = 'ASC';
  return $comment_args;
}
//comments_template_query_argsへフックする
add_filter( 'comments_template_query_args', 'edit_comment_query' );

とりあえず今日はここまで。。。

/wp 以下に作成したWPサイトのトップページだけは独立したページを使用します。


↑のindex.htmlをトップに表示させます。

1) テンプレートファイル template_toppage.phpを作成します。
内容は以下の通り

<?php
/*
Template Name: Top Page
*/
readfile(ABSPATH . '/index.html');
?>

ABSPATHはwordpressがインストールされたフォルダのパスを示します。
ですのでindex.htmlがwp/フォルダ以下にあっても上記の指定でOKです。

2) WP管理画面から固定ページを新規に作成します。
タイトルは「トップページ」などてきとうでかまいません。

テンプレートに先ほど作成したTop Pageが表示されますのでこれを選択します。

3) 「設定」=> 「表示設定」=>「フロントページの表示」で「固定ページ」にチェックをし、先ほど作成したページを選択します。

thumbnail,medium,large,fullから選ぶ

<?php the_post_thumbnail('画像のサイズ'); ?>

画像のサイズは下記から指定します。

thumbnail サムネイル
medium
large
full 原寸大

選べるのは4種類だけですが、管理画面からそれぞれのサイズの数値を指定できます。
「設定」→「メディア」から設定できます。

子カテゴリを非表示に

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);

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

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

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