[WordPress]get_postsテンプレ

get_postsテンプレ

$conditions = array(
    'post_type' => 'カスタム投稿タイプ',
    'posts_per_page' => -1,//-1:全て取得する
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'タクソノミー',
            'field' => 'slug',
            'terms' => 'ターム',
        )
    ),
);
$posts = get_posts($conditions);
<?php foreach($posts as $post):?>
<?php endforeach; wp_reset_postdata();?>

[CodeIgniter]カラム名一覧取得

カラム名一覧取得
application/core/MY_Model.phpとかに追記

public function get_column_names($table = '', $format = '%s')
{
    $names = $this->db->list_fields(empty($table) ? $this->name : $table);
    if(!empty($format)){
        foreach($names as &$name){
            $name = sprintf($format,$name);
        }
    }
    return $names;
}

[WordPress]記事を表示する最小処理

記事を表示する最小処理

<?php
//各環境に合わせて修正
require( dirname( __FILE__ ) . '/../../wp-blog-header.php' );
//表示する件数
$per_page = 4;
query_posts(array('posts_per_page'=>$per_page,'post_status'=>'publish'));
?>
<?php if(have_posts()):while(have_posts()):the_post(); ?>
<div>
	<?php $custom_fields = get_post_custom();?>
	<p><?php the_time('Y/m/d');?></p>
	<a href="<?php the_permalink();?>"><?php the_title();?></a>
	<p><?php the_content();?></p>
</div>
<?php endwhile;endif;?>

[CodeIgniter]テーブルのカラムのコメント取得

テーブルのカラムのコメント取得
/application/core/MY_Model.phpとかに実装

public function get_column_comments($table = '')
{
    if(empty($table)){
        $table = $this->name;
    }
    $query = "SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table' AND table_schema = '{$this->db->database}'";
    $result = $this->db->query($query);
    $result = $result->result('array');
    $tmp = array();
    foreach($result as $array){
        $tmp[$array['COLUMN_NAME']] = $array['COLUMN_COMMENT'];
    }
    return $tmp;
}

[WordPress]カスタムフィールドのファイル

カスタムフィールドのファイル

$cf = get_post_meta($post->ID);
$file_info = get_file_info_by_attachment_id($cf['cf_pdf'][0]);

/**
 * カスタムフィールドのファイル項目のIDからファイルの情報を返す
 * @param $attachment_id
 * @return string
 */
function get_file_info_by_attachment_id($attachment_id){
    $file_url = wp_get_attachment_url($attachment_id);
    $upload_dir = wp_upload_dir();
    $file_path = realpath($upload_dir['path'] . DIRECTORY_SEPARATOR . basename($file_url));
    $result = array(
        'url' => $file_url,
        'path' => $file_path,
        'realsize' => filesize($file_path),
    );
    return $result;
}

[WordPress]pagination

pagination
テーマのfunctions.phpに入れておくとよい。

function echo_pagination($params = array()){ 
	$params = array_merge(
		array(
			'per_page'       => 6,
			'link_count'     => 9,
			'left'           => '<li><a href="%url%">◀</a></li>',
			'right'          => '<li><a href="%url%">▶</a></li>',
			'format'         => '<li><a href="%url%">%no%</a></li>',
			'current_format' => '<li class="active"><a href="%url%">%no%</a></li>',
		),
		$params
	);
	global $paged;
	global $wp_query;
	if(empty($paged)){
		$paged = 1;
	}
	$pages = $wp_query->max_num_pages;
	if(!$pages){
		return;
	}

	if(1 < $paged){
		echo preg_replace('/%url%/',get_pagenum_link($paged-1),$params['left']);
	}

	$startPageNo = max(1, $paged-floor($params['link_count']*0.5));
	if($pages-$params['link_count'] < $startPageNo){
		$startPageNo = $pages-$params['link_count']+1;
	}
	for($i=$startPageNo;$i<$startPageNo+$params['link_count'] && $i <= $pages;$i++){
		$html = preg_replace('/%url%/',get_pagenum_link($i),$params[($i==$paged)?'current_format':'format']);
		echo preg_replace('/%no%/', $i, $html);
	}

	if($paged < $pages){
		echo preg_replace('/%url%/',get_pagenum_link($paged+1),$params['right']);
	}
}

[WordPress]404自動リダイレクトOFF

404自動リダイレクトOFF

/**
 * 下記の機能OFF
 * WordPressには本来存在しないURLを指定したとしても、WordPress側で推測してユーザーがアクセスしたかったであろうURLにリダイレクトする機能
 * @param $redirect_url
 * @return bool
 */
function disable_redirect_canonical($redirect_url) {
    if( is_404() ) {
        return false;
    }
    return $redirect_url;
}
add_filter('redirect_canonical','disable_redirect_canonical');

又は

remove_filter('template_redirect', 'redirect_canonical');

[CodeIgniter]バリデーション設定テンプレ

バリデーション設定テンプレ

public $validation = array(
    'login' => array(
        array(
            'field'  => 'email',
            'label'  => 'メールアドレス',
            'rules'  => 'trim|xss_clean|required',
            'errors' => array(
                'required' => '%sは必須です。',
                'custom_validation' => '%sかパスワードが異なります。',
            ),
        ),
        array(
            'field'  => 'password',
            'label'  => 'パスワード',
            'rules'  => 'trim|xss_clean|required',
            'errors' => array(
                'required' => '%sは必須です。',
            ),
        ),
    ),
    'update' => array(
        array(
            'field'  => 'company',
            'label'  => '会社名',
            'rules'  => 'trim|xss_clean|max_length[50]',
            'errors' => array(
                'max_length' => '%sは50文字以下にして下さい。',
            ),
        ),
        array(
            'field'  => 'zip',
            'label'  => '郵便番号',
            'rules'  => 'trim|xss_clean|regex_match[/[\d]{3}\-[\d]{4}/]',
            'errors' => array(
                'regex_match' => '%sは[半角数字3桁-半角数字4桁]で入力して下さい。',
            ),
        ),
        array(
            'field'  => 'address',
            'label'  => '住所',
            'rules'  => 'trim|xss_clean|max_length[100]',
            'errors' => array(
                'max_length' => '%sは100文字以下にして下さい。',
            ),
        ),
        array(
            'field'  => 'tel',
            'label'  => '電話番号',
            'rules'  => 'trim|xss_clean|regex_match[/^[\d]{2,5}\-[\d]{1,4}\-[\d]{4}$/]',
            'errors' => array(
                'regex_match' => '%sは[半角数字2~5桁-半角数字1~4桁-半角数字4桁]で入力して下さい。',
            ),
        ),
        array(
            'field'  => 'email',
            'label'  => 'メールアドレス',
            'rules'  => 'trim|xss_clean|valid_email|max_length[255]',
            'errors' => array(
                'valid_email' => '%sを正しく入力して下さい。',
                'max_length' => '%sは255文字以下にして下さい。',
            ),
        ),
        array(
            'field'  => 'password',
            'label'  => 'パスワード',
            'rules'  => 'trim|xss_clean|min_length[6]|max_length[12]|regex_match[/^[\w\-]+$/]',
            'errors' => array(
                'required' => '%sは必須です。',
                'min_length' => '%sは6文字以上にして下さい。',
                'max_length' => '%sは12文字以下にして下さい。',
                'regex_match' => '%sに使用出来るのは半角英数字、ハイフン、アンダースコアです。',
            ),
        ),
        array(
            'field'  => 'note',
            'label'  => '備考',
            'rules'  => 'trim|xss_clean|max_length[500]',
            'errors' => array(
                'max_length' => '%sは500文字以下にして下さい。',
            ),
        ),
    ),
);