WordPress

[WordPress]doing_wp_cronが付いてたらリダイレクト

define(‘ALTERNATE_WP_CRON’, true);の時に発生

if(isset($_GET['doing_wp_cron'])){
    header('Location: '.get_current_url(false,array('doing_wp_cron')));
    exit;
}
public function get_current_url($remove_get_query = false,$exclude_keys = array())
{
    $suffix = $_SERVER['REQUEST_URI'];
    if($remove_get_query){
        $tmp = explode('?',$suffix);
        $suffix = array_shift($tmp);
    }else if(!empty($exclude_keys)){
        $tmp = explode('?',$suffix);
        $suffix = array_shift($tmp);
        $get = get_get_queries($exclude_keys);
        if(!empty($get))
            $suffix .= '?' . http_build_query($get);
    }
    return get_base_url($suffix);
}
public function get_get_queries($exclude_keys = array())
{
    $result = array();
    foreach ($_GET as $key => $value){
        if(!empty($exclude_keys) && in_array($key,$exclude_keys)){
            continue;
        }
        $result[$key] = $value;
    }
    return $result;
}
public function get_base_url($suffix = '')
{
    return (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $suffix;
}

[WordPress]Duplicate Post

カスタム投稿タイプで「複製」を実行すると「投稿」にリダイレクトされるのを修正

wp_redirect( add_query_arg( array( 'cloned' => 1, 'ids' => $post->ID), $sendback ) );

duplicate-post-admin.phpの315行目あたりの上記を下記へ変更

wp_redirect( add_query_arg( array( 'cloned' => 1, 'ids' => $post->ID, 'post_type' => $post->post_type), $sendback ) );

[WordPress]get_post_by_post_name

public function get_post_by_post_name($post_name, $post_type = '')
{
    global $wpdb;
    $sql = array();
    $sql[] = "SELECT";
    $sql[] = "p.*";
    $sql[] = "FROM $wpdb->posts AS p";
    $sql[] = "WHERE p.post_status = 'publish' AND p.post_name = '$post_name'";
    if (!empty($post_type))
        $sql[] = "AND p.post_type = '$post_type'";
    $result = $wpdb->get_results(implode(' ', $sql));
    if (empty($result))
        return null;
    return $result[0];
}

[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();?>

[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;?>

[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']);
	}
}