What I'm trying to achieve in WooCommerce is that on the single product page, the Description tab, I'm trying to add the product page title before the word Description.
Here is my current WooCommerce code:
defined( 'ABSPATH' ) || exit;
global $post;
$heading = apply_filters( 'woocommerce_product_description_heading', __( 'Description', 'woocommerce' ) ); ?>
<?php if ( $heading ) : ?>
<h2>PRODUCT PAGE TITLE HERE <?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
<?php the_content(); ?>
The problem here, however, is that:
I have to overwrite the template file, is it possible via a hook?
The product title is not dynamic.
As a result, id like the tab to go from Description to BLACK NIKE SHOES Description
Example:
Any advice?
You can use the woocommerce_product_{$tab_key}_tab_title composite filter hook. Where $tab_key is in your case description
Use global $product and $product->get_name() to get the product title. This result can then be added to the existing string.
So you get:
function filter_woocommerce_product_description_tab_title( $title ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get title and append to existing string
$title = $product->get_name() . ' ' . $title;
}
return $title;
}
add_filter( 'woocommerce_product_description_tab_title', 'filter_woocommerce_product_description_tab_title', 10, 1 );
Optional: to change the WooCommerce product description heading instead, use the woocommerce_product_description_heading filter hook:
function filter_woocommerce_product_description_heading( $heading ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get title and append to existing string
$heading = $product->get_name() . ' ' . $heading;
}
return $heading;
}
add_filter( 'woocommerce_product_description_heading', 'filter_woocommerce_product_description_heading', 10, 1 );
I have a site where I am 'pulling' local events from a secondary website RSS feed. I have this working however the feed is displaying in reverse order with the local events dated later (i.e. at the end of October versus events dated for today) showing up at the top instead of the bottom.
Here is the code I am using for the feed ingest:
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://sample.com.au/events/feed/'); // specify the source feed
$limit = $feed->get_item_quantity(25); // specify number of items
$semti = array_flip($limit);
$items = $feed->get_items(0, $limit); // create an array of items
}
if ($limit == 0) echo '<div>The feed is unavailable.</div>';
else foreach ($items as $item) : ?>
<p><b><a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<?php echo esc_html( $item->get_title() ); ?></a></b>
<?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
<?php echo sanitize_text_field( $item->get_content() ); ?>
</p>
<?php endforeach; ?>
This works perfectly to get my remote RSS feed and display the title, date of the event and the excerpt, however the order is reverse sorted.
I tried adding filters like "sort and ksort" in the "foreach ($items $items) :" area but this did not work for me. I've racked my brains on this one and am hoping someone can help me out. I appreciate any guidance/help in advance.
Try the appropriately named array_reverse function!
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://sample.com.au/events/feed/'); // specify the source feed
$limit = $feed->get_item_quantity(25); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
$semti = array_reverse($items); // & flip it
}
if ($limit == 0) echo '<div>The feed is unavailable.</div>';
else foreach ($semti as $item) : ?>
<p><b><a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<?php echo esc_html( $item->get_title() ); ?></a></b>
<?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
<?php echo sanitize_text_field( $item->get_content() ); ?>
</p>
<?php endforeach; ?>
From PHP.net:
array_reverse
Return an array with elements in reverse order
array array_reverse ( array $array [, bool $preserve_keys = false ] )
Takes an input array and returns a new array with the order of the elements reversed.
Have got this working on general wordpress categories using this plugin:
https://wordpress.org/plugins/categories-images/
Then adding the following to the category.php template as advised on another thread.
<?php if (function_exists('z_taxonomy_image_url')) { ?>
<img src="<?php echo z_taxonomy_image_url(); ?>" alt="<?php the_title(); ?>" />
<?php } ?>
I'd like to do exactly the same with the product categories. Actually ideally I'd like to be able to add background images to each category so that the description text can go over the top, like the way this shop works:
http://www.natures-own.co.uk/Antioxidants/
Is this possible with some minor code tweaking, or better still is there a woocommerce equivalent to the wordpress plugin I've used?
I cannot find any resources for this anywhere, everything I find on searching is referring to just thumbnail of a category list as far as I can see!
Thanks in advance
Pat
You can add the category image and description by adding the following to the archive-product.php file after <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?> that if statement:
if (is_product_category())
{
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
//if you only want the image, uncomment the two lines below
//list($width, $height) = getimagesize($image);
//echo '<img src="'.$image.'" alt="" width="'.$width.'" height="'.$height.'"/>';
$cat_id=$cat->term_id;
$prod_term=get_term($cat_id,'product_cat');
$description=$prod_term->description;
echo '<div class="category-description" style="background-image:url('.$image.');">'.$description.'</div>';
}
To display Woocommerce Category image
use this code -
add_action('woocommerce_archive_description', 'woocommerce_add_category_image', 20);
function woocommerce_add_category_image()
{
global $product;
if (is_product_category())
{
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id);
if ($image)
{
echo '<img src="' . esc_url($image) . '" alt="" />';
}
}
}
I'd like to use a functions.php in my child theme to remove the blog title. Most posts here are for specific themes. No option I've tried have worked.
I found in header.php where the blog title is called:
<?php do_action('et_header_top'); ?>
<header class="clearfix">
<hgroup id="logo-area">
<?php $bloginfo_name = get_bloginfo( 'name' ); ?>
<h1 id="logo">
<?php if ( 'false' == et_get_option( 'flexible_logo_image', 'false' ) || '' == et_get_option( 'flexible_logo' ) ) echo apply_filters( 'et_logo_markup', $bloginfo_name ); else echo '<img src="' . esc_url( et_get_option( 'flexible_logo' ) ) . '" alt="' . esc_attr( $bloginfo_name ) . '" />'; ?>
</h1>
<h2><?php bloginfo( 'description' ); ?></h2>
</hgroup>
What do I put in functions.php to remove that?
I tried
`function remove_stuff()
{
remove_action('Fable_header','Fable_bloginfotitle',);
remove_action('Fable_header','Fable_bloginfodescription',);
}
add_action('init','remove_stuff');
?>`
with no change.
If you want to get rid of the blog title without deleting the code, you can comment it out. This helps reduce problems. If you want to put the blog title back later, you can easily put it out if it is commented out, but if you delete the text then you can't display the title.
Commenting out works like this: wrap your code in /* and */. Example: /* code */. In your case, you would want to comment out the code like this:
<?php do_action('et_header_top'); ?>
<header class="clearfix">
/* <hgroup id="logo-area">
<?php $bloginfo_name = get_bloginfo( 'name' ); ?>
<h1 id="logo">
<?php if ( 'false' == et_get_option( 'flexible_logo_image', 'false' ) || '' == et_get_option( 'flexible_logo' ) ) echo apply_filters( 'et_logo_markup', $bloginfo_name ); else echo '<img src="' . esc_url( et_get_option( 'flexible_logo' ) ) . '" alt="' . esc_attr( $bloginfo_name ) . '" />'; ?>
</h1>
<h2><?php bloginfo( 'description' ); ?></h2>
</hgroup> */
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');
} ?>