最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Delete empty title_tags, find a h1 heading and echo it

programmeradmin2浏览0评论

I want to repeat the title of my Wordpress page in a copyright statement in the footer, like

© 2020 "Review: Winter's Sunshine": The Berlin Review Magazine

The snippet I simply used in the past was:

© <?php echo date("Y"); ?> "<?php echo get_the_title(); ?>": [Website Name]

It worked exactly as I needed it.

Unfortunately - due to an exotic page builder - the website I'm currently working on doesn't always use the_title for the h1 headline. Sometimes the_title is ignored and there's a h1-headline somewhere in the html text below.

So, I think I need a function which checks if the_title is empty - if not empty, echoes the_title, and that's it.

But if the_title is empty or non-existing, 2. a. it should hide the complete empty <h1></h1> tag of the title (if it exists), b. look for another, non-empty h1-tag in the html text of the page, c. then echo that tag-content.

I came thus far:

$newtitle = h1 -> outercontent;
if (the_title()){ echo get_the_title(); }
else { echo $newtitle; }

Not very impressive, I'm afraid. Every help would be appreciated.

I want to repeat the title of my Wordpress page in a copyright statement in the footer, like

© 2020 "Review: Winter's Sunshine": The Berlin Review Magazine

The snippet I simply used in the past was:

© <?php echo date("Y"); ?> "<?php echo get_the_title(); ?>": [Website Name]

It worked exactly as I needed it.

Unfortunately - due to an exotic page builder - the website I'm currently working on doesn't always use the_title for the h1 headline. Sometimes the_title is ignored and there's a h1-headline somewhere in the html text below.

So, I think I need a function which checks if the_title is empty - if not empty, echoes the_title, and that's it.

But if the_title is empty or non-existing, 2. a. it should hide the complete empty <h1></h1> tag of the title (if it exists), b. look for another, non-empty h1-tag in the html text of the page, c. then echo that tag-content.

I came thus far:

$newtitle = h1 -> outercontent;
if (the_title()){ echo get_the_title(); }
else { echo $newtitle; }

Not very impressive, I'm afraid. Every help would be appreciated.

Share Improve this question edited Dec 28, 2019 at 11:25 HSF asked Dec 24, 2019 at 17:05 HSFHSF 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You can't do if(the_title()) cuz the_title() this code'll echo a value... Use get_the_title() for a return of the value.

Remove this code too: $newtitle = h1 -> outercontent;

Do this for check if title is empty:

if(empty(get_the_title())){
    // Is empty
} else {
    // is not empty
}

Or you can do only this:

if(!empty(get_the_title())){
    // I'm not empty, write your h1 code and title
}

You can do this too:

$title = !empty(get_the_title()) ? '<h1>' . get_the_title() . '</h1>' : '';
echo $title;
发布评论

评论列表(0)

  1. 暂无评论