Archived topic
Header Element Template Tag For Page SEO Meta
8 replies · Started by Stephen on August 4, 2020
Hello,
I've created a Header Element for Product Category Archives. It's a simple header section that needs to pull in a title and description. I can do this with the code below:
<h1>{{post_title}}</h1>
<p>[term_description]</p>
However, this doesn't work the way I need it for product filtering pages. I'm looking to have it so the title and description pulled in are from the seo meta of the page that you're currently on.
Linked the site below. Basically 'Carpets' is the product category name. 'a-colour-groups-beige' is the url suffix when the 'beige' filter is selected. The filter plugin allows for custom SEO rules, so the 'beige' selection has it's own meta description and title. It's this information that needs to be in the header. Is there are shortcode or template tag I can use in this header element to pull this in?
Thanks in advance
Hi there,
you could create your own shortcodes if you know the meta keys used by the SEO rules plugin.
Example for a [SEO-Title] shortcode
add_shortcode( 'SEO-Title', function() {
// Get your title
$seoTitle = get_post_meta( get_the_ID(), 'ADD-THE-POST-META-KEY-HERE', true );
// Output title if complete
if ( $seoTitle ) {
ob_start();
echo $seoTitle;
return ob_get_clean();
}
});
Thanks for the reply David.
I'm not sure what the meta keys are. The plugin is premmerce and they aren't overly helpful. Do you know what they should look like and where I could find them?
I think their seo plugin works alongside Yoast.
If it were using yoast meta keys would the snippet look like this:
add_shortcode( 'SEO-Title', function() {
// Get your title
$seoTitle = get_post_meta( get_the_ID(), '_yoast_wpseo_title', true );
// Output title if complete
if ( $seoTitle ) {
ob_start();
echo $seoTitle;
return ob_get_clean();
}
});
That would be correct for Yoast - might be worth trying them as there is a chance premmerce is writing into them.
If not there is this plugin which will display post meta - although if its dynamic it may not show:
Unfortunately that plugin doesn't display the metadata on the premmerce seo rule section.
The Yoast meta keys don't seem to work either. I'm looking through the premmerce seo plugin in the plugin editor to see how it works but it's alien to me. I can see there are references to Yoast but can't see the connection or what the meta keys could be.
I added Yoast and tested the shortcode made using the above snippet but it doesn't seem to be working.
<h1>[SEO-Title]</h1>
<p>[SEO-Description]</p>
The h1 and p tags have nothing in them on the frontend.
Hi there,
That likely means the post meta keys aren't correct, or there's no value added to them.
We're not aware of that plugin specifically, so we wouldn't know what they'd name their custom fields, unfortunately.
Hi Tom,
I've disabled the Premmerce plugin and just have Yoast but the shortcode suggested isn't pulling through the meta title and description on any of the pages, wherever I put it.
I might not even need it. There might be another solution to my SEO issue. Carpets is a 'Product Category Archive' and pulls through the header element I want (because I can target that specifically). I need a way to target the sub sections of Carpets, or target specific URLs with a hook or a header element. Is there a way to do that?
There's a similar request here where the user wanted URLs for display rules:
https://generatepress.com/forums/topic/feature-request-6/#post-1385716
Instead of the generate_hook_element_display filter you can use the generate_header_element_display
https://docs.generatepress.com/article/generate_header_element_display/