admin管理员组文章数量:1122818
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.
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.
Share Improve this question asked Mar 26, 2017 at 13:35 Master PronsMaster Prons 31 silver badge4 bronze badges 1- It found now is online right now. I'll wait for answer till tomorrow evening. Plz, show me right way – Master Prons Commented Mar 26, 2017 at 14:02
1 Answer
Reset to default 0You 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>
本文标签:
版权声明:本文标题:How to add dynamic content in title and meta description in wordpress theme for homepage, post page, category, tag and pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312424a1935089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论