WordPress wp_title blank on index page - wordpress

I'm new to WordPress and just installed version 3.3.1.
I did some googling regarding this question and found some answers but they were relevant to version 2.7 and were 2-3 years old.
Basically, the wp_title function works fine on every page except my home page where it returns blank and I get no title whatsoever. I could just hard code the title in but I'd rather not do that.
Guilty line of code:
<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>
I couldn't find anything regarding this problem happening in 3.3.1 so clearly I've done something wrong.

Here's is what I read from Codex:
If you are using a custom homepage with custom loops and stuff, you
will have an empty wp_title. Here goes a neat hack to add the
description/tagline at the wp_title place on homepage:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
So use is_front_page() to get the title on homepage, the way it is suggested in above code.

But if you use a static home page, this is the code:
<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>

Update
for WordPress versions (>= 4.4)
Try this
function some_name(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'some_name' );
Do this in functions.php and remove 'title' tag from head...

Working off of Amna's answer, I came up with the following code which should display the page title when there is one, followed by the site name.
<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>
Post/Page Outputs: "The Page Title - Site Name"
Home Page Outputs: "Site Name"
Obviously, this can also be swapped to display the site name first.
<?php bloginfo('name'); wp_title(' - '); ?>
Post/Page Outputs: "Site Name - The Page Title"
Home Page Outputs: "Site Name"
This can also be combined with a conditional to display the site description when viewing the home page.
<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>
Post/Page Outputs: "Site Name - The Page Title"
Home Page Outputs: "Site Name - The Site Description"

For google search on wordpress wp_title empty this is the first result. So I thought that I might share the most elegant solution for this.
In functions.php add a filter for wp_title.
function custom_wp_title( $title, $sep ) {
if ( is_feed() ) {
return $title;
}
global $page, $paged;
// Add the blog name
$title .= get_bloginfo( 'name', 'display' );
// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $site_description";
}
// Add a page number if necessary:
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
}
return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );

The new hack from Codex is as follows:
<title><?php wp_title(''); ?></title>
Then in your "functions.php" from theme file :
add_filter( 'wp_title', 'baw_hack_wp_title_for_home' );
function baw_hack_wp_title_for_home( $title )
{
if( empty( $title ) && ( is_home() || is_front_page() ) ) {
return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
}
return $title;
}

I use this one and it never failed:
function pageTitle($echo){
$title = "";
global $paged;
if (function_exists('is_tag') && is_tag()) {
$title .= single_tag_title(__("Tag Archive for "" , 'circle'),false);
$title .= '" - ';
}
elseif (is_archive()) {
$title .= wp_title('',true);
//$title .= __(' Archive - ' , 'circle');
$title .= __(' - ' , 'circle');
}
elseif (is_search()) {
$title .= __('Search for "' , 'circle') . esc_html(get_search_query()).'" - ';
}
elseif (!(is_404()) && (is_single()) || (is_page())) {
$title .= wp_title('',true);
$title .= ' - ';
}
elseif (is_404()) {
$title .= __('Not Found - ' , 'circle');
}
if (is_home()) {
$title .= get_bloginfo('name');
$title .= ' - ';
$title .= get_bloginfo('description');
}
else {
$title .= get_bloginfo('name');
}
if ($paged>1) {
$title .= ' - page ' . $paged;
}
if ( !$echo ) return $title;
echo $title;
}
Note that there are translation domains in it that you might want to change.

no need. Just add the <? Php wp_head ();?> Code at the end of the header.php
good luck.

I use this method in my WordPress site
//Meta Header
if ( ! function_exists( 'dima_wp_title' ) ) :
function dima_wp_title( $title ) {
if ( is_front_page() ) {
return get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );
} elseif ( is_feed() ) {
return ' | RSS Feed';
} else {
return trim( $title ) . ' | ' . get_bloginfo( 'name' );
}
}
add_filter( 'wp_title', 'dima_wp_title' );
endif;

Late to the conversation...
But if you want to use the actual title of the page that you are using for the static front page, you can use the following:
if (is_front_page())
{
$title = single_post_title( '', false );
}
Although, in the actual source for wp_title(), there is the following, specificaly disabling this for the static front page:
if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
$title = single_post_title( '', false );
}
I suspect there is good reason for this. So, proceed with caution.

My 2 cents for "misty lake" theme which had no title on home page and added incorrect title on all other pages.
Just removing the following line from header.php solves the issue, since Wordpress now injects the tag by itself:
<title><?php wp_title( '|', true, 'right' ); ?></title>
I consulted the following page – https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/

You could also put something like this inside your title tag:
<?php
if (is_front_page()) { ?>
Home | <?php bloginfo('description');
} else {
wp_title('|', 'true', 'right'); bloginfo('description');
} ?>

Related

Blog title is not display in browser

My permalink settings as below
/%category%/%postname%/
if i click on my blog or post it doesn't show the title in the browser page title.Please help me to fix.
enter image description here
Use this function in your function.php file
add_filter( 'wp_title', 'wpse_174379_show_posts_page_wp_title' ); function wpse_174379_show_posts_page_wp_title( $title ) { if( get_option( 'page_for_posts' ) ) { $posts_page = get_post( get_option( 'page_for_posts') );
$title = ' ' . $posts_page->post_title . ' ';
} return $title; }
I think its working for you
try this one...
<title><?php if (is_front_page() ) { bloginfo('name'); echo ' - '; bloginfo('description'); } else { wp_title(''); echo ' - '; bloginfo('name'); };?></title>

Meta description

With the help of the answer given at Place category_description in the meta description (wordpress) I think I've got this almost figured out, but it just doesn't seem to be working - when I view the page source for any page, the meta description is empty:
<meta name="description" content="" />
Here's what I've got:
In functions.php
<?php
if( is_single() || is_page() ) $description = get_the_excerpt();
elseif( is_category() ) $description = category_description();
else $description = "Free French lessons and language tools from Laura K. Lawless, including verb conjugations and bilingual articles to help you improve your reading and listening comprehension.";
$description = substr($description,0,500);
?>
In header
<meta name="description" content="<?= $description ?>" />
Any ideas? TIA!
Try this function, it will return something in most cases.
// functions.php
function blog_description() {
$content = get_queried_object();
if ( is_singular() ) {
$content = !empty( $content->post_excerpt ) ? $content->post_excerpt : ( !empty( $content->post_content ) ? $content->post_content : $content->post_title );
return str_replace( PHP_EOL, ' ', substr( wp_filter_nohtml_kses( $content ), 0, 155 ) );
} elseif ( is_category() ) {
$content = !empty( $content->description ) ? $content->description : get_bloginfo( 'name' ) . ' - ' . $content->name;
return str_replace( PHP_EOL, ' ', substr( wp_filter_nohtml_kses( $content ), 0, 155 ) );
}
return get_bloginfo( 'description' );
}
// header.php
<meta name="description" content="<?php echo blog_description(); ?>" />

How to get woocommerce price attributes

Im trying to get the following information on a woocommerce single product page in order to replace the default price style of woocommerce.
When Item is on sale, I want to be able to get the original price and discounted price and echo it inside a dive for further styling. Im trying to achive something like this.
<div id="orig-price">Original Price: <?php echo $price; ?></div>
<div id="sale-price">Sale Price: <?php echo $sale-price; ?></div>
<div id="saved">You saved: <?php $saved=$price-$saleprice; echo $saved; ?></div>
I tried opening price.php but did not find what I am looking for. here is what I get.
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"><?php echo $product->get_price_html(); ?></p>
<meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
<div><?php echo $sale_price_in_percent; ?></div>
</div>
Am I on the right place? Anyone know how can I access these attributes?
Thanks
Main function for displaying prices is get_price_html() in /woocommerce/includes/abstracts/abstract-wc-product.php file, and there are some filters to allow users to customize the html for prices:
woocommerce_sale_price_html
woocommerce_price_html
woocommerce_empty_price_html
woocommerce_free_sale_price_html
woocommerce_free_price_html
woocommerce_get_price_html
This function performs a complete job for showing prices including taxes and on sale calculations, and since you are completely rewriting the html the example below uses last filter there woocommerce_get_price_html. It extracts the prices from the original HTML and insert them into a new HTML structure.
add_filter( 'woocommerce_get_price_html', function( $price, $product )
{
global $woocommerce_loop;
// check if we are in single product page, in main section, and if product has price and is on sale
if ( is_product() && !isset( $woocommerce_loop ) && $product->get_price() && $product->is_on_sale() ) {
// collect prices from $price html string
$prices = array_map( function( $item ) {
return array( $item, (float) preg_replace( "/[^0-9.]/", "", html_entity_decode( $item, ENT_QUOTES, 'UTF-8' ) ) );
}, explode( ' ', strip_tags( $price ) ) );
$price = isset( $prices[0][0] ) ? '<span class="orig-price">Original Price: ' . $prices[0][0] . '</span>' : '';
$price .= isset( $prices[1][0] ) ? '<span class="sale-price">Sale Price: ' . $prices[1][0] . '</span>' : '';
if ( $product->get_regular_price() ) {
// set saved amount with currency symbol placed as defined in options
$price .= '<span class="saved">You saved: ' . sprintf( get_woocommerce_price_format(), get_woocommerce_currency_symbol(), $prices[0][1] - $prices[1][1] ) . '</span>';
}
}
return $price;
}, 10, 2 );
I'm using span tags here instead of divs used in question because the output is still wrapped in a paragraph. For further customization override the price.php template by placing him inside /themes/yourtheme/woocommerce/single-product/ folder.
This can be achieved by overriding woocommerce template. Create a new PHP file, write your div structure in that file.
Now refer this article on How to override template in WooCommerce
Hope this will move you in right direction

<!--more--> being ignored when WordPress content is displayed

I'm trying to write a plugin that will take page id's and return a preview of the page. Here's my code:
function page_preview($atts,$pageid = null) {
extract(shortcode_atts(array(
"pageid" => '0'
), $atts));
$the_query = new WP_Query( 'page_id=' . $pageid . '' );
global $more;
$more = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content=get_the_content();
}
}
return '<div class="callout">' .
'<h4>' .
$title .
'</h4>' .
$thumbnail .
$content . '<a href="' .
get_the_permalink($pageid) .
'">Continue reading</a></div>';
}
add_shortcode( 'pagepreview', 'page_preview' );
and it gets called on wpadmin editor like so:
[pagepreview pageid=11][pagepreview pageid=13][pagepreview pageid=8054]
i.e., that would display a page preview for each of those page ids.
The "more" doesn't work.
global $more;
$more = 0;
usually fixes this issue, but it's not in my case. Can anyone see what I"m doing wrong? Thanks.
You're getting the full content because
$content=get_the_content();
is not applying the_content() filters, and $more=0 must be on a line after $the_query->the_post(); inside the while loop. Change your while loop to this:
while ( $the_query->have_posts() ) {
$the_query->the_post();
$more=0;
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
}
See get_the_content and read more in pages on the WordPress Codex for where I sourced this.

Adding custom description meta tag in wordpress pagination

I have a question about wordpress pagination query.
For example; When i click the second page domain.tld/page/2 how to change the page's description meta in wp_head(); is there any idea ?
I've searched the answer and have not found right solution.
Check this out
function add_meta_tags_for_pagination_pages() {
global $page, $wp_query; // need to use globag vars in function
if ( is_paged() ) { // it is a pagination
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // get current pagination page
if (!is_home()){ // it is pagination in some category
$category = get_the_category($wp_query->posts[0]->ID);
$category_name = $category[0]->name;
}
echo '<meta name="description" content="' . (isset($category_name) ? $category_name . ' | ' : '') . sprintf( __( 'Page %s' ), $paged ) . '" />' . "\n";
echo '<meta name="keywords" content="' . (isset($category_name) ? $category_name . ', ' : '') . sprintf( __( 'Page %s' ), $paged ) . '" />' . "\n";
}
}
add_action( 'wp_head', 'add_meta_tags_for_pagination_pages' , 2 );
The above answer still works. Just adding get_the-excerpt() would be a better page description.
Also, the solution is not working on pages with a /category sub directory
Are you looking for this ?
remove_action ('wp_head', 'wp_generator');

Resources