0

簡単な概要: ページタイトルをthe_content()内に表示しようとしていますが、これは条件付きである必要があり、ページに画像が添付されている場合にのみ表示されます。これも関数ファイルを介して行う必要があります。

私が立ち上がった場所: これは私がこれまでに持っているコードです...しかしそれは機能していません、そして問題はそれがループの外にあることだと思います...どうすればコードを操作してページIDを検索できますか? ..またはどうすればそれを機能させることができますか?

<?php

        if ( has_post_thumbnail() ) {
        add_filter('the_content', 'contentTitle');
            function contentTitle($content='')
            {
                $theTitle = '<h1>' . get_the_title() . '</h1>';
                return $theTitle . $content;
            }
        } else {
                // Do nothing
            } 

?>
4

2 に答える 2

1

投稿 ID を使用できるように、投稿オブジェクトをグローバル化する必要があります。

add_filter('the_content', 'contentTitle');

function contentTitle($content='')
{
 global $post;
 if( has_post_thumbnail( $post->ID ){ 
   $theTitle = '<h1>' . get_the_title( $post->ID ) . '</h1>';
   return $theTitle . $content;
   }
}
于 2013-02-25T13:15:35.217 に答える
0

解決

「if」は関数自体の中にあるはずです。

<?php
add_filter('the_content', 'contentTitle');

function contentTitle($content='') {

   if ( has_post_thumbnail() ) {
   $theTitle = '<h1>' . get_the_title() . '</h1>';
   return $theTitle . $content;

   } else {

   // Do nothing

   }
}
?>
于 2013-02-25T13:10:20.273 に答える