1,731 views
この記事は最終更新から 2834日 が経過しています。
本ブログのTOPページでは、全投稿履歴を表示している。
この機能は、テーマディレクトリ直下の function.php に該当コードを組み込んで実現している。
前回の (26) WordPressで独自CSS設定プラグインを作成 と同じように、テーマ依存の編集はテーマ変更時に引き継がれないため、これもプラグインを使ってテーマから切り離しておくことにした。
■プログラム
前々回の (25) WordPressで設定画面付きプラグインを作成 をほぼそのまま使用した。
PostHistory::proc_display()に一覧表を表示する機能を追加実装した点が大きな変更だ。
<?php
/* Plugin Name: PostHistory */
global $g_ary_options_PostHistory;
$g_ary_options_PostHistory = array(
'my_table_border' =>array('表枠線','2px solid'),
'my_table_background' =>array('表背景','transparent'),
'my_table_width' =>array('表幅','100%'),
'my_table_free' =>array('表自由記述',''),
'my_th_border' =>array('投稿タイトル表示列の枠線','1px solid'),
'my_th_background' =>array('投稿タイトル表示列の背景','transparent'),
'my_th_fontsize' =>array('投稿タイトル表示列の文字サイズ','1rem'),
'my_th_free' =>array('投稿タイトル表示列の自由記述',''),
'my_td_border' =>array('日時表示列の枠線','1px solid'),
'my_td_background' =>array('日時表示列の背景','transparent'),
'my_td_fontsize' =>array('日時表示列の文字サイズ','0.9rem'),
'my_td_free' =>array('日時表示列の自由記述',''),
'my_New_img_url' =>array('Newアイコン画像のURL',''),
'my_New_img_day' =>array('Newアイコン画像の表示期間[日]','7') );
////////////////////////////////////////////////////////////////////////////////
if (class_exists('PostHistory')) {
$obj = new PostHistory();
}
////////////////////////////////////////////////////////////////////////////////
class PostHistory {
//////////////////////////////////////////////////////////////////////////////
public function __construct() {
global $g_ary_options_PostHistory;
add_shortcode('my_post_history', array($this, 'proc_display'));
add_action('admin_menu', array($this,'proc_add_menu'));
add_filter('widget_text', 'do_shortcode');
register_activation_hook( __FILE__, array($this, 'proc_plugin_activate'));
register_deactivation_hook(__FILE__, array($this, 'proc_plugin_deactivate'));
}
//////////////////////////////////////////////////////////////////////
public function proc_plugin_activate(){
global $g_ary_options_PostHistory;
foreach($g_ary_options_PostHistory as $option => $ary_prm){
add_option($option, $ary_prm[1]);
}
}
//////////////////////////////////////////////////////////////////////
public function proc_plugin_deactivate(){
global $g_ary_options_PostHistory;
foreach($g_ary_options_PostHistory as $option => $ary_prm){
delete_option($option);
}
}
//////////////////////////////////////////////////////////////////////////////
public function proc_display($atts) {
global $g_ary_options_PostHistory;
// $atts['nmax'] : 表示最大数
// $atts['catg'] : 表示カテゴリー (指定なしならばすべてを対象とする)
$nmax = (isset($atts['nmax']))? $atts['nmax'] : 100;
$catg = (isset($atts['catg']))? $atts['catg'] : "";
$recent_posts = wp_get_recent_posts($nmax);
$html_trs="";
foreach($recent_posts as $post){
if($post['post_status'] != "publish"){ // 「公開」状態でなければスキップ
continue; // skip to the next
}
if($catg != ""){
$ary_catg = get_the_category($post["ID"]);
foreach($ary_catg as $cCatg){
if($cCatg->slug != $catg){
goto tagLoopEnd; // skip to the next
}
}
}
$link = get_permalink($post["ID"]);
//------------------------------------------------------------------------
// 投稿日時文字列を作成
//------------------------------------------------------------------------
$date_bin = strtotime($post['post_date']);
$disp_date = strftime("%Y-%m-%d %H:%M", $date_bin);
//------------------------------------------------------------------------
// [UP]アイコンを連結
$up_img_url = esc_attr(get_option('my_New_img_url'));
if($up_img_url == ""){
$up_img_url = plugins_url('up.gif',__FILE__);
}
$up_img_day = esc_attr(get_option('my_New_img_day'));
if($up_img_url != ""){
if(time() - $date_bin < 24*60*60*$up_img_day){
$disp_date = "<img src='{$up_img_url}' /> {$disp_date}";
}
}
//------------------------------------------------------------------------
$html_trs .= <<< EOM
<tr>
<th><a href="{$link}">{$post['post_title']}</a></th>
<td>{$disp_date}</td>
</tr>
EOM;
tagLoopEnd:
}
$ary_option = array();
foreach($g_ary_options_PostHistory as $option => $ary_prm){
$ary_option[$option] = esc_attr(get_option($option));
}
return <<< EOM
<style>
#my_post_history table{
border: {$ary_option['my_table_border']};
background: {$ary_option['my_table_background']};
width: {$ary_option['my_table_width']};
{$ary_option['my_table_free']}
}
#my_post_history th{
border: {$ary_option['my_th_border']};
background: {$ary_option['my_th_background']};
font-size: {$ary_option['my_th_fontsize']};
{$ary_option['my_th_free']}
}
#my_post_history td{
text-align: right;
border: {$ary_option['my_td_border']};
background: {$ary_option['my_td_background']};
font-size: {$ary_option['my_td_fontsize']};
{$ary_option['my_td_free']}
}
#my_post_history td img{
vertical-align:middle;
}
@media (max-width: 767px) {
#my_post_history td img{
height: 0.9rem;
}
}
</style>
<div id="my_post_history">
<table>
{$html_trs}
</table>
</div>
EOM;
}
//////////////////////////////////////////////////////////////////////////////
public function proc_add_menu() {
add_menu_page('PostHistory Settings', 'PostHistory Settings', 'administrator', __FILE__, array($this,'proc_display_setting_page'));
add_action( 'admin_init', array($this,'proc_register_mysettings'));
}
//////////////////////////////////////////////////////////////////////
public function proc_register_mysettings() {
global $g_ary_options_PostHistory;
foreach($g_ary_options_PostHistory as $option => $ary_prm){
register_setting('PostHistory-settings-group', $option, array($this,'proc_handle_sanitization'));
}
}
//////////////////////////////////////////////////////////////////////
public function proc_handle_sanitization($txt){
return $txt; //sanitize_text_field($txt);
}
//////////////////////////////////////////////////////////////////////
public function proc_display_setting_page(){
$html_input = "";
global $g_ary_options_PostHistory;
foreach($g_ary_options_PostHistory as $option => $ary_prm){
$value = esc_attr(get_option($option));
$html_input .= <<< EOM
<tr valign="top">
<th scope="row">{$ary_prm[0]}</th>
<td><input type="text" name="{$option}" value="{$value}" style="width:100%" /></td>
</tr>
EOM;
}
?>
<div class="wrap">
<h2>PostHistory Setting</h2>
<form method="post" action="options.php">
<?php settings_fields('PostHistory-settings-group'); ?>
<?php do_settings_sections('PostHistory-settings-group'); ?>
<table class="form-table">
<?php echo $html_input; ?>
</table>
<?php
submit_button();
?>
</form>
</div>
<?php
}
} // end of class
?>
■使い方
[my_post_history]と書くと、全カテゴリが表示対象となる。
[my_post_history catg=”{カテゴリ名}”]と書くと、指定カテゴリが表示対象となる。
[my_post_history nmax=”{最大表示件数}”]と書くと、指定件数まで表示される。
※今回は複数カテゴリの指定に対応していない。
早速当サイトのTOPページで使い始めた。
アクセス数(直近7日): ※試験運用中、BOT除外簡易実装済2026-06-23: 0回 2026-06-22: 2回 2026-06-21: 4回 2026-06-20: 1回 2026-06-19: 0回 2026-06-18: 0回 2026-06-17: 3回