How to add dynamic content in title and meta description in wordpress theme for homepage, post page, category, tag and pages

wordpress.stackexchange https://wordpress.stackexchange.com/questions/261393

Frage

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.

War es hilfreich?

Lösung

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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top