Site logo

[Support request] How to customize CPT archive page with Element

Home Forums Support [Support request] How to customize CPT archive page with Element

Home Forums Support How to customize CPT archive page with Element

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1830101
    naisyo523

    Hello,

    Love the GP Theme 🙂

    I’m trying to do the following I appreciate your help if possible (I looked around a lot to find answers but could not find answers to match my situation):

    With CPT UI/Advanced Custom Fields plugins, I’ve created a “Free magazine pdf download archive page”. (included the link in private)
    On this archive page, all links open the PDF files. (There are no single post pages on this CPT)

    • On this archive page (we call the “Back Numbers” page), I’d like to add description text below the title that only showing up on this page.
    • In order to do that, I went to “Element” and created a new hook to show at “generate_after_archive_title”.
    • Then at “Display Rule”, I was expecting to be able to find the “Back Number” archive page but there weren’t any.
    • So I could not insert this custom paragraph I wanted for this particular page.

    I have created archive-back-numbers.php and content-back-numbers.php in my Child Theme.
    I’m not a PHP developer, so I may not have set things up correctly…

    Thank you in advance,

    Naisyo523

    #1830122
    Ying
    Staff
    Customer Support

    Hi there,

    Glad to see the positive feedback for GP!

    It sounds about right, you should be able to see the CPT archive page in the display rule.

    Can you try to switch to the parent theme?

    Let me know if it works 🙂

    #1830306
    naisyo523

    Hi! Thanks for the reply.

    I did switch to the parent theme.

    Then I went to the Element, Display Rules > Location > Post Archive. I’m not seeing CTP to select from.

    Here’s my archive-back-numbers.php code

    <?php
    /**
     * The template for displaying Archive pages for バックナンバー.
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    		<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
    			<div class="back-number-flex-container">
    				<?php
    				/**
    				 * generate_before_main_content hook.
    				 *
    				 * @since 0.1
    				 */
    				do_action( 'generate_before_main_content' );
    	
    				if ( generate_has_default_loop() ) {
    					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();
    	
    							get_template_part( 'content', 'back-numbers' );
    	
    						endwhile;
    	
    						/**
    						 * generate_after_loop hook.
    						 *
    						 * @since 2.3
    						 */
    						do_action( 'generate_after_loop', 'archive' );
    	
    					else :
    	
    						generate_do_template_part( 'none' );
    	
    					endif;
    				}
    	
    				/**
    				 * generate_after_main_content hook.
    				 *
    				 * @since 0.1
    				 */
    				do_action( 'generate_after_main_content' );
    				?>
    			</div><!-- END back-number-flex-container -->
    		</main>
    	</div>
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    	get_footer();
    

    Here’s content-back-numbers.php code:

    <?php
    /**
     * This is displayed in archive page 
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    ?>
    
    	<div class="back-number-flex-item">
    		<header class="entry-header">
    		<?php
    		$file = get_field('pdf');
    		if( $file ): ?>
    		<h2><a href="<?php echo $file['url']; ?>" target="_blank" title="<?php the_title(); ?>&nbsp;Vol.<?php the_field('vol'); ?> (PDF) が、新しい窓で開きます!"><?php the_title(); ?>&nbsp;Vol.<?php the_field('vol'); ?></a></h2>
    		<?php endif; ?>
    		</header><!-- .entry-header -->
    		
    		<div class="col1">
    			<figure class="wp-block-image size-large">
    			<?php
    			$file = get_field('pdf');
    			if( $file ): ?>
    			<a href="<?php echo $file['url']; ?>" target="_blank" title="<?php the_title(); ?>&nbsp;Vol.<?php the_field('vol'); ?> (PDF) が、新しい窓で開きます!"><?php the_post_thumbnail( 'back-number-cover' ); ?></a>
    			<?php endif; ?>
    			</figure>
    		</div>
    		
    		<div class="col2">
    			<p class="published-date">
    			発行日:<?php the_field('published-date'); ?>
    			</p>
    		
    			<div class="detail">
    			<?php the_field('detail'); ?>
    			</div>
    		
    		<?php
    		$file = get_field('pdf');
    		if( $file ): ?>
    			<div class="read-more-button">
    				<a href="<?php echo $file['url']; ?>" target="_blank" rel="noopener noreferrer" title="<?php the_title(); ?>&nbsp;Vol.<?php the_field('vol'); ?> (PDF) が、新しい窓で開きます!">内容を読む</a>
    			</div>
    		<?php endif; ?>
    		</div>
    	</div><!-- END class="back-number-flex-item -->
    
    		<?php
    		/**
    		 * generate_after_entry_content hook.
    		 *
    		 * @since 0.1
    		 *
    		 * @hooked generate_footer_meta - 10
    		 */
    		do_action( 'generate_after_entry_content' );
    
    		/**
    		 * generate_after_content hook.
    		 *
    		 * @since 0.1
    		 */
    		do_action( 'generate_after_content' );
    		?>
    

    Maybe I’m doing something wrong…?

    #1830409
    Elvin
    Staff
    Customer Support

    Hi there,

    I see more issues here than the missing display rule location.

    I don’t think generate_after_archive_title hook would work on that template because the template file content-back-numbers.php doesn’t have a do_action('generate_after_archive_title') link how the default template is structured – https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/archives.php#L40

    As for the display rule:

    The display rule for a CPT doesn’t appear under Posts. Any CPT will have its own item on the dropdown.

    Example – https://share.getcloudapp.com/mXurKylL

    Note: If the archive option doesn’t appear on the dropdown options, it’s likely that you didn’t have it enabled on the CPT UI.

    Make sure it’s enabled as shown here – https://share.getcloudapp.com/JruDx6RN

    #1831378
    naisyo523

    Hi there!

    Thanks for the suggestions.

    The display rule for a CPT doesn’t appear under Posts. Any CPT will have its own item on the dropdown.
    Example – https://share.getcloudapp.com/mXurKylL

    Note: If the archive option doesn’t appear on the dropdown options, it’s likely that you didn’t have it enabled on the CPT UI.

    My CPT UI “Has Archive” was set to “True” but my setting of “Public” was set to “False”. That was the reason why my Element > Display Rule wasn’t showing the correct archive page ;b

    I made the setting “Public” to be “False” because I read in the articles that that’s what I should do when my CPT archive page do NOT need single pages to link to. I know this is not related to GP but if any of you guys have suggestions on how to properly handle this? (I need a CPT archive page only, not detail single pages)

    Thanks again for your time!

    #1831593
    Elvin
    Staff
    Customer Support

    We can just keep your CPT setting as-is and let’s just set the display rule location of the GP element using filters.

    Can you share the ID of the GP element in question? Also, can you share what kind of element are you using? (block or hook)

    This is for the filter writeup.

    We’re going to write a PHP snippet of something like this:

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
      if ( 123 === $element_id && is_post_type_archive( 'back-numbers' ) ) {
         $display = true;
      }
    
      return $display;
    }, 10, 2 );

    We use generate_block_element_display for block element, generate_hook_element_display for hook element.

    123 === $element_id is basically targeting the element you want to use, && is_post_type_archive( 'back-numbers' ) is the part telling to only apply it on back-numbers post archive page.

    Let us know.

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