• Resolved Gregosz

    (@gregosz)


    I have encountered the following problem:
    Easy Table of Contents does not cooperate with my Custom Shortcode (I have created).

    In other words, I have two options:
    a) There is a table of contents on the page/post/CPT, but only if I do not add there my shortcode.
    b) If I add a shortcode, it works fine, but then the table of contents disappears.

    I don’t know what additional information I should add to explain it better.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Steven

    (@shazahm1hotmailcom)

    Please share a link to your Github repo for your custom shortcodes. It is impossible for me to know why your shortcodes cause ezTOC not to display without being able to their code.

    Thread Starter Gregosz

    (@gregosz)

    Hi!

    The aim of my shortcode is to display all ski images of accessories.
    Images are taken from simple Custom Post Type (“akcesoria”): only post title and featured image.

    I have prepared two pages to explain the issue.
    In both of them I have selected “Insert table of contents” and headings (h3, h4) checkboxes.

    First page:
    https://skipen.lemonaro.pl/service/services-with-table-of-contents/

    Page content:

    <h3>Sapien sed neque</h3>
    <p>Lorem ipsum dolor sit amet sapien sed neque.</p>
    <h4>Sed enim consectetue</h4>
    <h3>Fusce quam eu</h3> 
    <p>Sagittis ultricies. Donec odio a purus. Fusce quam porta nisl.</p>

    Second page:
    https://skipen.lemonaro.pl/service/services-without-table-of-contents/

    Page content:

    <h3>Sapien sed neque</h3>
    <p>Lorem ipsum dolor sit amet sapien sed neque.</p>
    <h4>Sed enim consectetue</h4>
    <h3>Fusce quam eu</h3> 
    <p>Sagittis ultricies. Donec odio a purus. Fusce quam porta nisl.</p>
    [display-accessories]

    First page displays Table of Contents.
    Second not, but displays table of images.

    I do not think it is important but just in case there is the snippet of my shortcode:

    function display_accessories_function() {
      $args = array('post_type'   => 'akcesoria',
                    'post_status' => 'publish', 
                    'posts_per_page'=>-1
                    );      
      $loop = new WP_Query( $args ); 
      $unpaired_loop = true;
      $number_of_accessories = 0;
      
      if ( $loop->have_posts() ) : 
        while ( $loop->have_posts() ) : $loop->the_post(); 
          $number_of_accessories = $number_of_accessories + 1;
          $akcesorium = get_the_title();
          $akcesorium_featured_img_url = get_the_post_thumbnail_url();      
          if($unpaired_loop){
            $col_1 = 
            '[su_row class=""] 
               [su_column size="1/2" center="no" class=""]    
                 <h5>' . $akcesorium .  '</h5>
                 <img src="' . $akcesorium_featured_img_url . '" >                     
               [/su_column]';
            $unpaired_loop = false;
            $last_akcesorium = $akcesorium;
            $last_akcesorium_featured_img_url = $akcesorium_featured_img_url;
            } 
          else {
            $col_2 = 
            '  [su_column size="1/2" center="no" class=""]
                 <h5>' . $akcesorium . '</h5>
                 <img src="' . $akcesorium_featured_img_url . '" >
               [/su_column]
             [/su_row]';      
            $unpaired_loop = true;
          }
            
          if($unpaired_loop) {
            $accessories_table = $accessories_table . $col_1 . $col_2;
          }  
        endwhile; 
      endif;
      if ( $number_of_accessories & 1 ) {
        $accessories_table = $accessories_table . 
        '[su_row class=""]               
           [su_column size="1/2" center="no" class="kol_1"]    
             <h5>' . $last_akcesorium . '</h5>
             <img src="' . $last_akcesorium_featured_img_url . '" >                     
           [/su_column]
        [/su_row]';
      }
        
      return do_shortcode($accessories_table);
    }
    
    add_shortcode('display-accessories', 'display_accessories_function');
    Steven

    (@shazahm1hotmailcom)

    Nothing jumps out at me, well, other than your custom loop is incomplete and will likely cause you other troubles.

    Thread Starter Gregosz

    (@gregosz)

    HI!
    You are right!
    At the moment I do not know what exactly but somothing is wrong with my loop.

    I commented it and allow to display just text and it works!
    I will let you know when I find what is going on.
    thanks a lot!

    It is my first loop in php! So I have no experience how to properly code it ??

    Steven

    (@shazahm1hotmailcom)

    You’re missing the wp_reset_postdata() call. See the exampe on the doc page:

    Thread Starter Gregosz

    (@gregosz)

    Hi!

    It works now! Thank you very much!!!

    In fact, I had the wp_reset_postdata () before, but after the “return …” line, not before it.

    Grzegorz

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Easy Table of Contents does not cooperate with Custom Shortcode’ is closed to new replies.