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');The page I need help with: [log in to see the link]
The topic ‘Code snippet executes twice’ is closed to new replies.