Site logo

[Resolved] Hook Elements, php and html

Home Forums Support [Resolved] Hook Elements, php and html

Home Forums Support Hook Elements, php and html

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2438515
    igniteperth

    Hi Folks, I am trying to generate a custom nav for custom post types that displays after content (generate_after_content) using html and php in a Hook Element.

    The php echo statement for next_post_link will not display inside the html div it is written in.

    Can you please clarify how Hook Elements render the code inside, or help me understand what I am doing wrong?

    <div class="pagination-wrapper">
    
    <!-- PREVIOUS POST -->
            <div class="prev-post">
    	     <?php echo $previous_post_link->ID;?>
    	</div>         
      <!-- end previous -->
      <!-- NEXT POST -->
    	<div class="next-post">
    	    <?php echo $next_post_link->ID;?>
    	</div>
      <!-- end next -->
    	
    </div>
    #2438837
    David
    Staff
    Customer Support

    Hi there,

    The Hooks that GP provides in the Elements module don’t do anything with the content you add inside them. They’re just like a placeholder that you can inject code/content into.

    I assume the function that is generating then $previous_post_link is already returning its results to the content hence it shows outside of your HTML.

    Can i see all of the code ?

    #2440093
    igniteperth

    This is the Hook Element

    <?php   if (isset($_POST['data'])){
                $post_id = $_POST['data'];
            }else{
                $post_id = "";
            }
            $wp_query->is_single = true;
    
            global $post;
            $post = get_post($post_id);
    				
            $next_post_link = next_post_link('%link');
            $previous_post_link = previous_post_link('%link');
    
    	$prev_post = get_previous_post();
    	$next_post = get_next_post();
    ?>
    
    <div class="pagination-wrapper">
    <!-- PREVIOUS POST -->
            <div class="prev-post">
    	 <?php echo $previous_post_link->ID;?>
      	</div>         
      <!-- end previous -->
      <!-- NEXT POST -->
    	<div class="next-post">
    	  <?php echo $next_post_link->ID;?>
    	</div>
      <!-- end next -->
    	
    </div>

    and this is what it creates:

    <div class="inside-article">
    
      <div class="entry-content"></div>
    
    //HOOK IS FROM HERE (generate_after_content) 
      <a href="https://dev.fabmarion.com.au/projects/nola-avenue-scarborough/" rel="next">Nola Avenue, Scarborough</a>
      <a href="https://dev.fabmarion.com.au/projects/kirwin-street/" rel="prev">Kirwin Street</a>
      <div class="pagination-wrapper">
    
        <!-- PREVIOUS POST -->
        <div class="prev-post"></div>         
        <!-- end previous -->
    
        <!-- NEXT POST -->
        <div class="next-post"></div>
        <!-- end next -->
    
      </div>
    //TO HERE
    
    </div>

    I am trying to understand why the php renders outside of the html it is placed in. Any pointers appreciated.

    #2440605
    David
    Staff
    Customer Support

    The links are being output here:

    
    $next_post_link = next_post_link('%link');
    $previous_post_link = previous_post_link('%link');

    Both the next_post_link and previous_post_link display the links.
    See the codex for more info:

    https://developer.wordpress.org/reference/functions/next_post_link/

    So you could just do this:

    
    <div class="pagination-wrapper">
      <!-- PREVIOUS POST -->
      <div class="prev-post">
         <?php previous_post_link(); ?> 	
      </div>         
      <!-- end previous -->
      <!-- NEXT POST -->
      <div class="next-post">
        <?php next_post_link(); ?>
      </div>
      <!-- end next -->
    </div>
    #2441384
    igniteperth

    I am such a Muppet! I mistook next_post_link for get_…

    TY!

    #2441391
    David
    Staff
    Customer Support

    🙂 Glad to be of help

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