[Resolved] After content hook (php) not working?

Home Forums Support [Resolved] After content hook (php) not working?

Home Forums Support After content hook (php) not working?

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #56967
    Pete

    I’ve got this code in my After Content GP hook with Execute PHP enabled. But it doesn’t work. However, if I insert the same code into the content-single.php template file (after <?php the_content(); ?>) then it works fine.

    <ul>
    <?php
    $taxonomy = 'category';
    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ', ';
    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) { $term_ids = implode( ',' , $post_terms ); wp_list_categories( 'title_li=&style=list&hierarchical=1&echo=1&taxonomy=' . $taxonomy . '&include=' . $term_ids ); }
    ?>
    </ul>
    #57107
    Tom
    Lead Developer
    Lead Developer

    You’re using the global variable $post, but you haven’t set it first.

    This works inside the content-single.php file because that $post variable has been called.

    Try this code:

    <ul>
    <?php
    global $post;
    $taxonomy = 'category';
    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ', ';
    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) { $term_ids = implode( ',' , $post_terms ); wp_list_categories( 'title_li=&style=list&hierarchical=1&echo=1&taxonomy=' . $taxonomy . '&include=' . $term_ids ); }
    ?>
    </ul>
    #57243
    Pete

    Awesome, thanks Tom. I’m playing with the Code Insert Manager plugin at the moment, which is essentially a hook type plugin, similar to your GP hooks. Unfortunately it seems the plugin isn’t supported any more which is a shame as it has a lot of potential (for me).

    Does this plugin merely use WP’s built in hook system?
    Would there be the ability to create further ‘hooks’ at different places within GP?

    Here’s some other similar plugins

    #57300
    Tom
    Lead Developer
    Lead Developer

    What other areas do you think would be good for hooks?

    #57319
    Pete

    Basically every area within/between each template element… like this in the content-single.php where xxxxx marks the spot.

    <?php
    /**
     * @package Generate
     */
    ?>
    xxxxx
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> itemprop="blogPost" itemtype="http://schema.org/BlogPosting" itemscope="itemscope">
    	xxxxx
    	<div class="inside-article">
    	xxxxx
    		<?php do_action( 'generate_before_content'); ?>
    		<header class="entry-header">
    		xxxxx
    			<h1 class="entry-title" itemprop="headline"><?php the_title(); ?></h1>
    xxxxx
    			<div class="entry-meta">
    				<?php generate_posted_on(); ?>
    			</div><!-- .entry-meta -->
    			xxxxx
    		</header><!-- .entry-header -->
    		<?php do_action( 'generate_after_entry_header'); ?>
    		<div class="entry-content" itemprop="text">
    		xxxxx
    			<?php the_content(); ?>
    			xxxxx
    						
    			<?php
    				wp_link_pages( array(
    					'before' => '<div class="page-links">' . __( 'Pages:', 'generate' ),
    					'after'  => '</div>',
    				) );
    			?>
    			xxxxx
    		</div><!-- .entry-content -->
    xxxxx
    		<footer class="entry-meta">
    		xxxxx
    			<?php
    				/* translators: used between list items, there is a space after the comma */
    				$category_list = get_the_category_list( __( ', ', 'generate' ) );
    
    				/* translators: used between list items, there is a space after the comma */
    				$tag_list = get_the_tag_list( '', __( ', ', 'generate' ) );
    
    				$meta_text = '';
    
    				// But this blog has loads of categories so we should probably display them here
    				if ( '' != $tag_list && '' != $category_list ) {
    					$meta_text = '<span class="cat-links">%1$s</span><span class="tags-links">%2$s</span>';
    				} elseif ( '' == $tag_list && '' != $category_list ) {
    					$meta_text = '<span class="cat-links">%1$s</span>';
    				}
    				
    				printf(
    					$meta_text,
    					$category_list,
    					$tag_list
    				);
    			?>
    xxxxx			
    			<?php generate_content_nav( 'nav-below' ); ?>
    xxxxx
    			<?php edit_post_link( __( 'Edit', 'generate' ), '<span class="edit-link">', '</span>' ); ?>
    			xxxxx
    		</footer><!-- .entry-meta -->
    		<?php do_action( 'generate_after_content'); ?>
    	</div><!-- .inside-article -->
    	xxxxx
    </article><!-- #post-## -->
    xxxxx

    And here with single.php

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package Generate
     */
    
    get_header(); ?>
    xxxxx
    	<div id="primary" <?php generate_content_class();?>>
    	xxxxx
    		<main id="main" <?php generate_main_class(); ?> itemtype="http://schema.org/Blog" itemscope="itemscope" itemprop="mainContentOfPage" role="main">
    
    		<?php do_action('generate_before_main_content'); ?>
    		<?php while ( have_posts() ) : the_post(); ?>
    		xxxxx
    
    			<?php get_template_part( 'content', 'single' ); ?>
    xxxxx
    			<?php
    				// If comments are open or we have at least one comment, load up the comment template
    				if ( comments_open() || '0' != get_comments_number() ) : ?>
    				xxxxx
    					<div class="comments-area">
    					xxxxx
    						<?php comments_template(); ?>
    						xxxxx
    					</div>
    					xxxxx
    			<?php endif; ?>
    xxxxx
    		<?php endwhile; // end of the loop. ?>
    		<?php do_action('generate_after_main_content'); ?>
    		</main><!-- #main -->
    		xxxxx
    	</div><!-- #primary -->
    	xxxxx
    
    <?php 
    do_action('generate_sidebars');
    get_footer()
    #57320
    Pete

    I understand there’s possible duplication, but it’s easier than trying to delve deeper and deeper into all the includes to find the spot/hook.

    #57331
    Pete

    Also why i’m here πŸ™‚ …

    Back in the old days themes had the whole range of template hierarchy files such as
    home.php
    index.php
    single.php
    archive.php
    category.php – if we wanted a specific file for a specific category we just copy and pasted into a new file called category-xyz.php
    tag.php
    etc

    these days the traditional concept of hierarchy is hidden away in the templates files themselves.

    It made it easy to edit from a newby user point of view as there was little to no ‘includes’ to try to hunt down only to find those includes had other includes. Also there was few ‘conditionals’. The hierarchy system dealt with that.

    these days the complexity of theme files makes it hard for the newby user to decypher what is going on in one theme file that does the job of so many other files.

    This post is a classic case (in my eyes) of theme developers finally coming to the realization that they haven’t considered the end users as much as they should have but maybe have got the coder bug and stormed ahead with making themes that out paced the ability of the user to grasp?

    #57471
    Tom
    Lead Developer
    Lead Developer

    That’s definitely too many hooks – I can’t see too many reasons why all of those would need to be included. I think very few users would make use of them, and they would in time bloat the theme’s code.

    If you’re wanting to make heavy modifications to the files, then a child theme is definitely the way to go. I’ve built quite a few very custom websites using GP where I’ve changed a lot of those files almost completely, which is exactly why child themes exist.

    Hooks are great, but definitely shouldn’t be over-used.

    As for the theme files – the way GP is set up is actually more simple, as it deals with all of those files in one file (archive.php).

    However, you can still create all of the theme files you listed above in your child theme – you have complete control.

    The only reason I didn’t with GP is because all of those files have very similar – if not identical code structures. It would be a bunch of duplication in a simple theme like GP.

    home.php – no need as there’s not a specific homepage template in GP.
    index.php – included in the theme.
    single.php – included in the theme
    archive.php – included in the theme
    category.php – handled by archive.php as the code structure is identical
    tag.php – same as above

    The idea behind GP is that it’s super lightweight and super friendly to modification – you can go to town creating category-xyz.php files and tag.php file and modify as much as you want – but some users just want a simple, fast theme.

    I certainly hope I don’t have the coder bug – I’ve built a career on building themes and plugins easy for the end user πŸ™‚

    Thanks for the discussion!

    #225944
    Jessica

    home.php – no need as there’s not a specific homepage template in GP.
    index.php – included in the theme.
    single.php – included in the theme
    archive.php – included in the theme
    category.php – handled by archive.php as the code structure is identical
    tag.php – same as above

    I am creating a custom fields group I would normally add to a custom page template – which is working fine. However, the hoomepage (frontpage or home) isn’t working.

    I see based on what you wrote above that it won’t… what should my homepage template be in order to get the custom code to show up on the page (editor and front end)?

    Thanks!

    #225965
    Tom
    Lead Developer
    Lead Developer

    I’m not too sure what you mean?

    If you need a template for your home page, it should be named front-page.php.

    You can find all of the templates available in your child theme here: https://developer.wordpress.org/themes/basics/template-hierarchy/

    That will work whether you’re using GP or not, it’s standard WP behavior πŸ™‚

    #225966
    Jessica

    Thanks…you probably didn’t understand why I meant because I’m an idiot. Haha. I had everything done right (including the page temple you named) but it would have helped if I turned the custom fields to “on/active” instead of disabled. This, nothing was showing.

    So I have it working great now except, I am trying to add a background image to the custom doc set in template (home-hero) but it’s wrapped with the page max-width so the background is stopping at the edge of the page content area, unlike if I were use a single section.

    Trying to figure out how to set the wrap for that div so it’s full width (for the background image only that’s being called in my csa from my assets folder). Since it’s a custom page template, and I’m using custom fields, I can’t use the page header in this case since it won’t allow me to do the layering I want without the client seeing in in the page editor.

    I have the dog contents set to flexi but the background image still stops at the max page with of 1040px. (I hope that Maltese sense).

    #225976
    Tom
    Lead Developer
    Lead Developer

    You would need to:

    a) Place the hero area outside of the #page container. You could do this using the generate_after_header hook coupled with an if condition matching your page template.

    b) Make the #page container full width only for your page template, and then wrap the content inside of it that needs to be contained in the grid-container class.

    #226245
    Jessica

    Thanks! I’ll try this and let you know.
    Appreciate it.

    #226265
    Tom
    Lead Developer
    Lead Developer

    Awesome. Let me know how it goes πŸ™‚

    #323734
    Jessica

    I’m not able to get front-page.php working with my child theme; it still appears to be pulling the default page template. https://toothwisdom.wpengine.com/

    I’ve wiped everything from the front-page.php template except the header & footer; the homepage is still pulling in sections content included in the content editor.

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