• Resolved kepanodk

    (@kepanodk)


    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.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter kepanodk

    (@kepanodk)

    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

    (@bungeshea)

    You need to return content from a shortcode instead of directly displaying it.

    Thread Starter kepanodk

    (@kepanodk)

    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.