NO IMAGE

WordPress パーマリンクをカテゴリー/年/で表示させたい 検証

  • 2017年2月25日
  • 2017年2月27日
  • 未分類
  • 1306view


実際の実装はこちらここは検証のページです。

パーマリンクは/%category%/%year%/%post_id%/ となっているが、
要望として、

  • カテゴリー一覧ページに年代出せる機能
  • URLがhttp://kato.space/カテゴリー/2017/ とさせたい。
  • ページャーがクリックで、http://kato.space/カテゴリー/2017/page/4/ となるので、
    単純にURLを取得するだけだとエラーになる。

まずはcategory-スラッグ.phpを用意する
実際のパーマリンクを取得してみる。

一度、get値取るか?

検証1

http://kato.space/wp/wordpress/2017/ としたとき、
print_r($_GET);では配列は0 Array ( )となる。
パーマリンクを基本にすると、
http://kato.space/wp/?cat=5
Array ( [cat] => 5 ) となり、
http://kato.space/wp/?cat=5&m=201702
http://kato.space/wp/?cat=5&m=201302
http://kato.space/wp/?m=201302
上記であれば取得できる。
Array ( [m] => 201302 )

検証2

年代ごとの取得は可能のよう
@See http://qiita.com/noqua/items/a26cfcb378300ba20bb1
archiveFunc() という関数がある模様

<?php
/*==============================================
	年別アーカイブ表示
==============================================*/
function archiveFunc($year){
	if(have_posts()) : query_posts('posts_per_page=-1&year='.$year.'');
?>
	<div class="archives">
	<h2><?php print $year; ?></h2>
	<dl>
<?php while(have_posts()) : the_post(); ?>
	<dt><?php the_time('Y-m-d'); ?></dt>
	<dd><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></dd>
<?php endwhile; ?>
	</dl>
	</div>
<?php endif; wp_reset_query(); ?>
<?php
}
?>

@See 年別 アーカイブの表示方法について
@See  WordPressのアーカイブページで投稿を年度別に一覧表示したかった。

<?php
    function archiveFunc($year){
        $newslist = new WP_Query( array(
        'post_type' => 'works',
        'posts_per_page' => -1,
        'year' => $year
        ));
?>
<h3><?php echo $year; ?>年</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
    <tr>
        <th width="15%">■期間</th>
        <th width="20%">■発注者</th>
        <th>■工事名</th>
    </tr>
    <?php
        if($newslist->have_posts()):
        while($newslist->have_posts()):
        $newslist->the_post();
    ?>
    <tr>
        <td><?php the_field('works_period'); ?></td>
        <td><?php the_field('works_orderer'); ?></td>
        <td><strong><?php the_title(); ?></strong></td>
    </tr>
<?php
    endwhile;
    endif;
    wp_reset_postdata();
?>
</tbody>
</table>
<?php
    }
    $thisyear = date('Y');
    for ($year=$thisyear; $year >= 2010; $year--) { 
        archiveFunc($year);
    }
?>

 

検証3

単純にカテゴリーページで年代でフィルタリンクかければ良いのでは?
問題は/page/2/とかの問題

$args = array(
	//'posts_per_page'   => 5,
	'offset'           => 0,
	'category'         => '',
	'year'             => '2017',
	'category_name'    => '',
	'orderby'          => 'date',
	'order'            => 'DESC',
	'include'          => '',
	'exclude'          => '',
	'meta_key'         => '',
	'meta_value'       => '',
	'post_type'        => 'post',
	'post_mime_type'   => '',
	'post_parent'      => '',
	'author'	   => '',
	'post_status'      => 'publish',
	'suppress_filters' => true 
);
$posts = get_posts($args);

上記で取得2017年とかは絞込みは可能

なので、

$year = '2013';
$args = array(
	'category'         => '5',
	'year'             => $year,
);

この$yearをgetから取得できれば完璧か。
http://kato.space/wp/wordpress/2017/
もしくは
http://kato.space/wp/wordpress/2017/
http://kato.space/wp/wordpress/page/2/
要望は

http://kato.space/wp/wordpress/2017/
http://kato.space/wp/wordpress/2017/page/2/

だが

URLから抜き出すしかないのか?
WordPressで現在のURLのパスを配列で取得する
@See https://watanabemitsutoshi.com/wordpress-get-url-path

 

検証4

躓き あれ?パーマリンク

/%category%/%year%/

で、
http://kato.space/wp/wordpress/2013/
http://kato.space/wp/wordpress/2016/
でもいけるが、リンクが取得できない。
/%category%/%year%/%post_id%/ じゃなきゃだめだよね。

 

検証5

とりあえず、

$args = array(
	'category'         => '5',
	'year'             => $year,
);
$posts = get_posts($args);
global $post;
if($posts): foreach($posts as $post): setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a><br>
<?php endforeach; endif;?>

という感じで取得できるし、
URLが

http://kato.space/wp/wordpress/2013/
http://kato.space/wp/wordpress/2013/page/2/

http://kato.space/wp/wordpress/2017/
http://kato.space/wp/wordpress/2017/page/2/

上記でもアクセスできるので、あとは、
<?php wp_get_archives(‘type=yearly’); ?> このリンクを修正したい。


http://kato.space/wp/wordpress/2013/page/2/でページ移管ができない

 

調べると、http://kato.space/wp/wordpress/2017/もページャーが効かない

$args = array(
	'category'         => '5',
	'year'             => $year,
	'paged'            => $paged

現在のページが不明になっていた模様

@See http://webimemo.com/wordpress/1813

 

 

検証6

get_archives_linkを加工する

通常だと
http://kato.space/wp/date/2016/ となる
これを ↓
http://kato.space/wp/カテゴリー名/2016/ のリンクとしたい。

@See 「wp_get_archives()」で出力されるHTMLタグをカスタマイズ[WordPress]
純粋に年代記事があるか検索はできないのだろうか?
wp_get_archives ではクエリ文を投げている。

$query = "SELECT YEAR(post_date) AS `year`, 
count(ID) as posts FROM $wpdb->posts $join 
$where GROUP BY YEAR(post_date) ORDER BY post_date $order

じゃ、これ普通にクエリ作ればいいのか

SELECT * FROM `wp_posts` WHERE DATE_FORMAT(`post_date`, ‘%Y’)=2013;

SELECT DATE_FORMAT( `post_date` , "%Y" ) AS y, COUNT( id ) AS count
FROM `wp_posts` 
GROUP BY DATE_FORMAT( `post_date` , "%Y" ) 

SELECT DATE_FORMAT( `post_date` , “%Y” ) AS y, COUNT( id ) AS count FROM `wp_posts` GROUP BY DATE_FORMAT( `post_date` , “%Y” )

@See

mysqlでdate型を月毎に集計する
http://y0m0r.hateblo.jp/entry/20140618/1403105741

じゃ、これで関数を作るか。

検証7

コメンドアウトはテストで出力、そのままちと残す。

function years_li_links($string){
$sql = "SELECT DATE_FORMAT( `post_date` , \"%Y\" ) AS y, COUNT( id ) AS count\n"
    . "FROM `wp_posts` \n"
    . "GROUP BY DATE_FORMAT( `post_date` , \"%Y\" ) ";
	global $wpdb;
$rows = $wpdb->get_results($sql);
print_r($rows);
echo '<hr>';
foreach( $rows as $key => $value) {
echo '<li>';
//echo "<a href='topic'>";
//echo site_url();
//echo $string.'/';
//echo $rows[$key]->y.'/';
$li_link = site_url().'/'.$string.'/'.$rows[$key]->y.'/';
//echo $li_link;
echo "<a href='{$li_link}'>";
echo $rows[$key]->y;
echo '</li>';
  }
}

検証8

ページャーで空のページがある。
@See http://xtra-blog.net/pagination/

@See https://2inc.org/blog/2012/09/06/1947/
どうやら「posts_per_pageの値が「1ページに表示する最大投稿数」よりも小さいとページングが正しく動作しない」というのは定説らしい。

$args = array(
	'posts_per_page'   => get_option('posts_per_page'),
	'category'         => '5',
	'year'             => $year,
	'paged'            => $paged,

);

 

上記で解決。

 

 

 

 

 

 

http://kato.space/wp/wordpress/2017/
http://kato.space/wp/wordpress/2017/page/2/
でも動作するな。
では、%year%が取得できればいいのか

 

パーマリンクのテスト
wp_get_archives

デモ
http://kato.space/test_archives.php

関数リファレンス/get archives link

http://kato.space/wp/date/2017/page/4/
これを
http://kato.space/カテゴリー/2017/page/4/
として動作させたい

まずはcategory-スラッグ.phpを用意する

NO IMAGE
最新情報をチェックしよう!

    Warning: Trying to access array offset on value of type bool in /home/xsvx1010357/kato.space/public_html/wp/wp-content/themes/the-thor/template-parts/single-prevnext.php on line 37

    Warning: Trying to access array offset on value of type bool in /home/xsvx1010357/kato.space/public_html/wp/wp-content/themes/the-thor/template-parts/single-prevnext.php on line 38

    Warning: Trying to access array offset on value of type bool in /home/xsvx1010357/kato.space/public_html/wp/wp-content/themes/the-thor/template-parts/single-prevnext.php on line 39

  • Warning: Trying to access array offset on value of type bool in /home/xsvx1010357/kato.space/public_html/wp/wp-content/themes/the-thor/template-parts/single-prevnext.php on line 83

    Warning: Trying to access array offset on value of type bool in /home/xsvx1010357/kato.space/public_html/wp/wp-content/themes/the-thor/template-parts/single-prevnext.php on line 84

    Warning: Trying to access array offset on value of type bool in /home/xsvx1010357/kato.space/public_html/wp/wp-content/themes/the-thor/template-parts/single-prevnext.php on line 85
>最強のWordPressテーマ「THE THOR」

最強のWordPressテーマ「THE THOR」

システムの構築・保守運用「   」 社内システム担当が欲しいが、専属で雇うほどの仕事量はない。 必要な時に必要なだけ頼りたいというお悩みを持つ企業様へ専門知識を持って対応を行っております。 サーバから各種システムまで自社・他社で構築されたシステムに対してサポートを行っております。

CTR IMG