created: 2021/08/09 modified: 2021/09/14
初めに
ファイル:Cocoon childのfunctions.php
関数の分類コード:91xx
次の記事のマイルールに従ってショートコードを作っています。
WordPressでマイルールを決めて記事の管理をする
ソースコード
カテゴリごとの記事一覧
概要:
①親カテゴリや子カテゴリページで使っている記事一覧のショートコードです。
不具合:
①get_posts( $args )の$argsで 'category_name' を使用すると、対象記事が1記事しかない場合、get_posts( $args ) は、0記事を返すようです。
対処:
① 'category' => $categorys[0]->term_idを使用するようにソースを変更しました。
修正:
①ソート順と種別を引数として渡せるように修正しました。
詳細は、テンプレートタグ/get postsを参照してください。
//<--- 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' => 50,
'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 --->
全記事の一覧をCSVに書き込む
概要:
①記事を管理するため記事情報をCSVに書き込みます。
②現在のCSVのディレクトリはVPSの/var/www/html/になります。
不具合:
①get_category_by_ID()を使用すると重大な問題が発生します。
対処:
①get_category()を使用しました。
//<--- 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' => 100,
'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' => 100,
'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 --->
メールアドレス: