0

I am a novice and want to show two tabs under every wordpress post. Under the first tab there would be related posts and under the second tab there would be wp comments. So The default comment area should be disabled. I have also managed to create tabs using this plugin: https://wordpress.org/plugins/tabby-responsive-tabs/ and adding the following code to my child theme's functions.php.

  function add_post_content($content) {
  if(!is_feed() && !is_home()) { 

   $above = '<p></p>';
   $below = '[tabby title="First Tab"]

      code needed to show related posts here...

          [tabby title="Second Tab"]

      <?php comments_template(); ?>

          [tabbyending]';

   $content = "$above $content $below";
  }
 return $content;
}
add_filter('the_content', 'add_post_content');

But is it the correct way? And of course, the comments are not showing by this code. So please provide a working code for the related posts (no thumbnail) and the comments to show up in the respective tabs.

1 Answer 1

0

You need to use do_shortcode() for the shortcode to actually work inside PHP

 function add_post_content($content) {
    if(!is_feed() && !is_home()) { 

    $above = '<p></p>';
    $content_of_first_tab = 'A list of related posts ';


    $args= array(
        'number'  => '5',
        'post_id' => get_the_ID(), // use post_id, not post_ID.
         );
    $comments = get_comments( $args );
    $output = '';
    foreach ( $comments as $comment ) :
        $output .= $comment->comment_author . '<br />' . $comment->comment_content. '<br />';
    endforeach;
    $content_of_second_tab = $output ;
    $below = do_shortcode( '[tabby title="First Tab"]' . $content_of_first_tab . '[tabby title="Second Tab"]'. $content_of_second_tab. '[tabbyending]'  );

    $content = $above.''.$content .''.$below;
    }
return $content;
}
add_filter('the_content', 'add_post_content');


This question is specific to a plugin and should be posted in the appropriate support forum
Do not copy and paste code unless you know what you're doing.
I hope that helps you.

7
  • Thanks. I believe it's a question about my custom development, not the plugin.
    – Jayanta
    Commented Feb 11, 2017 at 16:02
  • Does the code example I provided work for you? Commented Feb 11, 2017 at 16:34
  • Yes it works, but not the way I wanted it. It's showing a list of comments. But I want to show the whole comments template with all the comments, the reply and edit links and the comment submission form.
    – Jayanta
    Commented Feb 11, 2017 at 16:48
  • I've pointed you in the right direction, I can't post the whole source code, this kind of task requires some digging into your theme files, I understand how you want it but it's not possible to make it happen without actually viewing your theme files. You should first learn on how WordPress comments template works and then you can implement it in the example code I provided. Commented Feb 11, 2017 at 16:52
  • Please mark my answer as correct and accept it and then ask another question on comments template file in WordPress. Commented Feb 11, 2017 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.