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

menus - How to get the Page Name of the current page?

programmeradmin1浏览0评论

On mobile, I want to display what page my users are on. I have 6 top pages at the moment.
Home, Projects, Blog, Contact, Privacy Policy and Impressum. Both Projects and Blog have posts in them. Projects is a custom post type as well.
When a user clicks on Contact, title should change to contact. If they go to Project and click on a post, the title should say projects. I hope I make it clear.
I don't mind working with if statements either if that's a solution.

Here is an image of what it would look like:

I hope I am making myself clear. Does anyone have a solution?

I have tried it with get_the_title() to echo the title, but this does not give me proper results. When I'm on my Contact Page, it does say Contact. But once I'm on my home, project or blog page, it gives me the title of some post item, and not the title of the page as I want.

To give more context of what I want:
Imagine it like a breadcrumb, but instead, I only want one part of it. So instead of
Home > Contact or Home > Project > Some Post Title I want only "Contact" or "Project". How would I go about that?


So now I have this code:

if ( is_front_page() ) {
         echo "Startseite";
     } elseif ( is_singular("blog") ) {
         echo "Blog";
     } elseif ( is_singular("project") ) {
         echo "Projekt";
     } elseif ( get_the_title() == "Datenschutz" ) {
         echo "Datenschutz";
     } elseif ( get_the_title() == "Impressum" ) {
         echo "Impressum";
     } elseif ( get_the_title() == "Contact" ) {
         echo "Kontakt";
     } elseif ( get_the_title() == "Blog" ) {
         echo "Blog";
     } elseif ( get_the_title() == "project" ) {
         echo "Projekte";
     }

Which almost works. The only Problem I am having is, when I'm on the Project and Blog page, where you can select a post. It doesn't show "Projekte" or "Blog".
When I'm clicking on a Project Post, it shows "Projekte". But when I'm on a Blog Post, it does not show "Blog".

Any ideas on how to fix that?

On mobile, I want to display what page my users are on. I have 6 top pages at the moment.
Home, Projects, Blog, Contact, Privacy Policy and Impressum. Both Projects and Blog have posts in them. Projects is a custom post type as well.
When a user clicks on Contact, title should change to contact. If they go to Project and click on a post, the title should say projects. I hope I make it clear.
I don't mind working with if statements either if that's a solution.

Here is an image of what it would look like:

I hope I am making myself clear. Does anyone have a solution?

I have tried it with get_the_title() to echo the title, but this does not give me proper results. When I'm on my Contact Page, it does say Contact. But once I'm on my home, project or blog page, it gives me the title of some post item, and not the title of the page as I want.

To give more context of what I want:
Imagine it like a breadcrumb, but instead, I only want one part of it. So instead of
Home > Contact or Home > Project > Some Post Title I want only "Contact" or "Project". How would I go about that?


So now I have this code:

if ( is_front_page() ) {
         echo "Startseite";
     } elseif ( is_singular("blog") ) {
         echo "Blog";
     } elseif ( is_singular("project") ) {
         echo "Projekt";
     } elseif ( get_the_title() == "Datenschutz" ) {
         echo "Datenschutz";
     } elseif ( get_the_title() == "Impressum" ) {
         echo "Impressum";
     } elseif ( get_the_title() == "Contact" ) {
         echo "Kontakt";
     } elseif ( get_the_title() == "Blog" ) {
         echo "Blog";
     } elseif ( get_the_title() == "project" ) {
         echo "Projekte";
     }

Which almost works. The only Problem I am having is, when I'm on the Project and Blog page, where you can select a post. It doesn't show "Projekte" or "Blog".
When I'm clicking on a Project Post, it shows "Projekte". But when I'm on a Blog Post, it does not show "Blog".

Any ideas on how to fix that?

Share Improve this question edited Oct 16, 2020 at 18:50 Mähnenwolf asked Oct 15, 2020 at 8:47 MähnenwolfMähnenwolf 1479 bronze badges 3
  • get_the_title() function will retrieve the current post title.Please check and let me know is that work? – Yash Tiwari Commented Oct 15, 2020 at 12:55
  • The problem with that, it will give me the Post Title, but when viewing any post I want the Page title. – Mähnenwolf Commented Oct 16, 2020 at 10:32
  • The only Problem I am having is, when I'm on the Project and Blog page, where you can select a post. It doesn't show "Projekte" or "Blog". so what is the exactly unexpected page displayed? – RachC Commented Oct 17, 2020 at 1:44
Add a comment  | 

1 Answer 1

Reset to default 0

After many attempts and help from others, I finally got a solution to my problem. That's the code I used:

<?php 

      
     if ( is_front_page() ) {
         echo "Startseite";
     } elseif ( is_singular("project") ) {
         echo "Projekte";
     } elseif ( get_the_title() == "Datenschutz" ) {
         echo "Datenschutz";
     } elseif ( get_the_title() == "Impressum" ) {
         echo "Impressum";
     } elseif ( get_the_title() == "Contact" ) {
         echo "Kontakt";
     } elseif ( get_post_type() == "project" ) {
         echo "Projekte";
     } elseif ( is_single() || is_home() ) {
         echo "Blog";
     }
      
  ?> 
发布评论

评论列表(0)

  1. 暂无评论