[Support request] Insert custom content after 3rd post

Home Forums Support Insert custom content after 3rd post

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #954726
    Vlad

    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! Vlad

    #954763
    David
    Staff
    Customer Support
    #954814
    Vlad

    Hi 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, Vlad

    #954827
    David
    Staff
    Customer Support

    I 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.

    #954832
    Vlad

    I 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 the generate_before_main_content is declared within the theme functions? It’s being used in the index.php with do_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, Vlad

    #954884
    David
    Staff
    Customer Support

    Custom 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:

    https://wordpress.stackexchange.com/questions/141253/hook-into-the-loop-via-a-plugin-and-output-something-after-every-x-post

    #955015
    Vlad

    Thank 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.
    Vlad

    #955033
    David
    Staff
    Customer Support

    Hooks 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(); ?>

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.