2017-10-25 14 views
0

ロングタイムのlurker、初めてのポスター。Wordpress functions.phpの条件文の構文

私の知識は非常に限られているが、私はポストのコンテンツの前にいくつかのカスタムフィールドのショートコードを表示するカスタムのfunctions.phpファイルにフィルタを追加するために管理してきました:

//Insert custom event fields at the beginning of post content 
    function custom_event($content) { 
    $beforecontent = '<strong>[acf field="date"] [acf field="time"] [acf field="location" ] </strong>'; 
    $fullcontent = $beforecontent . $content; 
    return $fullcontent; 
} 
add_filter('the_content', 'custom_event'); 

誰もが正しいと私を助けてください特定のカテゴリにある投稿の条件付きにしたいのですか?私はin_category( 'x')関数を使うことができると思うが、私は構文がわからない。私は最終的にそれが「イベント」カテゴリ内の記事のために、[場所]

で[時間]で[日付]で

が表示されますので、変数間のいくつかのテキストを追加したいです。

ありがとうございます。

答えて

1

ここが出発点です。

function custom_event($content) { 
    global $post; 

    if(in_category('category-name-slug-id', $post)) { 
     $beforecontent = '<strong>[acf field="date"] [acf field="time"] [acf field="location" ] </strong>'; 
     $fullcontent = $beforecontent . $content; 
     return $fullcontent; 
    } 

    return $content; 

} 

add_filter('the_content', 'custom_event'); 
+0

パーフェクト、おかげでアンドリュー。私はショートコードの間にテキストを追加し、それは私が望むように正確に動作します! もう一度ありがとうございます。 – Benz1

+0

もしかすると別の質問です。私は$ beforecontent変数を $ beforecontent = 'に変更しました[acf field = "time"]の[acf field = "date"]にある[ACF field = "date"]]'; オン/アット・アット・テキスト・ラベルを条件付きにするにはどうすればいいですか?変数が空白でない場合、たとえば出力されない時間や場所が表示されるようにするには: "[On [date]]" ショートコードget_field( "acf field")関数を使って呼び出すこともできます。 ありがとうございます。 – Benz1

+0

ショートコードが空の文字列を返すかどうかを確認します。 do_shortcode()関数を使用してショートコードを出力して値を確認することができます。https://developer.wordpress.org/reference/functions/do_shortcode/ –

0

ありがとうAndrew。多くのグーグルと試行後にエラー&が発生したので、私は次のことを考え出しました。私はそれが非常にエレガントなソリューションだとは思わないか、構文が完全に正しいが、それが動作するように見えます:

function custom_event($content) { 
    global $post; 
    if(get_field("date") != "" || get_field("time") != "" || get_field("location") != ""){ 
     $open = "&#0091;"; 
    } 
    if(get_field("date") != ""){ 
     $ondate = 'On '.get_field("date"); 
    } 
    if(get_field("time") != ""){ 
     $attime = 'at '.get_field("time"); 
    } 
    if(get_field("location") != ""){ 
     $atlocation = 'at '.get_field("location"); 
    } 
    if(get_field("date") != "" || get_field("time") != "" || get_field("location") != ""){ 
     $close = "&#0093; "; 
    } 
    if(in_category('events', $post)) { 
     $beforecontent = "<strong>$open$ondate $attime $atlocation$close</strong><br><br>"; 
     $fullcontent = $beforecontent . $content; 
     return $fullcontent; 
    } 
    return $content; 
} 
add_filter('the_content', 'custom_event');