Category: wordpress


2015/04/11

画像をアップロードする際に自動的にリサイズする

1024px以上のもをアップロードしても、アップロード時に縦横長い方を
【ダッシュボード→メディア設定】の大サイズ(1024pxがデフォルト)にリサイズします。

 

function.php

function otocon_resize_at_upload( $file ) {
 // $file contains file, url, type
 // array( 'file' => 'path to the image', 'url' => 'url of the image', 'type' => 'mime type' )

// resize only if the mime type is image
 if ( $file['type'] == 'image/jpeg' OR $file['type'] == 'image/gif' OR $file['type'] == 'image/png') {

// set width and height
 $w = intval(get_option( 'large_size_w' ) ); // get large size width
 $h = intval(get_option( 'large_size_h' ) ); // get large size height

// get the uploaded image
 $image = wp_get_image_editor( $file['file'] );

// if no error
 if ( ! is_wp_error( $image ) ){
 // get image width and height
 $size = getimagesize( $file['file'] ); // $size[0] = width, $size[1] = height

if ( $size[0] > $w || $size[1] > $h ){ // if the width or height is larger than the large-size
 $image->resize( $w, $h, false ); // resize the image
 $final_image = $image->save( $file['file'] ); // save the resized image
 }
 }

} // if mime type

return $file;

}
 add_action( 'wp_handle_upload', 'otocon_resize_at_upload' );

 

容量が大きすぎると、アップロード自体ができないので、アップロードのサイズの上限を上げておいた方がいいです。

アップロードファイルサイズの上限を変更|WordPress

 

2015/03/26

カスタムフィールドテンプレートでカレンダーを使って日付を入力する

[t_date]
label = 開催日
type = textfield
size = 35
date=true
dateFormat = yyyy-mm-dd
startDate = ‘2000-01-01’
dateFirstDayOfWeek = 0

2014/11/27

プラグインに頼らないページネーション|WordPress

function.php

/** ページネーション */
 function pagination($pages = '', $range = 4)
 {
 $showitems = ($range * 2)+1;

global $paged;
 if(empty($paged)) $paged = 1;

if($pages == '')
 {
 global $wp_query;
 $pages = $wp_query->max_num_pages;
 if(!$pages)
 {
 $pages = 1;
 }
 }

if(1 != $pages)
 {
 echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
 if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
 if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

for ($i=1; $i <= $pages; $i++)
 {
 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
 {
 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."#news' class=\"inactive\">".$i."</a>";
 }
 }

if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
 if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
 echo "</div>\n";
 }
 }

 

テーマファイル

<div class="navigation">
 <?php if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } ?>
</div>

 

スタイルシート

下記のスタイルシートで、自由なデザインに。。。

.navigation{
 width: 100%;
 height: auto;
 float: left;
}

 .pagination {
 clear:both;
 padding:20px 0;
 position:relative;
 font-size:11px;
 line-height:13px;
 }

.pagination span, .pagination a {
 display:block;
 float:left;
 margin: 2px 2px 2px 0;
 padding:6px 9px 5px 9px;
 text-decoration:none;
 width:auto;
 color:#555555;
 background: #cccccc;
 }

.pagination a:hover{
 color: #fff;
 background-color: #555555;
 }

.pagination .current{
 padding:6px 9px 5px 9px;
 background: #555555;
 color:#fff;
 }

2014/09/09

複数のサイドバーテンプレートを表示する方法

1.「sidebar-●●●.php」というファイルを作成

2.テンプレート内に以下を入力

<?php get_sidebar(‘●●●‘); ?>

 

2014/06/06

カテゴリー毎にsingle.phpのデザインを変更する方法

<?php
 $post = $wp_query->post;

if ( in_category('カテゴリーID') ) {
 /* カテゴリーID1 */
 include(TEMPLATEPATH.'/single-1.php');

}

else {
 /* それ以外 */
 include(TEMPLATEPATH.'/single-default.php');
 }
 ?>

“カテゴリーID”の投稿は[single1.php]のデザインを使用してそれ以外の投稿は、[single2.php]のデザインを使用する。
という内容。

single.phpのコードはこの記述のみ。
あとはsingle1.php、single2.phpにそれぞれ単一ページ用のコードを記述。

もっとたくさんデザインを使い分けたいとき

2つだけだはなく、もっと多くのデザインを使い分けたいときは

 <?php
 $post = $wp_query->post;

if ( in_category('カテゴリーID1') ) {
 /* カテゴリーID1 */
 include(TEMPLATEPATH.'/single-1.php');

}

elseif ( in_category('カテゴリーID2') ) {
 /* カテゴリーID2 */
 include(TEMPLATEPATH.'/single-2.php');

}

elseif ( in_category('カテゴリーID3') ) {
 /* カテゴリーID3 */
 include(TEMPLATEPATH.'/single-3.php');

}

elseif ( in_category('カテゴリーID4') ) {
 /* カテゴリーID4 */
 include(TEMPLATEPATH.'/single-4.php');

}

else {
 /* 上記に当てはまらない */
 include(TEMPLATEPATH.'/single-default.php');
 }
 ?>
2014/05/24

アイキャッチ画像をLightboxで展開するコード|WordPress

rel=”lightbox”の場合

<?php $thumbnail_id = get_post_thumbnail_id($post->ID); $image = wp_get_attachment_image_src($thumbnail_id, 'full'); ?>
 <a href="<?php echo $image[0]; ?>" rel="lightbox">
  <?php the_post_thumbnail('thumbnail'); ?>
 </a>
2014/01/05

特定のカテゴリー、表示件数の表示

IDで指定

<?php query_posts('showposts=5&cat=3'); ?>

 

 

スラッグで指定

<?php query_posts("showposts=10&category_name=news"); ?>

 

必ず下記で締める。

<?php wp_reset_query(); ?>

ページ送りはできないので、注意。

2013/12/20

ページスラッグを取得する|WordPress

<?php echo attribute_escape($post->post_name); ?>