Skip Navigation

[Résolu] Dynamic Meta Data based on custom field, or view data

This support ticket is created Il y a 7 années. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par stuart Il y a 7 années.

Assisté par: Nigel.

Auteur
Publications
#591160

I would like to alter the meta data in the header dynamically based on URL parameters

I have a view that is on a custom post type
URL like this:
lien caché

I would like to use the shortcode:
[wpv-search-term param='catlong']
[wpv-search-term param='location']
to set the Meta Title of the page to Women Looking for Men London.

ive found a couple of options that look like they could help.. but alas. they cant .
https://wordpress.org/plugins/wp-dynamic-keywords-injector/ (the meta title option doesnt work, buts a good idea)

lien caché
(This uses a custom field that is not yet posed into... if it could use the wpv-search-term with the parameters then it would).

Any ideas to how i could achieve this, even if they are pointers id be happy.

#591193

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi Stuart

Toolset doesn't include any capabilities for modifying the page head, only the body, so your solution will require custom coding.

In this case it should be fairly straightforward. You can edit the header.php file from your theme to insert the meta tags you require, and if you want the values of those tags to be generated dynamically with the wpv-search-term shortcode, you can add the shortcode to the PHP template by using the built-in WP function do_shortcode: https://developer.wordpress.org/reference/functions/do_shortcode/

I haven't tested it but for URL parameters I think the wpv-search-term shortcode should work in that context, and the $post object is available by then, so you should be able to use shortcodes to output custom field values for the post being displayed.

#591343

Thanks Nigel as always I think this is enough for me to have a play.
One more thing on this I just realised, I only need this to be applied on certain CPT that are dynamically generated content and not on all CPTs. I may be making this way too complicated for myself.

thanks

reference I found for any non php developers reading this
lien caché

#591433

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi Stuart

If you want to apply this to only certain CPTs you can either include some if statements in your header to check the post type (using conditional tags, in this case get_post_type as described here: https://developer.wordpress.org/themes/basics/conditional-tags/#a-post-type), or by making a specific PHP template for that post type, according to the template hierarchy (https://developer.wordpress.org/themes/basics/template-hierarchy/).

For a post type of, say, project, that involves copying your theme's single.php to a file named single-project.php which will then be used for displaying single project posts. You can then make edits to that file which will only affect that post type.

In this case, you are making changes to header.php, so you would need to make an alternative header.php file and edit single-project.php to call your alternative using an argument with get_header(): https://codex.wordpress.org/Function_Reference/get_header

#591804

Thanks Nigel, i`ll be testing that. Seems pretty straightforward for my non php skills.

#603934

It appears what I am trying to do is a little more complex than first thought... none of the above was effective due to the way wordpress now works with the header.

However the fix i managed to achieve is letting WordPress render shortcodes in the page Title.

This allows me to set the URL parameters and then add the search term short codes to the title of the pages like this:

Title: [wpv-search-term param='catlong'] in [wpv-search-term param='location']
renders: <title>A Women Looking For Men in London</title>

Here is how i got around it (took a while to work out, and there may be a better way?).

document_title_parts is the key... and where i found this info
lien caché

add_filter('document_title_parts', 'wpmu_doc_title_shortcode', 10);
function wpmu_doc_title_shortcode($title) {
$my_title = $title['title'];
$my_title = do_shortcode($my_title);
$title['title'] = $my_title;
return $title;
}

The only issue with this would be third parties 'could' inject keywords in the the dynamic variables in the URL and then index random pages.... however i see this as pretty unlikely and the same process is actually being done on Gumtree... see here

gumtree com au/s-cheese+maker/k0 (remake the url)
Change cheese+maker and add your own keywords and see what happens.