Site logo

Archived topic

hooks in posts

5 replies · Started by Ignacio on July 3, 2018

Viewing posts 1–6 of 6

Hi.
The hooks zone is perfect for adding code in certain zones. But I'm looking for a way to add that code individually in the posts. That is, in the post edition I can add code to the header hook. So each post will have a different thing added.
Any recommendations?
REgards and thanks

Hi there,

simplest method is to use the WordPress Custom Fields, create your field and add the data you want to display and then add this to the hook changing Your-Custom-Field to your field name.

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>

You can also do this in Page Headers, same process of creating the custom field but then simply add this template tag:

{{custom_field.name}}

The name being the name of the field

Hi David

Yes, I had thought that one option could be a custom field text, and then make the header display that field if it contains something. Just what you proposed. But I that code would not have known how to do it hehe. Thank you so much for the code. Works perfectly

One last question. In case I don't have the option to generatepress and the hooks, I can't find a plugin that allows me to manage the hooks in an easy way.
What I've done is add your code in:

function custom_hook() {
	.........
}
add_action( 'wp_head', 'custom_hook' );

and it also works.

Regards

wp_head and wp_footer hooks are part of WordPress core so should be in all themes. They however should be used for adding scripts / meta data etc. not really for outputting content. There are other WP ones you can output to such as those in the comments box.
The rest will be theme or plugin dependent. So will vary from one to another. I think GP set the standard for introducing such an array of hooks that are accessible.

yes, yes, the hooks should be, but usually the themes don't give the ease of management as GP does.
And correct, they're for adding scripts. I'll add json, for the schema.
Thank you so much for your help, David.

Yes GP does make it super easy. And its going to get even easier very soon ;)

This archived topic is closed to new replies.