add_filter('content_pagination',function($pages, $post){
if(!is_single()){return $pages;}
$pages = explodePost($pages,$post);
return $pages;
},10,2);
function explodePost($pages, $post){
if(get_post_meta($post->ID,'mp_switch',true)){return $pages;}
$content = $post->post_content;
if(strpos($content,'//act.ballooon.jp/imp/imp')){ // for ballooon counter
return $pages;
}
$len = mb_strlen($content,'utf-8');
$insert = "<!--nextpage-->";
$width = 3000;
if($len/$width<2){
return $pages;
}
$content = preg_replace("/\r\n|\r|\n/", "\n", $content);
$st = $width;
foreach(range(1,(int)($len/$width)) as $i){
if($st > $len){ break; }
$content = preg_replace("/^.{0,$st}.+\?\n\n\K/us", $insert, $content);
$st = mb_strrpos($content,$insert,0,'utf-8')+$width;
}
// pattern blockquate
$pat = sprintf("/(<blockquote[^>]*>.*)%s(.*<\/blockquote>)/isu",preg_quote($insert,'/'));
$content = preg_replace($pat,'${1}${2}',$content);
$pat = sprintf("/%s(.{0,%d})$/isu",preg_quote($insert,'/'),$width/2);
$content = preg_replace($pat,'${1}${2}',$content);
$return = explode($insert,$content);
$return = array_filter($return,'strlen');
$return = array_values($return);
if(!$return[0]){ return $pages; }
return $return;
}
ショートコードでphpファイルを読み込む方法
add_shortcode('tpl',function($attr){ ob_start(); get_template_part($attr['file']); $ret = ob_get_contents(); ob_end_clean(); return $ret; });
wordpressでfront-page.phpやhome.phpがあるときに2ページ目をindex.phpに変更する
add_action('template_include',function($template){ if(!is_front_page()){return $template;} $paged = get_query_var('paged', false); if(!$paged){return $template;} return get_index_template(); });
WordPressでtopページを固定ページにする方法(表示設定を変更したくない場合)
if($_SERVER['REQUEST_URI'] === '/'){ add_filter('option_show_on_front',function($option){ return 'page'; }); add_filter('option_page_on_front',function($option){ return 3333;//固定ページのPOSTID }); }
WordPressの関連記事をカテゴリーとタグからプラグインなしで取得する(自作)
global $wpdb;
$sql = 'SELECT object_id,COUNT(object_id) AS c FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '.get_the_ID().') GROUP BY object_id ORDER BY c DESC, object_id DESC LIMIT 12;';
$object = $wpdb->get_results($sql);
$ids = [];
foreach($object as $v){
if(get_the_ID() == $v->object_id){
continue;
}
$ids[] = $v->object_id;
}
$params = [
'post_type' => 'post',
'include'=> $ids,
'orderby' => 'post__in',
];
$relposts = get_posts($params);
foreach ( array_slice($relposts,0,6) as $post ) :
setup_postdata( $post );
endforeach;
SELECT object_id,COUNT(object_id) AS c FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '.get_the_ID().') GROUP BY object_id ORDER BY c DESC, object_id DESC LIMIT 12;
このSQLが肝
このSQLでterm一覧を取得しその中の記事を集計して関連度の高い順に取得している
includeを使うとexcludeが効かないので取得時に除外
記事数も制御できないのでarray_sliceでカット
多めに取得しているのは非公開記事や固定ページを弾くため
WPの記事本文の改ページ無効化
add_filter('content_pagination',function($pages){
$pages = array(implode("\n",$pages));
return $pages;
});
WordPressでパスワード保護記事を非表示
add_action('pre_get_posts',function($query){
if(is_admin()){
//管理画面は除外
return;
}
if($query->get('post_status') == 'publish' || $query->is_archive()){
// get_posts と query_posts両方対応
$query->set('has_password', false);
}
});
WordPressでリスト画面のタイトルの修正
add_filter('get_the_archive_title', function($title){
$tmp = explode(':',$title);
if(count($tmp) < 2){
return $title;
}
return sprintf('%s %s',$tmp[1],$tmp[0]);
});
WordPressでsitemap 自動生成
wp-config.phpがあるディレクトリにsitemap.phpを設置
サイトマップ
wordpress 現在のURL取得
$uri = explode('?',$_SERVER['REQUEST_URI'])[0]; $url = home_url($uri);