- This topic has 7 replies, 2 voices, and was last updated 4 years, 9 months ago by David.
-
AuthorPosts
-
July 11, 2019 at 3:50 am #954726Vlad
Hi there,
I have the latest version of the GP theme and premium plugin and I have this frustrating issue I am unable to solve:
I am trying to create a custom hook that will allow me to insert content (AdSense code or plain images) between posts on the main page (eg. after the 3rd and 4th post or something like that). I am using a GP child theme, just to be able to modify whatever file needs to be modified.I think this could be achieved by modifying the_query, something like this:
if ( have_posts() ) : while ( have_posts() ) : the_post(); $coounter++; if ($counter == 3) : do_action( 'generate_between_posts_custom_hook' ); endif; get_template_part( 'content', get_post_format() ); endwhile; endif;
However, I am stuck at this. How am I supposed to create and use the
generate_between_posts_custom_hook
custom hook on this one?
Thank you! VladJuly 11, 2019 at 4:43 am #954763DavidStaffCustomer SupportHi there,
Tom provides a reply here on a similar request:
July 11, 2019 at 5:48 am #954814VladHi David,
I know about Tom’s post, I’ve browsed the forum a bit before posting.
Actually, I am not trying to create a widgetized area, I just need to hard code a custom hook right after Nth post with whatever content I might save in the hook (plain image or plain text) and display it on the front page via the index.php file.I have some experience tweaking WP themes, so if it involves editing .php files, I’ll do it, just need some guidance here.
Now, how do I do this, how do I create a custom hook to accomplish this without installing any additional plugins? If it’s not for this, what are the Custom Hook and Custom Hook Name for?
Thanks, VladJuly 11, 2019 at 6:00 am #954827DavidStaffCustomer SupportI referenced that post as Tom states to do this is quite complicated. It is not often that Tom would say this which means to code this would be beyond our scope.
The AdInserter plugin he mentions does all of what you require and would be the easiest method. Otherwise you would need to look for custom development to create the necessary template.
July 11, 2019 at 6:12 am #954832VladI see. Well, I’ll have to find a another way anyway 🙂
If it’s not too much, could you tell me how to use the ‘Custom Hook’ and ‘Custom Hook Name’ fields in the ‘Add New Element’ dashboard screen? Also, could you tell me where thegenerate_before_main_content
is declared within the theme functions? It’s being used in the index.php withdo_action( 'generate_before_main_content' );
right before The Loop and maybe I can create a similar function in order to show some custom content.
Thanks a lot, VladJuly 11, 2019 at 7:46 am #954884DavidStaffCustomer SupportCustom Hook field in Elements is for using a pre-defined hook that is not in the hook list. For example if you add a plugin that has a known hook you can add that in the Custom Hook Field.
That hook is declared in the various templates for example you can see it here in the archive.php
https://github.com/tomusborne/generatepress/blob/master/archive.php
Its simply declares the hook. Then functions/content are hooked into it.
You may find this stackoverflow QA of interest:
July 11, 2019 at 7:57 am #955015VladThank you, David! I was actually asking where the
generate_before_main_content
function is declared (like functions.php or other file like it), not where it’s called, I saw in the templates, but I need to know where and how to declare a similar function so then I can call it for my own custom hook.
Hope this helps.
VladJuly 11, 2019 at 8:19 am #955033DavidStaffCustomer SupportHooks are a function of WordPress so you simply declare them like so:
do_action('my_hook');
If you want you create a function:
function my_hook() { do_action('my_hook'); }
And then call that function:
<?php my_hook(); ?>
-
AuthorPosts
- You must be logged in to reply to this topic.