Title: Code snippet executes twice
Last modified: May 28, 2022

---

# Code snippet executes twice

 *  Resolved [kepanodk](https://wordpress.org/support/users/kepanodk/)
 * (@kepanodk)
 * [4 years ago](https://wordpress.org/support/topic/code-snippet-executes-twice/)
 * I’m trying to copy a centextual navigation menu from the theme “Graphene”, that
   is intended to be in a sidebar. I, however would like to have on the page, so
   I don’t need to have sidebars activated.
 * I have changed the name of the function and classes, as to try remove any connection
   to the theme-function.
 * It works as intended, but it is executed twice, as you can see on the added page.
   
   I have no experience in PHP, so it most likely is an error of mine. But I hope
   someone is able to help resolving the issue.
 * Edit: I have placed just one shortcode with the code [JDR_childpages] right under
   the page header
 * my snippet:
 *     ```
       if ( ! function_exists( 'ToC' ) ) :
       function ToC(){
   
       	if ( ! is_singular() ) return;
   
       	$current = get_the_ID();
   
       	$ancestors = get_ancestors( $current, 'page' );
   
       	if ( $ancestors ) $parent = $ancestors[0];
       	else $parent = $current;
       	$title = "In this section:";
   
   
   
       	$args1 = array(
   
       		'post_type'			=> array( 'page' ),
   
       		'posts_per_page'	=> -1,
   
       		'post_parent'		=> $parent,
   
       		'orderby'			=> 'menu_order title',
   
       		'order'				=> 'ASC'
   
       	);
   
       	$children = new WP_Query( apply_filters( 'JDR_page_navigation_args', $args1 ) );
   
   
   
       	if ( $children->have_posts() ) :
   
       	?>
   
               <div class="JDR contextual-nav">
   
                   <h3 class="JDR-section-title-sm"><?php echo $title; ?></h3>
   
                   <div class="list-group page-navigation">
   
                   	<a class="list-group-item parent <?php if ( $parent == $current ) echo 'active'; ?>" href="<?php echo esc_url( get_permalink( $parent ) ); ?>"><?php echo get_the_title( $parent ); ?></a>
   
                       <?php while ( $children->have_posts() ) : $children->the_post(); ?>
   
                       <a class="list-group-item <?php if ( get_the_ID() == $current ) echo 'active'; ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
   
                       <?php endwhile; ?>
   
                   </div>
   
               </div>
   
           <?php 
   
       	endif; wp_reset_postdata(); 
   
       }
   
       endif;
       add_shortcode('JDR_childpages', 'ToC');
       ```
   
    -  This topic was modified 4 years ago by [kepanodk](https://wordpress.org/support/users/kepanodk/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcode-snippet-executes-twice%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [kepanodk](https://wordpress.org/support/users/kepanodk/)
 * (@kepanodk)
 * [4 years ago](https://wordpress.org/support/topic/code-snippet-executes-twice/#post-15692666)
 * I found the cause of the problem. I have another plugin installed – “Easy Table
   of Contents”. When this is activated, my snippets are shown twice, otherwise 
   the shorcode are shown as intended.
 * I tested with a basic shortcode to see if the shortcode were to blame:
 *     ```
       function jdr1(){
       	?>
       <p>Test</p>
       <?php
       }
   
       add_shortcode('test_SC', 'jdr1');
       ```
   
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [4 years ago](https://wordpress.org/support/topic/code-snippet-executes-twice/#post-15693689)
 * You need to `return` content from a shortcode instead of directly displaying 
   it.
 *  Thread Starter [kepanodk](https://wordpress.org/support/users/kepanodk/)
 * (@kepanodk)
 * [4 years ago](https://wordpress.org/support/topic/code-snippet-executes-twice/#post-15700226)
 * Ahh, you are quite right..
 * Thank you 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Code snippet executes twice’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [kepanodk](https://wordpress.org/support/users/kepanodk/)
 * Last activity: [4 years ago](https://wordpress.org/support/topic/code-snippet-executes-twice/#post-15700226)
 * Status: resolved