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 回答