I have a site where I need to change the product title to remove the word'`-product' from the product title when it is shown.
For example the product name is stored as "1000 Piece Puzzle -Product" when the shopper comes to the front page we want them to see "1000 Piece Puzzle" (without the "-product")
I tried
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',10);
add_action('woocommerce_shop_loop_item_title','fun',10);
function fun($ttl)
{
echo str_replace('-product', '', $ttl);;
}
However it would not work. Am I going about this the wrong way?
You can accomplish that by adding a filter to change the product title. Instead of using an action.
add_filter( 'the_title', 'change_product_title', 10, 2 );
function change_product_title( $title, $id ) {
$Find = '-product';
$ReplaceWith = '';
//REMOVE DESIRED STRING FROM PRODUCT TITLE
$title = str_replace($Find, $ReplaceWith, $title);
return $title;
}
Related
I have a WordPress Shortcode that displays the page title, but I need now to exclude specific words form the Page Title
For example, sometimes I have in the title words like Best or Top
For example, if the page title is Best City in California
shortcode needs to show ( City in California )
This is my code on how to display the title
function post_title_shortcode(){
return get_the_title();
}
add_shortcode('page_title','post_title_shortcode');
Thank You
It seems easy. Inside the function post_title_shortcode do something similar:
function post_title_shortcode() {
$replacement = [
'Bay' => '',
// add as many as you want
];
return str_replace(
array_keys($replacement),
array_values($replacement),
get_the_title()
);
}
Write some code in your shortcode handler to adjust the text of the title. This kind of thing might work.
function post_title_shortcode(){
$title = get_the_title();
$title = trim( str_replace( 'Best ', '', $title, 1 ) );
$title = trim( str_replace( 'Top ', '', $title, 1 ) );
return $title;
}
add_shortcode('page_title','post_title_shortcode');
I need to change the page title of this page. I created this page from the category. So the page title is showing the category name. I need to change that page title to News.
I couldn't find an option or setting to change the title so need to change the title by using custom CSS.
This is what I see when I check the coding of the page:
Please help me change the page name to News instead of Category: News
Place this code in your functions.php file
function change_category_page_title( $title ) {
$title_arr = explode(':', $title);
if ( count($title_arr) > 0 ){
if(array_key_exists(1, $title_arr)) {
return $title_arr[1];
}
}
return $title;
}
add_filter( 'pre_get_document_title', 'change_category_page_title', 9999 );
You can use the filter get_the_archive_title for that:
function set_archive_title( $title ) {
if ( is_category() ) {
$parts = explode( ':', $title );
unset( $parts[0] );
$title = trim( implode( ' ', $title ) );
}
return $title;
}
add_filter( 'get_the_archive_title', 'set_archive_title' );
This splits the string in array parts for every occurring :. Then the first part of the array gets removed and then the remaining parts are changed back to a string.
On a WordPress site, using a custom theme, if you hover over a product category in a drop-down menu, you’ll see something like:
Glucose(8)
See also this screenshot: https://monosnap.com/file/nyPmhxJef67uCi6IlivriCLKlXeZ5o
Whereas, if you look at the cart in the header area, there is a space between the text and the ()... For example: https://monosnap.com/file/s2lPYO6aaGU8Fc7Sc6uCMitabBhBvz
I'm trying to making this consistent (plus having some space there looks better), and it was suggested to me to add a filter on the title, using code such as:
function wpforo_add_space_in_menu ( $title ) {
if( !is_admin() && !in_the_loop() ) {
$words = explode( '(', $title );
$words[0] = $words[0] . ' ';
$title = implode( '(', $words );
}
return $title;
}
add_filter( 'the_title', 'wpforo_add_space_in_menu' );
I added this to the theme's "funtions.php" file, but I didn't see anything change afterward.
So I'm wondering if there is anything obviously wrong in the code above, or if you can think of another option to do what I'm trying to do.
I currently have an issue with trimming the length of the product title in the "Related Products" section of my single product page.
I have set up a custom hook pointing to a custom template for this. The only problem is that it isn't trimming any of it - it is still printing the whole title.
I have tried to use get_the_title() and it doesn't print anything to screen, so in the code below I have tried to pass the title as a string and not an array.
What am I doing wrong?
<?php $title = the_title('<h3 class="product_title entry-title">', '</h3>');
$text = wp_trim_words($title, 2, '...')
?>
<?php echo $text; ?>
Thanks in advance
Regards
Michael
the_title() will be auto print as you don't need to echo it.
You need to use filter for title as the following in functions.php,
function trim_title( $title ) {
$title = wp_trim_words( $title , 40, '...' );
return $title;
}
add_filter( 'the_title', 'trim_title', 10, 1 );
This solution doesn't work in up-sells.php even if it's almost the same as related.php , instead of this whole function trim_title( $title ) in both files after $post_object = get_post( $upsell->get_id() ); (or $related_product) I added $post_object->post_title = substr( $post_object->post_title, 0, 50) . '…'; and it works. It limits to 50 characters, you can change.
I am making a WordPress theme and I want to add different icons for Category. Basically I want if I select Music category while adding a WordPress Post then there should be Music icon before category in single post.
( Screenshot added)
If I select Image category then there should be Image icon before category in Single Post Page. I am using WordPress and I actually saw this work on this WordPress theme:
I am planning to use Font-Awesome icons... Anyone here for help :)
Apply a filter on the_category hook to modify the HTML of your the_category function and add a class name. This code was obtained from a WordPress StackExchange answer.
function add_class_callback( $result ) {
$class = strtolower( $result[2] );
$class = str_replace( ' ', '-', $class );
$replacement = sprintf( ' class=""><i class="fa %s"></i>%s</a>', $class, $result[2] );
return preg_replace( '#>([^<]+)</a>#Uis', $replacement, $result[0] );
}
function add_category_slug( $html ) {
$search = '#<a[^>]+(\>([^<]+)\</a>)#Uuis';
$html = preg_replace_callback( $search, 'add_class_callback', $html );
return $html;
}
add_filter( 'the_category', 'add_category_slug', 99, 1 );
The above function should add the required markup. Then, include the following CSS in you stylesheet.
.post-categories .music:before {
content: "\f001";
}