前提・実現したいこと
ワードプレスで全記事タイトルの先頭に一括で【○○】と入力したいです。
詳しい方、ご教授お願いします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
何かしら調べたり試したりしていないのでしょうか?
回答1件
0
ベストアンサー
bulk_action に追加すれば良いと思います。
php
1<?php 2add_filter( 'bulk_actions-edit-post', function( array $actions ) { 3 $actions['add_prefix'] = __( 'タイトルに【○○】を追加' ); 4 return $actions; 5} ); 6 7add_filter( 'handle_bulk_actions-edit-post', function( string $sendback, string $doaction, array $post_ids ) { 8 if ( 'add_prefix' !== $doaction ) { 9 return $sendback; 10 } 11 12 foreach ( $post_ids as $post_id ) { 13 $post = get_post( $post_id ); 14 if ( 0 !== strpos( $post->post_title, '【○○】' ) ) { 15 $post->post_title = '【○○】' . $post->post_title; 16 wp_update_post( $post ); 17 } 18 } 19 return add_query_arg( 'updated', count( $post_ids ), $sendback ); 20}, 10, 3 );
bulk_actions-{$this->screen->id} | Hook | WordPress Developer Resources
handle_bulk_actions-{$screen} | Hook | WordPress Developer Resources
投稿2019/07/12 16:21
総合スコア1011
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/13 02:36
回答ありがとうございます。
phpが全く理解できていないので自信ありませんが、
貼って頂いたリンク先を参考に自分で頑張ってみます。
それでも分からなかったら、また質問させて頂きます。
ありがとうございました!
2019/07/13 03:55
ちなみに、張り付ける場所はfunctions.phpで合ってますでしょうか?
functions.phpに貼り付けてもエラーになってしまうのですが…
子テーマのfunctions.phpの頭に【<?php】が入っているので
add_filter~から下を貼り付けた形です。
子テーマのfunctions.phpの最後が【return $ret;(改行)}】で終わっているので
【return $ret;(改行)}】の上の行までの間に貼り付けました。
申し訳ございません、よろしくお願いします。
2019/07/13 04:03
PHPのバージョンは7.0以上でしょうか?
2019/07/13 04:15
はい。7.1.5です。
2019/07/13 04:20
functions.php の <?php の次の行に貼り付けてもエラーになりますか?
2019/07/13 04:30
エラーにはなりませんが、【○○】が反映されません・・・
ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
<?php //子テーマ用関数
add_filter( 'bulk_actions-edit-post', function( array $actions ) {
$actions['add_prefix'] = __( 'タイトルに【○○】を追加' );
return $actions;
} );
add_filter( 'handle_bulk_actions-edit-post', function( string $sendback, string $doaction, array $post_ids ) {
if ( 'add_prefix' !== $doaction ) {
return $sendback;
}
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( 0 !== strpos( $post->post_title, '【○○】' ) ) {
$post->post_title = '【○○】' . $post->post_title;
wp_update_post( $post );
}
}
return add_query_arg( 'updated', count( $post_ids ), $sendback );
}, 10, 3 );
//親skins の取得有無の設定
function include_parent_skins(){
return true; //親skinsを含める場合はtrue、含めない場合はfalse
}
//子テーマ用のビジュアルエディタースタイルを適用
add_editor_style();
//以下にSimplicity子テーマ用の関数を書く
function custom_wp_tag_cloud($args) {
$myargs = array(
'orderby' => 'count', //使用頻度順
'order' => 'RAND', // 降順(使用頻度の高い順)
'number' => 25 // 表示数
);
$args = wp_parse_args($args, $myargs);
return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_wp_tag_cloud' );
//PCでのみ表示するコンテンツ
function if_is_pc($atts, $content = null ){
$content = do_shortcode( $content);
if(!wp_is_mobile()){
return $content;
}
}
add_shortcode('pc', 'if_is_pc');
//スマートフォンで表示するコンテンツ
/*タブレットも含まれる*/
function if_is_sp($atts, $content = null ){
$content = do_shortcode( $content);
if(wp_is_mobile()){
return $content;
}
}
add_shortcode('sp', 'if_is_sp');
//サムネイル
function add_thumbnail_size() {
add_theme_support( 'post-thumbnails' );
add_image_size('thumb600',600,360,true);
}
add_action( 'after_setup_theme', 'add_thumbnail_size' );
//のーんデックス関連
if ( !function_exists( 'is_noindex_page' ) ):
function is_noindex_page(){
return
(is_archive() && !is_category()) || //アーカイブページはインデックスに含めない
( is_paged() && is_paged_category_page_noindex() ) || //ページの2ページ目以降はインデックスに含めない(似たような内容の薄いコンテンツの除外)
is_search() || //検索結果ページはインデックスに含めない
is_404() || //404ページはインデックスに含めない
is_attachment(); //添付ファイルページも含めない
}
//RSS画像
function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
endif;
add_filter('wp_feed_cache_transient_lifetime',create_function('$a', 'return 1800;'));
//ga rankingにサムネイル表示
add_filter('sga_ranking_before_title', 'showRankingImage', 10, 3);
function showRankingImage($ret, $id, $cnt) {
$post_url = get_permalink($id); // 記事のURL
$title = get_the_title($id); //タイトル
$ret = "";
if( has_post_thumbnail( $id ) ) { // アイキャッチ画像の有無
$post_thumb_src = wp_get_attachment_image_src( get_post_thumbnail_id( $id ),array( 680, 408 ) );
$post_thumb = $post_thumb_src[0];
$ret = "<div class='ranking-img'><a href=\"{$post_url}\" title=\"{$title}\"><img src=\"{$post_thumb}\" alt=\"{$title}\" title=\"{$title}\"></a></div>";
}
return $ret;
}
2019/07/13 04:34
投稿の一覧ページの一括操作に「タイトルに【○○】を追加」が追加されていませんか?
2019/07/13 04:42
追加されてました!
そういう事なんですね。私が勘違いしておりました。
解決しました、ありがとうございました。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。