メモ
テンプレート側から取る手段として
https://contactform7.com/ja/getting-default-values-from-shortcode-attributes/
こっちも
<?php echo do_shortcode('[ショートコードの文字列]'); ?>
メモ
テンプレート側から取る手段として
https://contactform7.com/ja/getting-default-values-from-shortcode-attributes/
こっちも
<?php echo do_shortcode('[ショートコードの文字列]'); ?>
部分一致扱いやんけ!
えらい苦労した。
完全一致が=としてるなら、まぁそういう場合もあるよなと考えるべきだったのかどうなのか・・・。
つくった。
カスタム投稿用
タクソノミーを使った記事の出力。
自身が持つタクソノミーと同一のIDどれか1つでも持っている記事を自身を除く$post_count件をランダムで出力する。
//関連記事の取得 カスタム投稿用
function get_related_custom_posts($post_id,$post_type,$post_count){
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
foreach($taxonomies as $taxonomy){
$terms[$taxonomy->name] = get_the_terms($post_id,$taxonomy->name);
}
$tax_query['relation'] = 'OR';
foreach($terms as $term_name => $term_array){
//$tax_query[$term_name] = $term_array;
foreach($term_array as $term){
$tax_query[][] = array(
'taxonomy' => $term_name,
'field' => 'term_id',
'terms' => $term->term_id,
);
}
}
$args['posts_per_page'] = $post_count;
$args['post_type'] = $post_type;
$args['tax_query'] = $tax_query;
$args['post_status'] = 'publish';
$args['post__not_in'] = array($post_id);
$args['orderby'] = 'rand';
$related_post = new WP_Query( $args );
return $related_post->posts;
}
日付の出力方法をよく忘れるのでメモ
date('Y/m/d',strtotime($post->post_date));
//終日設定がうまく起動しないので時間をつい1日分の時間を自動で追加 (それに伴い日付の表示をCSSで削除 $start_date = $event_schedule['start_date']."T00:00:00+09:00"; $end_date = $event_schedule['end_date']."T23:59:59+09:00";
WordPressでAjaxをつかってfullcalendarにイベントを掲載しようとしていたときに
ドツボにはまりそうだったのでメモ書き。
現在でもスマートな解決にはなってない。
phpでjonsを返しているが
fullcalendarのallDay(終日設定)を入れても最終日の最後までイベントが開催されていることになっていない。
原因としてはphp側でデータを引っ張って来ている時に足りない部分をfullcalendarが自動変換することが原因のようなことらしい?
現状としてはソースを掲載しているように時間を付け足し
画面上に表示される時刻の表記をCSSで消しているという美しくない実装で終わっている。
う〜〜〜ん。
手書きで書いたjson形式のデータだと問題ないから 手書きでも問題あったわ。
もしかしたらphp側のjson_encodeの問題なのかもしれない。
もうちょっとこれは見てみようか。
画像が表示されないんだよ!!!
っていうのがwordpressのバージョンアップを行うたびにあって
その度にやっていた作業を忘れていたのでメモ
変更ファイル:admin-ajax.php
//これを
@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
//こう
if(in_array($_POST['action'], array('query-attachments', 'send-attachment-to-editor'))){
@header('Content-Type: application/json; charset=' . get_option('blog_charset'));
}else{
@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
}
これをやらないといけない要因っていうのが
サーバーの設定によってはちゃんとJSON形式で送らないとデータを処理しない?とかそんな感じの状況がある。
これはfunctions.phpのほうでフックできないかなぁ・・。
バージョン更新するたびに本体ファイルの更新をしないといけないのはどうかも思うし
こういう状況があるって把握してそうなwordpressの開発者の人が本体ファイルを変更してくれるといいのだけどもなぁ。
OGPタグを配列で出すようなものを作ろうと思い現状のテンプレートとの兼ね合いでうんうんと
悩んでいるところ。
参考にしているところはbody_class()などなど。
カテゴリーなんかは説明文を使ってソートをかけているからその辺をどうするか。
カテゴリーの説明文を出力するようにすると単純に出した場合は数字になるし
並び替えを行うのにテンプレートファイルを触らないといけないなどの環境にはしたくはないかなぁ。
プラグインで対応はできるのだけども自分で考えてやっておきたい。
//OGPタグ用
function ogp_tags(){
$ogp = array();
$type = 'blog';
$image = 'まだ';
$no_image_file_url = '/img/ogp_no-photo.png';
$front_home = get_option('page_on_front');
if(is_front_page()){ // フロントページならば
$post = get_post($post_ID);
$text = get_bloginfo('description');
$title = get_the_title($post->ID);
$thumbnail_id = get_post_thumbnail_id();
if(has_post_thumbnail()){
$thumbnail_array = wp_get_attachment_image_src($thumbnail_id,'ogp_thm');
$image = $thumbnail_array[0];
}else{
$image = $no_image_file_url;
}
}else if(is_category()){ // カテゴリーページであるかどうか
$text = category_description();
$title = single_cat_title( '' , false );
}else if(is_tax()){ // タクソノミーページであるかどうか
$text = term_description();
$title = single_term_title( '' , false );
}else if(is_archive()){ // アーカイブページであるか
if(is_month( )){ // 月アーカイブ
$text = "is_month";
}else if(is_day()){ // 日アーカイブ
$text = "is_day";
}else{ // それ以外のアーカイブ
$text = "is_archive";
}
$title = get_the_archive_title();
}else{ //それ以外
$post = get_post($post_ID);
$post_type = $post->post_type;
if($post_type == "post" || $post_type == "page"){ // 投稿タイプの判断
if($post->post_excerpt){ //記事に抜粋があるかどうか
$text = $post->post_excerpt;
}else{
$text = $post->post_content;
}
}else{
$text = $post->post_content;
}
$title = strip_tags($post->post_title);
$thumbnail_id = get_post_thumbnail_id();
if(has_post_thumbnail()){
$thumbnail_array = wp_get_attachment_image_src($thumbnail_id,'ogp_thm');
$image = $thumbnail_array[0];
}else{
$image = $no_image_file_url;
}
}
$text = strip_tags(preg_replace('/(\s| )/','',$text));
$ogp['og:description'] = substr($text, 0, 190);
$ogp['og:site_name'] = get_bloginfo('name');
$ogp['og:type'] = $type;
$ogp['og:url'] = get_the_permalink();
$ogp['og:title'] = $title;
$ogp['og:image'] = $image;
print_r($ogp);
}
したが修正版 外部から言語ファイルとってきたりしてそのままの実装には案の定ならなかった。
//OGPタグの出力
function site_ogp(){
global $cong;
$ogp = array();
$type = 'website';
$image = 'まだ';
$title = '';
$site_url = home_url();
$site_descriotions = $cong['meta2']['description'];
$url = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$template_url = get_bloginfo('template_url');
$no_image_file_url = $template_url.'/img/ogp_img.png';
$front_home = get_option('page_on_front');
if(is_front_page()){ // フロントページならば
$post = get_post($post_ID);
$url = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"];
$text = get_bloginfo('description');
$title = get_the_title($post->ID);
$thumbnail_id = get_post_thumbnail_id();
if(has_post_thumbnail()){
$thumbnail_array = wp_get_attachment_image_src($thumbnail_id,'ogp_thm');
$image = $thumbnail_array[0];
}else{
$image = $no_image_file_url;
}
}else if(is_category()){ // カテゴリーページであるかどうか
$text = category_description();
$title = single_cat_title( '' , false );
$text = $site_descriotions['is_category'];
$image = $no_image_file_url;
}else if(is_tax()){ // タクソノミーページであるかどうか
$text = term_description();
$title = single_term_title( '' , false );
$text = $site_descriotions['is_tax'];
$image = $no_image_file_url;
}else if(is_archive()){ // アーカイブページであるか
if(is_month( )){ // 月アーカイブ
$text = "is_month";
}else if(is_day()){ // 日アーカイブ
$text = "is_day";
}else{ // それ以外のアーカイブ
$text = "is_archive";
}
$title = get_the_archive_title();
$text = $site_descriotions['is_archive'];
$image = $no_image_file_url;
}else{ //それ以外
$post = get_post($post_ID);
$post_type = $post->post_type;
$post_name = $post->post_name;
$url = get_the_permalink();
if($post_type == 'interview'){ // インタビューであれば
$top_img = wp_get_attachment_image_src(post_custom('Top_img'),array(600,600) );
$text = post_custom('excerpt');
$title .= strip_tags(post_custom('title'));
$title .= " ".strip_tags(post_custom('Company'));
$image = $site_url.$top_img[0];
$url .= "?area=metro";
}else if($post_type == 'page'){ // ページであれば
$text = array_key_exists($post_name,$site_descriotions) ? $site_descriotions[$post_name] : $site_descriotions['other_ss'];
$title = strip_tags($post->post_title);
$thumbnail_id = get_post_thumbnail_id();
if(has_post_thumbnail()){
$thumbnail_array = wp_get_attachment_image_src($thumbnail_id,'ogp_thm');
$image = $thumbnail_array[0];
}else{
$image = $no_image_file_url;
}
}else{ //インタビュー・ページ以外
$title = strip_tags($post->post_title);
if($post->post_excerpt){ //記事に抜粋があるかどうか
$text = $post->post_excerpt;
}else{
$text = $post->post_content;
}
$thumbnail_id = get_post_thumbnail_id();
if(has_post_thumbnail()){
$thumbnail_array = wp_get_attachment_image_src($thumbnail_id,'ogp_thm');
$image = $thumbnail_array[0];
}else{
$image = $no_image_file_url;
}
}
} //それ以外ここまで
$text = strip_tags(preg_replace('/(\s| )/','',$text));
$ogp['og:site_name'] = get_bloginfo('name');
$ogp['og:type'] = $type;
$ogp['og:url'] = $url;
$ogp['og:title'] = $title;
$ogp['og:image'] = $image;
$ogp['og:description'] = mb_substr($text, 0, 95, "UTF-8");
foreach($ogp as $key => $ogp_tag){
echo '<meta property="'.$key.'" content="'.$ogp_tag.'"/>'."\n";
}
}
WordPressを知ってからかれこれ何年か過ぎ、
自分である程度細かくテンプレートを設置できるようになってからプログラムの効率化というものを考えるようになりました。
カスタム投稿の階層についてはいまだに微妙にわかってないのだけども
(URLの構造に関しては短い方がいいだろっ!って思ってる節はあるけども
カスタム投稿名とターム名は関連付けしやすい名付けかたにしたほうがよかった。
今の私のWordpressの記事はカスタム投稿名がwpressで使っているタームの名前がwp-cat
これがwordpress-catにするなり
カスタム投稿名をwpにするならやたらと長くなりそうなcase文をもっと簡略化できたのにぃいいいい!
と思う。
ファンクション名とかもそうだけど名付けかたにも使い道を含めて考えないといけないねぇ。
共通点を持たせればそれを利用できるってことだね。
個人的なメモ書き用のカスタム投稿を作っているが今まではloopで制御して見せないようにしていたが
ログインしていない場合は見せないだけでなく404を返すようにfunction.phpで対応してみた。
function page_not_found_404() {
$post_type = "post_type"
if( is_post_type_archive($post_type) || $post_type == get_post_type() ) {
if(!is_user_logged_in()){
status_header(404);
include(TEMPLATEPATH . '/404.php');
exit();
}
return;
}
}
add_action('template_redirect', 'page_not_found_404');
ローカルサーバだとうまくいっているので本番環境で帰ってきたら実行してみよう。
当たり前だけどインクルードを使っているのでテーマフォルダの中に404.phpがないと機能しないからね。
追記
と一旦実装してみて、吐き出されたソースを細かく見ていくと
titleタグの中身やbody_class()の中身が元のコンテンツの情報のままになっている。
その辺りをちゃんと変更できるようになっておかないと微妙だなぁ。
404.phpのテンプレートにheaderやらなんやらを全て記述していくならできるけども・・。
他の箇所のフィルターを設定すれば問題ないのだけども、一発でできたほうだいいからなぁ。
あとはhtaccessなんかの設定とか挙動を考えておかないと
トップページにリダイレクトされたり404のページが表示されたりと挙動があやふやになっちゃうかもね。
WordPress4.4でwp_title()が非推奨(暫定非推奨?)っぽくなり、
タイトルの箇所の表示はwp_head()からの表示になり、
日記アーカイブページで他のアーカイブページと同じように
記事タイトル名|サイト名
に合わせたくなったので調整できるかどうか調べていたところ
私のサイトの投稿記事’post’の日記アーカイブ(/?post_type=post)は
Wordpress内での判断だとアーカイブではなくis_home()でtrueの状態になる様子。
is_archiveではfalseとなる。
body_class()でblogと表示されるのでそこから探してみた。
ファンクションの内容はwp-includes/post-template.phpに記述
ということで以下
//タイトルタグを有効に
add_theme_support( 'title-tag' );
//セパレーターの変更
function title_separator( $sep ){
$sep = '|';
return $sep;
}
add_filter( 'document_title_separator', 'title_separator' );
//タイトルの表示を変更
function site_title_parts( $title ){
// $title['title'] = ''; //記事タイトルやアーカイブタイトル
// $title['page'] = ''; //ページ数
// $title['tagline'] = ''; //サイト概要?として入れることができる箇所?
// $title['site'] = ''; //サイト名
if(is_front_page()){
$title['title'] = '';
$title['tagline'] = get_bloginfo('description');
}else if(is_home()) $title['title'] = '日記';
return $title;
}
add_filter( 'document_title_parts', 'site_title_parts' );
これで表示的には
6×6.jp | のんびり日記 (トップページ:フロントページのこと
日記 | 6×6.jp (日記アーカイブ
日記 | ページ 2 | 6×6.jp (日記アーカイブ ページ付
FF14 | 6×6.jp (FF14アーカイブ
上のようになったので満足、仕様もある程度把握できたので満足満足