作成日: 2021/08/31 更新日: 2023/03/25 サイトの紹介と使い方
初めに
①ファイル:テーマCocoonのchildテーマのfunctions.phpのすべてのコードです。
②fuctionとショートコードは、使っても使わなくても将来の拡張があったときのために、配列引数$attsを用意しています。
分類
①後続作業で追加、修正を行うとき、検索しやすいようにfunctionやショートコードを分類します。
分類コード
function
ワードプレス本文では、直接使えません。
11xx:サイト固有の値を取得します。例えば、サイトの引っ越しの時、この分類コードの部分だけの修正で全てのコードが機能するようにします。
12xx:サイト内で共有のget 関数です。例えば、functions.php内でよく使う固有値を定義します。
13xx:サイト内で共有のset 関数です。例えば、functions.php内でよく使う固有値を定義します。
ショートコード
21xx:全ての記事あるいは多くの記事で共有するショートコードです。
例えば、パン屑リストです。
多くの記事にショートコードを書き足すのが面倒なので、パンくずリストなどに他の機能も併せ持つようにしました。
31xx:いくつかの記事で共有する定型文です。
81xx:デバッグ用のショートコードです。現在は内容はありません。
91xx:記事一覧の ショートコードです。
全てのコード
<?php //子テーマ用関数
if ( !defined( 'ABSPATH' ) ) exit;
//子テーマ用のビジュアルエディタースタイルを適用
add_editor_style();
define('WPLANG', 'ja');
//以下に子テーマ用の関数を書く
//<-------------------------------------------------------------------------->
//<--- 1100 はサイト特有(例えばホームドメイン名など)の値を返す分類コード
//引っ越しなどでサイトが移動したとき、この分類コードの値だけ変更する。--->
//<--- 1101
function my_get_homepage(){
return "https://aidendo-pilot.com";
}
// 1101 --->
// <--- 1102
function my_get_domain(){
return "aidendo-pilot.com";
}
// 1102 --->
//<-------------------------------------------------------------------------->
//<--- 1200はサイト内で共有のget 関数 --->
//<--- 1201
function my_get_block_paragraph_begin(){
$ret = "<!-- wp:paragraph --><br>";
return $ret;
}
// 1201 --->
//<--- 1202
function my_get_block_paragraph_end(){
$ret = "<br><!-- /wp:paragraph -->";
return $ret;
}
// 1202 --->
//<--- 1203
function my_get_block_code_begin(){
$ret = "<!-- wp:code -->";
$ret .= "<pre class=\"wp-block-code\"><code>";
return $ret;
}
// 1203 --->
//<--- 1204
function my_get_block_code_end(){
$ret .= "</code></pre>";
$ret .= "<!-- /wp:code -->";
return $ret;
}
// 1204 --->
//<--- 1205
function my_get_block_html_begin(){
$ret = "<!-- wp:html -->";
return $ret;
}
// 1205 --->
//<--- 1206
function my_get_block_html_end(){
$ret = "<!-- /wp:html -->";
return $ret;
}
// 1206 --->
//<-------------------------------------------------------------------------->
//<--- 1300はサイト内で共有のset 関数 --->
//<--- 1301
function my_set_list_a_tag( $arg1,$arg2 ){
$ret = "<li><a href=\"";
$ret .= my_get_homepage();
$ret .= $arg1;
$ret .= "\">";
$ret .= $arg2;
$ret .= "</a></li>";
return $ret;
}
// 1301 --->
//<--- 1302
function my_set_list_a_tag_article( $post ){
$arg1 ="/" . $post->post_title;
$arg2 = " [ ";
$date = new DateTime($post->post_date_gmt);
$arg2 .= $date->format("Y/m/d");
$arg2 .= ":";
$date = new DateTime($post->post_modified_gmt);
$arg2 .= $date->format("Y/m/d");
$arg2 .= " ] ";
$arg2 .= $post->post_title;
$ret = my_set_list_a_tag( $arg1,$arg2 );
return $ret;
}
// 1302 --->
//<-------------------------------------------------------------------------->
//<--- 2100は記事ごとで共有のショートコード関数 --->
//<--- 2101
add_shortcode('mysc_bread_crumb', 'my_bread_crumb');
function my_bread_crumb($atts){
$post = get_post();
$ret = "<ul id=\"my_top\" class=\"my-bread-crumb\">";
$ret .= my_set_list_a_tag( "/","menu" );
if( has_tag("81parent_cat",$post) ):
elseif( has_tag("82child_cat",$post) ):
$categorys = get_the_category();
$parent = get_the_category_by_ID( $categorys[0]->category_parent );
$arg1 = "/category_parent_" . $parent;
$ret .= my_set_list_a_tag( $arg1,$parent );
elseif( has_tag("21parent",$post) ):
$categorys = get_the_category();
$parent = get_the_category_by_ID( $categorys[0]->term_id );
$arg1 = "/category_parent_" . $parent;
$ret .= my_set_list_a_tag( $arg1,$parent );
elseif( has_tag("22child",$post) ):
$categorys = get_the_category();
$parent = get_the_category_by_ID( $categorys[0]->category_parent );
$arg1 = "/category_parent_" . $parent;
$ret .= my_set_list_a_tag( $arg1,$parent );
$child = get_the_category_by_ID( $categorys[0]->term_id );
$arg1 = "/category_child_" . $child;
if( stristr($child,"samplecode") ):
$child = "sampleCode";
elseif( stristr($child,"functioncode") ):
$child = "functionCode";
elseif( stristr($child,"class") ):
$child = "class";
endif;
$ret .= my_set_list_a_tag( $arg1,$child );
endif;
$ret .= "<li> /</li>";
$ret .= "</ul>";
$ret .= my_search_form($atts);
return $ret;
}
// 2101 --->
//<--- 2102
add_shortcode('mysc_show_child_category_on_parent', 'mysc_show_child_category_on_parent');
function mysc_show_child_category_on_parent($atts){
$ret = "<ul class=\"my-menu-list\">";
$categorys = get_the_category();
$args = array(
'type' => 'post',
'child_of' => $categorys[0]->term_id
);
$categories = get_categories( $args );
foreach ($categories as $category):
$arg1 = "/category_child_" . $category->cat_name;
if( stristr($category->cat_name,"samplecode") ):
$arg2 = "sampleCode";
elseif( stristr($category->cat_name,"functioncode") ):
$arg2 = "functionCode";
elseif( stristr($category->cat_name,"class") ):
$arg2 = "class";
else:
$arg2 = $category->cat_name;
endif;
$ret .= my_set_list_a_tag( $arg1,$arg2 );
endforeach;
$ret .= "</ul>";
return $ret;
}
// 2102 --->
//<--- 2103
add_shortcode('mysc_mail', 'my_mail');
function my_mail()
{
$ret = "aidendo@aidendo.sakura.ne.jp<br>";
$ret .= "<a href=\"#my_top\">to Top</a>";
return $ret;
}
// 2103 --->
//<--- 2104
add_shortcode('mysc_date', 'my_date');
function my_date($atts){
$post = get_post();
$ret = "created: ";
$date = new DateTime($post->post_date_gmt);
$ret .= $date->format("Y/m/d");
unset($date);
$ret .= " modified: ";
$date = new DateTime($post->post_modified_gmt);
$ret .= $date->format("Y/m/d");
unset($date);
return $ret;
}
// 2104 --->
//<--- 2105
add_shortcode('mysc_home', 'my_home');
function my_home($atts){
$ret = "<a href=\"";
$ret .= my_get_homepage();
$ret .= "/\">Menu</a>";
return $ret;
}
// 2105 --->
//<--- 2106
add_shortcode('mysc_search_form', 'my_search_form');
function my_search_form($atts){
$ret = my_get_block_html_begin();
$ret .= "<form method=\"get\" action=\"https://www.google.co.jp/search\" target=\"_blank\">";
$ret .= "<input type=\"hidden\" name=\"sitesearch\" value=\"";
$ret .= my_get_homepage();
$ret .= "\">";
$ret .= "<input type=\"text\" class=\"my-search-box\" name=\"q\" placeholder=\"keywords in site\" value=\"\">";
$ret .= "<button type=\"submit\" class=\"my-search-btn\">Go</button>";
$ret .= "</form>";
$ret .= my_get_block_html_end();
return $ret;
}
// 2106 --->
//<-------------------------------------------------------------------------->
//<--- 3100は定型文のショートコード関数 --->
//<--- 3101
add_shortcode('mysc_paid_variation', 'my_paid_variation');
function my_paid_variation($atts){
$args = array(
'posts_per_page' => 500,
'post_type' => 'post',
'post_status' => 'private'
);
$ps = get_posts( $args );
if( $ps ):
foreach( $ps as $p ):
if( has_tag("9901fixed",$p) ):
$ret = my_get_block_paragraph_begin();
$ret .= "<p>";
$ret .= $p->post_content;
$ret .= "</p>";
$ret .= my_get_block_paragraph_end();
endif;
endforeach;
endif;
return $ret;
}
// 3101 --->
//<-------------------------------------------------------------------------->
//<--- 8100はデバッグ用のショートコード関数 --->
//<-------------------------------------------------------------------------->
//<--- 9100は記事一覧のショートコード関数 --->
//<--- 9101
add_shortcode('mysc_article_list', 'my_article_list');
function my_article_list($atts){
$ret = "<ul class=\"my-link-list\"> ";
$post_self = get_post();
$categorys = get_the_category();
$args = array(
'posts_per_page' => 500,
'category' => $categorys[0]->term_id,
'orderby' => $atts[0],
'post_type' => 'post',
'order' => $atts[1]
);
$posts = get_posts( $args );
if( $posts ):
foreach ($posts as $post):
if( has_tag("81parent_cat",$post_self) ):
if( has_tag("21parent",$post) ):
$ret .= my_set_list_a_tag_article( $post );
endif;
elseif( has_tag("82child_cat",$post_self) ):
if( has_tag("22child",$post) ):
$ret .= my_set_list_a_tag_article( $post );
endif;
endif;
endforeach;
endif;
$ret .= "</ul>";
return $ret;
}
// 9101 --->
//<--- 9102
add_shortcode('mysc_article_list_csv', 'my_article_list_csv');
function my_article_list_csv($atts){
$fh = fopen($atts[0], "w");
if( $fh ):
$ret .= "<p>" . $atts[0] . "</p>";
$rec = "id,parent,child,tag,title\n";
fwrite( $fh,$row );
$args = array(
'posts_per_page' => 500,
'post_type' => 'post'
);
$my_posts = get_posts( $args );
foreach ($my_posts as $p){
$rec = strval($p->ID);
$rec .= ",";
$cats = get_the_category( $p->ID );
if( count($cats) ){
if( has_tag("21parent",$p) ):
$rec .= $cats[0]->name;
$rec .= ",,";
$rec .= "21parent,";
elseif( has_tag("22child",$p) ):
$par = get_category( $cats[0]->parent );
$rec .= $par->name;
$rec .= ",";
$rec .= $cats[0]->name;
$rec .= ",";
$rec .= "22child,";
elseif( has_tag("81parent_cat",$p) ):
$rec .= $cats[0]->name;
$rec .= ",,";
$rec .= "81parent_cat,";
elseif( has_tag("82child_cat",$p) ):
$par = get_category( $cats[0]->parent );
$rec .= $par->name;
$rec .= ",";
$rec .= $cats[0]->name;
$rec .= ",";
$rec .= "82child_cat,";
else:
$rec .= "no tag for category,,";
endif;
}else{
$rec .= "none category,,";
}
$rec .= $p->post_title;
$rec .= "\n";
fwrite( $fh,$rec );
}
$args = array(
'posts_per_page' => 500,
'post_type' => 'page'
);
$my_pages = get_posts( $args );
foreach ($my_pages as $p){
$rec = strval($p->ID);
$rec .= ",page,,,";
$rec .= $p->post_title;
$rec .= "\n";
fwrite( $fh,$rec );
}
fclose( $fh );
else:
$ret .= "file open error";
endif;
return $ret;
}
// 9102 --->
add_filter( 'run_wptexturize', '__return_false' );