0

I am creating wordpress theme and want to get and display dynamic content for my theme Title Tag and Meta Description Tag.

For Title Tag Content: I want Title For Meta Description: I want Bloginfo('description');

When I used following codes for my title it works fine:

<?php if( is_home() || is_front_page() ): ?>
    <title><?php bloginfo('name'); echo ' | '; bloginfo('description'); ?></title>
    <?php elseif ( is_single() || is_page() ): ?>
    <title><?php wp_title('|', true, 'right'); bloginfo('name'); ?></title>
    <?php elseif ( is_category() ): ?>
    <title><?php single_cat_title(); echo ' | '; bloginfo('name'); ?></title>
    <?php elseif ( is_tag() ): ?>
    <title><?php single_tag_title(); echo ' | '; bloginfo('name'); ?></title>
    <?php elseif ( is_day() ): ?>
    <title><?php echo 'Post for the day ' . get_the_date('j F, Y') . ' | '; bloginfo('name'); ?></title>
    <?php elseif ( is_month() ): ?>
    <title><?php echo 'Post for the month ' . get_the_date('F Y') . ' | '; bloginfo('name'); ?></title>
    <?php elseif ( is_year() ): ?>
    <title><?php echo 'Post for the year ' . get_the_date('Y') . ' | '; bloginfo('name'); ?></title>
    <?php endif; ?>

But I am trying to make this same code in neat manner something like that:

<title>
        <?php
            if( is_home || is_front_page() ): bloginfo('name'); echo ' | '; bloginfo('description');
            elseif( is_single() || is_page() ): wp_title('|', true, 'right'); bloginfo('name');
            elseif( is_category() ): single_cat_title(); echo ' | '; bloginfo('name');
            elseif( is_tag() ): single_tag_title(); echo ' | '; bloginfo('name');
            elseif( is_day() ): echo 'Post for the day ' . get_the_date('j F, Y') . ' | '; bloginfo('name');
            elseif( is_month() ): echo 'Post for the month ' . get_the_date('F Y') . ' | '; bloginfo('name');
            elseif( is_year() ): echo 'Post for the year ' . get_the_date('Y') . ' | '; bloginfo('name');
            endif;
        ?>
    </title>

Kindly, tell where I am wrong.

Q2. When I tried to display bloginfo description it is not working. Notice that I am using static front-page for my theme. Those codes are as follow:

<?php if (is_single() || is_page()): if (have_posts()): while (have_posts()): the_post(); ?>
    <meta name="description" content="<?php the_excerpt_rss(); ?>" />
    <?php endwhile; endif; ?>
    <?php elseif ( is_home() || is_front_page() ): ?>
    <meta name="description" content="<?php bloginfo('description'); ?>" />
    <?php endif; ?>

Kindly, Show me right way for Dynamic Title and Meta Description for my all post, pages, categories, tags and other pages.

  • It found now is online right now. I'll wait for answer till tomorrow evening. Plz, show me right way – Master Prons Mar 26 '17 at 14:02
0

You should use WordPress conditionals to determine which page you're on, build the title based on that, then print the title.

<?php
  $sep = ' | ';
  $name = get_bloginfo( 'name' );

  if( is_home() || is_front_page() )
    $title = $name . $sep . get_bloginfo( 'description' );

  if( is_single() || is_page() )
    $title = wp_title( $sep, false, 'right' ) . $name;

  if( is_category() )
    $title = single_cat_title( '', false ) . $sep . $name;

  if( is_day() )
    $title = 'Post for the day ' . get_the_date( 'j F, Y' ). $sep . $name;

  if( is_month() )
    $title = 'Post for the month ' . get_the_date( 'F, Y' ). $sep . $name;

  if( is_year() )
    $title = 'Post for the year ' . get_the_date( 'Y' ). $sep . $name;
?>
<title><?php echo $title;?></title>
  • Can you tell me answer to my Q2 i.e. how can I make my custom dynamic description and what is wrong in my code. Thanks for answer for title tag. – Master Prons Mar 27 '17 at 5:33
  • Please, tell me solution for dynamic description related codes too I am waiting for your response – Master Prons Mar 27 '17 at 5:56
  • I used same method to description it worked for all pages but still not working for static home page i.e. front-page. I used codes as: $page_description = get_bloginfo('description'); Plz, give me your valuable suggestion as per your convenient. – Master Prons Mar 27 '17 at 8:10
  • In my case, I've made a diferent header.php to the front page, and another header-inner.php to the inner pages... – Anfuca Apr 21 '17 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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