[Resolved] Add custom Element to All archive pages

Home Forums Support Add custom Element to All archive pages

  • This topic has 8 replies, 2 voices, and was last updated 11 months ago by Tom.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1040581
    Heather

    Hi there,

    I would like to add a custom CTA in the 2nd row of all my blog archive pages. I currently have my layout as a 3 column grid showing 9 posts, so I would like the pages to essentially be:

    post post post
    post post cta
    post post post

    pagination

    I know I can add something custom to the archive.php pages, but I am not sure where it would go in the code as it seens like the pages are set up to just do_action (GP theme specific hook/section). Where/how would I add this in the following code:

    <div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    		<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
    			<?php
    			/**
    			 * generate_before_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_before_main_content' );
    
    			if ( have_posts() ) :
    
    				/**
    				 * generate_archive_title hook.
    				 *
    				 * @since 0.1
    				 *
    				 * @hooked generate_archive_title - 10
    				 */
    				do_action( 'generate_archive_title' );
    
    				while ( have_posts() ) : the_post();
    					/*
    					 * Include the Post-Format-specific template for the content.
    					 * If you want to override this in a child theme, then include a file
    					 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
    					 */
    					get_template_part( 'content', get_post_format() );
    
    				endwhile;
    
    				/**
    				 * generate_after_loop hook.
    				 *
    				 * @since 2.3
    				 */
    				do_action( 'generate_after_loop' );
    
    				generate_content_nav( 'nav-below' );
    
    			else :
    
    				get_template_part( 'no-results', 'archive' );
    
    			endif;

    Also, I can not provide a url/login info as this is all being done for a company on my local.

    #1040790
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    As of right now it’s possible without creating a custom template file.

    What you want to do is insert your code after the get_template_part( 'content', get_post_format() ); line.

    The code you enter should look something like this: https://crunchify.com/how-to-insert-ads-on-home-page-after-2nd-and-5th-post-in-genesis-framework/

    I’m working on adding a hook in that area, but want to make sure we do it right (possibly hooking the get_template_part into the same hook.

    #1042683
    Heather

    Does GP use Genesis? Not sure how I would make this code work specifically for GP. I’ve tried to add code after that part, but it messes with the columns layout of the page, so it turns my 3 columns into 2, with a cta randomly on the right side where the 3rd column exists.

    Can I create an additional get_template_part or something underneath using that loop counter?

    #1042930
    Tom
    Lead Developer
    Lead Developer

    It doesn’t – I linked to that article because of the specific code they’ve used to insert the ad, which you can place in your custom template:

    global $loop_counter;
    $loop_counter++;
    
    if( (!is_paged() && !is_singular()) && ($loop_counter == 1 || $loop_counter == 4)) {
        echo '<div style="margin-bottom:25px;"><ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-4032710958875645" data-ad-slot="7425196307"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>';
        $loop_counter = $loop_counter + 1;
    }
     
    global $loop_counter_paged;
    $loop_counter_paged++;
    if( (is_paged() ) && ($loop_counter == 2 || $loop_counter == 5) ) {
        echo '<div style="margin-bottom:25px;"><ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-4032710958875645" data-ad-slot="7425196307"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>';
        $loop_counter_paged = $loop_counter_paged + 1;
    }

    We don’t have a hook right now, so a custom template is needed, but I’m looking into adding one in the near future.

    #1044385
    Heather

    Ok, so I should add this code right below the get_template_part( ‘content’, get_post_format() );

    Could you target get_template_part with a custom hook? Like creating an after_get_template_part by selecting custom hook when making a new one?

    #1044455
    Heather

    So I added that code under where you said and it worked! Which is great.

    However, it seems to be showing up on category archive pages, but not the blog archive (“posts page”) itself.

    I added the code to archive.php, is there another php file specific to the blog archive page that shows all posts? I want to add this there as well.

    #1044676
    Tom
    Lead Developer
    Lead Developer

    They don’t have a hook there, unfortunately.

    You need to add it to the index.php file as well 🙂

    Hope this helps!

    #1046972
    Heather

    That worked, thanks so much!

    #1047296
    Tom
    Lead Developer
    Lead Developer
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.