Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

CPT Archive customization highlighting categories

  • hi Team,

    this is basically a follow up to: https://generate.support/topic/archive-page-with-looping-over-subcategories/

    I’ve followed David’s approach and created an archive-CPT.php for my CPT type (slug) and copied over the archive.php content from the GP theme.

    Starting _simple_ I do want to list all categories (+ their assigned image) prior highlighting the individual list of posts.

    <?php
    /**
     * The template for displaying Archive pages.
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    	<div <?php generate_do_attr( 'content' ); ?>>
    		<main <?php generate_do_attr( 'main' ); ?>>
    			<?php
    			/**
    			 * generate_before_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_before_main_content' );
    
    			$subcategories = get_terms( array(
    				'parent' => 0,
    				'taxonomy' => 'CPT-category',
    				'hide_empty' => true)
    			);
    			echo '<ul>';
    			foreach ($subcategories as $cat) {
    				$image = get_field('category_image', $cat);
    				if ($image) {
    					echo wp_get_attachment_image($image, 'medium');
    		
    				}
    				echo '<li>';
    				echo '<a href="' . get_term_link($cat) . '">' . $cat->name . '</a>';
    				echo '</li>';
    			}
    			echo '</ul>';
    ...
    

    This works as expected. Is it possible to use the GP/GB templating to render a template inside the foreach ($subcategories as $cat), allowing me to not fiddle much with CSS\layout in this php file?

    For reference, I’m using GPP,GBP with the new release candidate.

  • Hi there,

    see lines 58 – 62 of my original GIST:

    https://gist.github.com/diggeddy/c2bdc6190a314238d15bad6979a5ac34#file-category-php-L60

    There is a nested foreach loop which returns the posts for the current category. And within that is the:

    generate_do_template_part( 'archive' );

    That function returns the theme content.php template, which can be overwritten using the GP Element – Content Template.

  • Thank you David, this made it more clear.

    I assume, it is not possible to have a template showing a single card layout (for example) in which I can inject the data? Basically I want to be able to design the container via a template: https://gist.github.com/diggeddy/c2bdc6190a314238d15bad6979a5ac34#file-category-php-L52

    The idea is that I have a some card design based on the categories prior showing the archive page. Something like:

    
    foreach ($categories as $cat) {
      generate_do_template_part(...)
    }
    
    generate_do_template_part( 'archive' );
    

    Thanks again, David

  • Ah ok.
    It is possible but there are things to consider.
    For example inside your foreach loop you could add your own Hook
    eg.

    do_action('my_custom_hook_name');

    Then you could hook into that using a GP Element, theres an option to choose a Custom Hook where you can type your own hook name

    But… you need to consider how to get the dynamic content you want to display in that template…

  • Yeah this seems to be something I’m keen on.
    Is there a way to use GenerateBlocks_Register_Dynamic_Tag with a list of objects and use them inside a loop?

  • Thank you, I was not able to get it running but embed the content now via a shortcode.

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