Wordpress使用functions.php在内容中有条件地显示页面标题
快速概览: 我试图让页面标题显示在_content()中,但是这需要有条件,并且仅当页面有附加图像时才显示。这也需要通过函数文件来完成 我到哪里去了: 这是我到目前为止的代码。。。但它不起作用,我认为问题是它在循环之外。。。如何使用代码查找页面id。。。或者我怎样才能让它工作Wordpress使用functions.php在内容中有条件地显示页面标题,function,wordpress-theming,wordpress,Function,Wordpress Theming,Wordpress,快速概览: 我试图让页面标题显示在_content()中,但是这需要有条件,并且仅当页面有附加图像时才显示。这也需要通过函数文件来完成 我到哪里去了: 这是我到目前为止的代码。。。但它不起作用,我认为问题是它在循环之外。。。如何使用代码查找页面id。。。或者我怎样才能让它工作 <?php if ( has_post_thumbnail() ) { add_filter('the_content', 'contentTitle');
<?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
}
?>
解决方案 “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
}
}
?>
您应该全局化post对象,以便post 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;
}
}
add_filter('the_content','contentTitle');
函数contentTitle($content='')
{
全球$员额;
如果(有帖子缩略图($post->ID){
$theTitle=''。获取标题($post->ID)。'';
返回$theTitle.$content;
}
}