Wordpress: Custom <title> for archive pages - wordpress

I'm having a little trouble modifying the page title for my archive pages. I have a custom post type for movies, and I can't seem to alter it. Right now it reads "Movies archive". I'd like to change it altogether to something like "Programme".
Any ideas?
Thanks!

It depends on the function your template is using to output the title of your custom post type archive.
I guess it's using either wp_title or get_the_archive_title so you can try adding a filter (inside functions.php of your theme):
function movies_archive_title( $title ) {
if(is_post_type_archive('movie'))
return 'Programme';
return $title;
}
add_filter( 'wp_title', 'movies_archive_title' );
add_filter( 'get_the_archive_title', 'movies_archive_title' );
If the page viewed is the movies archive (be sure to replace 'movie' with the exact name of your custom post type name) then it replace the title with Programme.

You can add a filter in functions.php like this to override the title, yet preserve the site name and title separator:
function movies_archive_title( $title ) {
$site_name = get_bloginfo();
$sep = apply_filters( 'document_title_separator', '|' );
$sep = str_pad( $sep, 30, " ", STR_PAD_BOTH );
if(is_post_type_archive('movie'))
return 'Programme'.$sep.$site_name;
return $title;
}
// Raise priority above other plugins/themes that
// have effect on the title with 900 (the default is 10).
add_filter( 'wp_title', 'movies_archive_title', 900 );
add_filter( 'get_the_archive_title', 'movies_archive_title', 900 );
Replace movie with your custom post-type name and Programme with the desired title in the above example.
With some plugins and themes you need to raise the priority with the priority parameter of the add_filter method or you won't see a change.

Related

Add blocks to custom location on shop page?

On my woocommerce shop page I want to remove the description/blocks from the all-in-one woocommerce product grid block. Then add them back above it separately for layout reasons.
So I've removed the description like this:
remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
Then I thought I could just add it back in with a shortcode block in the archive-product.html template.
add_shortcode('shop_description', 'woocommerce_product_archive_description');
The problem is, this adds it to the very top of document even before the <html> tag, instead of where I put the shortcode block...
I realised the woocommerce_product_archive_description function uses echo and to make it work with a shortcode it needs to instead use return.
So I made a new function for the shortcode and boiled it down to be:
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$allowed_html = wp_kses_allowed_html( 'post' );
$description = wc_format_content( wp_kses( $shop_page->post_content, $allowed_html ) );
if ( $description ) {
return $description;
}

Add itemReviewed props into woocommerce product loop

On my woocommerce theme I get some errors on Google structured data such as:
I've found code into mytheme/woocommerce/loop/ratings.php
In this file there are only this method:
$product->get_average_rating();
The question is: there's a hook or action that implement this function?
I need to implement "ItemReviewed" props.
This is down to your product schema, option one is to remove the error but wont push the reviews is
/**
* Remove the generated product schema markup from Product Category and Shop pages.
*/
function wc_remove_product_schema() {
remove_action( 'woocommerce_shop_loop', array( WC()->structured_data, 'generate_product_data' ), 10, 0 );
}
add_action( 'woocommerce_init', 'wc_remove_product_schema' );
if you want to aggregate your reviews you will need to update your schema if you are using yoast and woocommerce this can be achieved by looking into the specific yoast woocoommerce plugin and the plugin docs to add the correct schema
https://developer.yoast.com/schema-documentation/woocommerce-seo/
I had the same problem - reviews were not validating and throwing an error
Managed to get it fixed by editing /wp-content/themes/YOURTHEME/woocommerce/single-product/review.php
I added the below code:
<p>Item Reviewed: <span itemprop="itemReviewed"><?php echo get_the_title(); ?></span></p>
That generated the itemReviewed markup in Structured Data Tool and my reviews validated.
Might want to place it in the child theme just in case :)
define the woocommerce_structured_data_review callback
function filter_woocommerce_structured_data_review( $markup, $comment ) {
global $product;
$markup['itemReviewed']['sku'] = $product->get_sku();
$markup['itemReviewed']['brand'] = $product->get_attribute( 'brand' ) ?? null;
$markup['itemReviewed']['description'] = wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) );
$markup['itemReviewed']['image'] = wp_get_attachment_url( $product->get_image_id() );
$markup['itemReviewed']['isbn'] = $product->get_attribute( 'isbn' ) ?? null;
$markup['itemReviewed']['AggregateRating'] = $product->get_average_rating();
return $markup;
};
Woocommerce Reviews filter
add_filter( 'woocommerce_structured_data_review',
'filter_woocommerce_structured_data_review', 10, 2 );

Override WooCommerce Shop Site Breadcrumb with Yoast SEO Breadcrumb

I tried to remove the WooCommerce Breadcrumbs with this code:
add_action( 'init', 'remove_wc_breadcrumbs' );
function remove_wc_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}
...which works.
After that I activated the Yoast Seo Plugin Breadcrumbs an placed this code in my theme header.php
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
...which also works.
The problem appears when I navigate to my "shop-site" which I defined in the WooCommerce settings. The full breadcrumb looks like this
Home > Product
Note: The Page name in WordPress for the "shop-site" is "Test" (not "Product").
I want to override the "Product" in something different. I tried to override it by defining a custom breadcrumb title in the advanced Yoast settings on the wordpress "shop-site" page but it didn't seems to work. Overriding works if the page isn't defined as the "shop-site" in the WooCommerce settings. So I guess some function overwrites the Yoast Breadcrumb override but I can't figure out which function does this.
The same problem appears when I navigate to a single product. Breadcrumb looks like this
Home > Product > Product Name
Again I want to rename the "Product" (only the second crumb here) into something different.
Can someone solve this mysticism?
I fixed it with this code:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_product() ){
$from = 'rel="v:url" property="v:title">Product</a>';
$to = 'rel="v:url" property="v:title">New Title</a>';
$output = str_replace( $from, $to, $output );
}
elseif ( is_shop() ) {
$from = '<span class="breadcrumb_last">Products</span>';
$to = '<span class="breadcrumb_last">New Title</span>';
$output = str_replace( $from, $to, $output );
}
return $output;
}
I wonder if there wouldn't be an easier way to change the first "product" into sth like "product category"...
Have you tried in wp-admin:
Go to "Yoast"
Then "SEO settings"
Then "Breadcrumbs" and adapt taxonomies (eg: product > category).

Function to Change Page Title In Wordpress

I am using the below function to change the page title in wordpress. It is working on a single page (page.php), but is not working on my static home page or individual posts (single.php).
What do I need to change in order to make this work across the entire site?
<?php
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'wp_title', 'wpse46249_filter_wp_title' );
?>
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'the_title', 'wpse46249_filter_wp_title' );
please note the title is saved in wp_posts table. The filter you used above will change all new posts titles being saved. This filter just modifys the title after pulling it from the db and doesn't actually change the db value.
Also will only work on pages where the_title() is called.

How Get Unique meta title (post number), when Post Title already exist

As i know, WordPress automatically adds suffix in the end of permalink (post name) if a post with the same name already exists in the database.
But not adds suffix in the end of Meta title, because its depend on themes.
Anyway im using Thesis Themes, no duplicate meta title for archive meta title,
HOEMPAGE
mydomainname.com = in page 1, the title is <title>My Homepage Title</title>
mydomainname.com/page/2 = in page 2, the title is <title>My Homepage Title — Page 2</title>
CATEGORY
mydomainname.com/category/category-title = in page 1, the title is <title>My Category Title</title>
mydomainname.com/category/category-title/page/2 = in page 2, the title is <title>My Category Title — Page 2</title>
TAGS
Its same, always unique dynamically.
More and more page 3,4,5++ title still unique (not duplicate) AUTOMATICALLY.
Im using Thesis Themes on my blog (Public user can update post on Fropnt End), user only create post on Front End, Authors can't access my wp-admin CMS and Thesis "Details and Additional Style" fields. So, How about Post Entry Title? How to get this features when user Create New Post with the same Title?
Example, im trying to do like below:
webresepi.com/sambal-udang = first post meta title is <title>Sambal Udang</title>
webresepi.com/sambal-udang-2 = this post title already exist on my database, so i want this post meta title like this <title>Sambal Udang — 2</title>
get number from that permalink when Post Title is already exist on database.
This uses the same idea as faa, but his solution has lots and lots of things in it that will prevent it from working. Use FTP to find your theme in the folder /wp-content/themes/ and then put this piece of code in functions.php:
add_action( 'template_redirect', function() {
if( is_single() ) { // Is a single post
$slug = basename( $_SERVER['REQUEST_URI'] ); // The slug e.g. sambal-udang-2
$slug = ucwords( str_replace( '-', ' ', $slug ) );
add_filter( 'wp_title', function( $title ) use( $slug ) {
return $slug;
}); // This filter overrides wp_title which is used to print the meta title. It replaces whatever would we written normally with our edited version of the slug.
}
});
The above piece of code assumes your server uses PHP version 5.3 or later. For support on older systems, we need to do away with anonymous functions:
add_action( 'template_redirect', 'xyz_tmplt_redirect' );
function xyz_tmplt_redirect() {
if( is_single() ) {
add_filter( 'wp_title', 'xyz_change_title' );
}
}
function xyz_change_title( $title ) {
$slug = basename( $_SERVER['REQUEST_URI'] ); // The slug e.g. sambal-udang-2
$slug = ucwords( str_replace( '-', ' ', $slug ) );
return $slug;
}

Resources