AFFINGERは基本カスタム投稿タイプ非サポートなのだが、更新履歴だけはカスタム投稿タイプで管理することにする。
カスタム投稿タイプの追加
プラグインを使う方法もあるが、一つだけなのでfunction.phpに直接記入して追加する事とする。
参考サイト
以前まるっとコピペに使った参考サイトは数年ぶりに見に行ったらエロサイトに姿を変えていたのである程度参考になりそうなところをいくつか羅列
カスタム投稿をfunctions.phpでつくる方法について
追加したコード
ひとつめの参考サイトのコードに書き換えました。
/* ---------- カスタム投稿タイプを追加 ---------- */
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type(
'history',
array(
'label' => '更新履歴',
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'thumbnail',
'revisions',
),
)
);
}
カスタム投稿タイプを表示するショートコードを作成
トップに更新履歴としてカスタム投稿タイプの投稿一覧を表示する為にショートコードを作成する。
これもfunction.phpに追記する。
またしても以前の参考サイトは消えて無くなっていた…。
以下のサイトが参考になるかも?
https://hirashimatakumi.com/blog/5856.html
実際のコード(PHP)
function getNewInfo($atts) {
extract(shortcode_atts(array(
"num" => '', //最新記事リストの取得数 二つとも固定ページ側でも指定可能
), $atts));
global $post;
$oldpost = $post;
$myposts = get_posts('post_type=history&numberposts='.$num);
$retHtml='<dl class="news_list">';//クラス名は各要素、好きなものを付けて下さい。
foreach($myposts as $post) :
setup_postdata($post);
$retHtml.='<dt>';
$retHtml.=get_post_time( get_option( 'date_format' ));
$retHtml.='</dt>';
$retHtml.='<dd>';
$retHtml.='<a href="'.get_permalink().'">'.the_title("","",false).'</a>';
$retHtml.='</dd>';
endforeach;
$retHtml.='</dl>';
$post = $oldpost;
wp_reset_postdata();
return $retHtml;
}
add_shortcode("newsinfo", "getNewInfo");//newsinfoは、ショートコードの名前 getNewInfoは関数名
これで固定ページにはショートコードで「newsinfo num="5"」と記載すれば最大5件表示される。
実際のコード(CSS)
.news_list dl {
padding: 0px !important;
margin: 0px !important;
}
.news_list dt {
padding: 5px 0;
}
.news_list dd a {
text-decoration: none;
}
.news_list dd a:hover {
text-decoration: underline;
}
.news_list dd:before {
content: "\f138";
font-family: FontAwesome;
margin-right:3px;
margin-left: 3px;
}
.no-thumbitiran h5 {
font-size: 1.2em;
}
リストのdd:beforeのFont Awesomeアイコンがなぜか動かない…元のバージョンでは大丈夫だったのに…。
こちらに記載されてるとおりに改めて指定し直してうまく表示できるようになった。
AFFINGER6から対応バージョンがかわったようで、公式マニュアルに記載の仕方についても載ってた。FontAwesome5のコードはAFFINGERのテーマ管理から貼れるみたい。
一覧ページにタイトルが欲しい
AFFINGER公式マニュアルの通りに作ってもなぜか一覧ページにタイトルがない…。
色々試したところ、カスタム投稿タイプのスラッグと固定ページのスラッグが同じだとうまく動かないみたいで、パーマリンクを変更してその固定ページへのリンクを張って完了。
仕上げ
個別のシングルページが何故かNotFoundになっており動いてなかった。
忘れがちなパーマリンクの空更新をしたら治ったのでこれでカスタム投稿タイプ関連の作業は終了。