0

Made own template from scratch and tutorials. This is showing description of post/page Title and in home its showing website name.

<h3><?php is_home() ? bloginfo('description') : wp_title(''); ?></h3>

I wanted to change that if home page then that it writes News and if page then its name but if page is subpage then parent name.

2
  • w...o...w... that title is awesome. No, honestly: I don't get what you're after. Hint: The title should sum up what it is about, not the whole task in pseudo code.
    – kaiser
    Apr 27, 2013 at 12:18
  • Changed it :) but still how to make this questions title better?
    – Kangarooo
    Apr 27, 2013 at 12:21

1 Answer 1

1

Put this in your functions.php:

function my_title() {
    if (is_home())
        echo 'News';
    else {
        global $post;
        if ($post->post_parent)
            echo get_post($post->post_parent)->post_title;
        else echo $post->post_title;
    }
} // function my_title

and then use <?php my_title(); ?> anywhere you want.

The above code prints News on the home page, for child posts the name of their respective parent, and the post title otherwise.

If you want to do this for pages only (no other post types), then use this:

function my_title() {
    if (is_home())
        echo 'News';
    else {
        global $post;
        if ('page' === $post->post_type && $post->post_parent)
            echo get_post($post->post_parent)->post_title;
        else echo $post->post_title;
    }
} // function my_title
3
  • WOW. I start to understand php messing all the time with this my self made WP theme :) From your answer i copied just inner part to my header.php between php tag for title: if (is_home()) echo 'News'; else { global $post; if ($post->post_parent) echo get_post($post->post_parent)->post_title; else echo $post->post_title; } And that works Perfect! Thanks :)
    – Kangarooo
    Apr 27, 2013 at 12:54
  • Hm, that let's me think you only have an index.php template file, right? Maybe you should read a bit on theme development...? Glad I could help, anyway. ;)
    – tfrommen
    Apr 27, 2013 at 12:56
  • I started making WP theme own clean cause was wasting a lot time trying to make my tables to look correct using child eleven theme and then some blank themes. Then i started making own clean theme with just Header index and Footer. I understood that WP reads file named functions if exists and there can be put functions with defining their names but for now so that i can see all fixes and inspire for new from them ill keep it just in 3 files. And only this additional if differs Pages from other views that i hope is not taking a lot electricity to calculate. :) THX
    – Kangarooo
    Apr 27, 2013 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.