[Support request] Issue deploying custom single.php per post category in GeneratePress child theme

Home Forums Support [Support request] Issue deploying custom single.php per post category in GeneratePress child theme

Home Forums Support Issue deploying custom single.php per post category in GeneratePress child theme

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2261883
    Jack

    I have a need for different single.php and content.php files for various post categories. I did the following:
    1. Downloaded and installed the Generate Press child theme.
    2. Added the following to functions.php:
    /* * Define a constant path to our single template folder */ define(SINGLE_PATH, TEMPLATEPATH . '/single'); /** * Filter the single_template with our custom function */ add_filter('single_template', 'my_single_template'); /** * Single template function which will choose our template */ function my_single_template($single) { global $wp_query, $post; /** * Checks for single template by category * Check by category slug and ID */ foreach((array)get_the_category() as $cat) : if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php')) return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'; elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php')) return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'; endforeach; }
    This directs WordPress to look for a single file in a /single folder of the theme that matches the category slug. I create a single folder, and create a single-cat-apps.php file. In the root of the theme, I create a content-cat-apps.php file based on the content-single.php file that contains the meat and potatoes of the post’s tweaked layout. In the single file, I have the following directive:
    generate_do_template_part( 'content-cat-apps' );
    Yet whenever I pull up a post in the category in question, the custom layout refuses to show. It’s as if the theme isn’t even aware of the single files or the content file. Any help on this would be appreciated.

    #2262082
    David
    Staff
    Customer Support

    Hi there,

    is it possible to see the post / archive where the issue is ?

    #2262551
    Jack

    I actually have the code for single-cat-apps.php and content-cat-apps.php so you can see what’s going on behind the scenes. Unfortunately there’s quite literally nothing to look at from the side of hte post as nothing changes when applying the template. Forgot to indicate this earlier, but I do have the elements module enabled so custom code in functions.php should be enabled.
    Ok, so per the directives in functions.php earlier, here is Single-cat-apps.php in the /single directory of the child theme. Take particular note of line 29, as this is my directive for displaying custom content.

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @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' );
    
    			if ( generate_has_default_loop() ) {
    				while ( have_posts() ) :
    
    					the_post();
    
    					generate_do_template_part( 'content-cat-apps' ); ///This refers to a file in the root directory of the theme called content-cat-apps.php, in the same place content-single.php is located in the parent theme
    
    				endwhile;
    			}
    
    			/**
    			 * generate_after_main_content hook.
    			 *
    			 * @since 0.1
    			 */
    			do_action( 'generate_after_main_content' );
    			?>
    		</main>
    	</div>
    
    	<?php
    	/**
    	 * generate_after_primary_content_area hook.
    	 *
    	 * @since 2.0
    	 */
    	do_action( 'generate_after_primary_content_area' );
    
    	generate_construct_sidebars();
    
    	get_footer();

    And now here is the above mentioned content-cat-apps.php. At around line 73 is where the modifications start.

    <?php
    /**
     * The template for displaying single posts.
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata( 'article' ); ?>>
    	<div class="inside-article">
    		<?php
    		/**
    		 * generate_before_content hook.
    		 *
    		 * @since 0.1
    		 *
    		 * @hooked generate_featured_page_header_inside_single - 10
    		 */
    		do_action( 'generate_before_content' );
    
    		if ( generate_show_entry_header() ) :
    			?>
    			<header <?php generate_do_attr( 'entry-header' ); ?>>
    				<?php
    				/**
    				 * generate_before_entry_title hook.
    				 *
    				 * @since 0.1
    				 */
    				do_action( 'generate_before_entry_title' );
    
    				if ( generate_show_title() ) {
    					$params = generate_get_the_title_parameters();
    
    					the_title( $params['before'], $params['after'] );
    				}
    
    				/**
    				 * generate_after_entry_title hook.
    				 *
    				 * @since 0.1
    				 *
    				 * @hooked generate_post_meta - 10
    				 */
    				do_action( 'generate_after_entry_title' );
    				?>
    			</header>
    			<?php
    		endif;
    
    		/**
    		 * generate_after_entry_header hook.
    		 *
    		 * @since 0.1
    		 *
    		 * @hooked generate_post_image - 10
    		 */
    		do_action( 'generate_after_entry_header' );
    
    		$itemprop = '';
    
    		if ( 'microdata' === generate_get_schema_type() ) {
    			$itemprop = ' itemprop="text"';
    		}
    		?>
    
    		<div class="entry-content"<?php echo $itemprop; // phpcs:ignore -- No escaping needed. ?>>
    			<?php
    			the_content();
    			echo "<h2>Description</h2>";
    				the_field('appdescription');
    			echo "<h2>Version</h2>";
    				the_field('version');
    			echo "<h2>Free or Paid</h2>";
    				the_field('license');
    			echo "<h2>Accessibility Statement</h2>";
    				the_field('accessibility_statement');
    			echo "<h2>Page Elements</h2>";
    				the_field('page_elements');
    			echo "<h2>Button Labeling</h2>";
    				the_field('button_labeling');
    			echo "<h2>Link to the App</h2>";
    				$link = get_field('link_to_the_app');
    		 echo "<a href='$link'>Get it on Google Play</a>";
    			wp_link_pages(
    				array(
    					'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
    					'after'  => '</div>',
    				)
    			);
    			?>
    		</div>
    
    		<?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' );
    		?>
    	</div>
    </article>
    #2262916
    Jack

    Update: Now using a different method by creating a custom post type with the slug being apps. I provide the single-apps.php and the content-apps.php file. In the single-apps.php file, I have the following line:
    generate_do_template_part( 'content', 'apps' );
    But when I load a post, absolutely nothing is displayed, not even the before and after content.

    #2263083
    David
    Staff
    Customer Support

    If you remove the template redirection function and place the templates in the root of your child theme does it work?

    #2263394
    Jack

    What actually fixed the problem was removing the cpt and falling back to the category system, and then falling back to the get_template_part function rather than the generate_do_template_part function. Being that both carry out essentially the same function and this one just calls for the raw filenames as they are, i.e. content-cat-slug.php. I imagine that’s what was confusing generate_do_template_part as there were three parts to the filename, not two. So all is well now.

    #2263406
    David
    Staff
    Customer Support

    aah ok – glad to hear you got that sorted! And thanks for sharing how

    #2263513
    Jack

    The only slight issue now is that the changes made to functions.php must have broke comments display, but I’m not sure how they could be related. Only modified code in functions.php:

    /*
    * Define a constant path to our single template folder
    */
    define(SINGLE_PATH, STYLESHEETPATH . '/single'); //we use STYLESHEETPATH in place of TEMPLATEPATH when working with a child theme
    /**
    * Filter the single_template with our custom function
    */
    add_filter('single_template', 'my_single_template');
    /**
    * Single template function which will choose our template
    */
    function my_single_template($single) {
    global $wp_query, $post;
    /**
    * Checks for single template by category
    * Check by category slug and ID
    */
    foreach((array)get_the_category() as $cat) :
    if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
    return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
    elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
    return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
    endforeach;
    }

    Even for posts not given a designated category, thus using the standard single.php in the parent theme, the leave a comment box does not display. I know it must have to do with functions.php because this doesn’t happen without the code in place, and switching back to standard GeneratePress immediately brings the comments display back.
    Is this an issue in GeneratePress specifically, or more so a WordPress issue?

    #2264156
    David
    Staff
    Customer Support

    So i assume the comment form returns on the single post if you remove that function ?

    #2264219
    Jack

    Yes, removing the code from functions.php brings back the comments/leave a comment field for the posts. Given all the function does is tell wp where to look for category post single templates, I’m not seeing what’s causing comments to disappear even when the default single template is being used in uncategorized posts.

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