Badan
Badan

Reputation: 266

Add javascript after n-th paragraph of a specific post in Question2Answer

I am struggling with this from quite some time but cannot find any soltion. I have a really long blog-type post with lots of paragraphs that I have added as a question in my Question2Answer website. I would like to put adsense code after let's say the 3rd paragraph, but there is no way to do it without modifying the php code. I read the Q2A documentation and from what i understood, i need to change the qa_view function. This is the original code for displaying question content in qa-theme.php:

public function q_view_content($q_view)
{
    $content = isset($q_view['content']) ? $q_view['content'] : '';

    $this->output('<div class="qa-q-view-content">');
    $this->output_raw($content);
    $this->output('</div>');
}

However, I have no idea how to add the javascipt code here and to refer it to specific post id. I was thinking about adding wrapper div after a specific paragraph, but i do not know how to accomplish this, since I am very new with php. Any help will be much appreciated.

Upvotes: 0

Views: 275

Answers (1)

user10089632
user10089632

Reputation: 5570

I do not recommend doing this because it's confusing the browsing experience as you interfere with the reading content. I suggest you either

  • Follow StackOverflow style and put an AD banner at the top or at the side of your page
  • Or follow Quora style where you create a 'see more' button then put your AD so there is a clear separation between the content and ads.

If you insist query your third

child of the question using JS or JQuery, then use insertAfter, for instance:

?>
var ADS = document.createElement( 'div' );
ADS.innerHtml = ' ... the Html content of your ADS banner' ;
var question = document.getElementById( 'question-id' );
var paragraphe = question.querySelectorAll('p')[2];  // third 
question.insertBefore( ADS , question.children(2).sibling );
// sibling is used to insert rather after the paragraph
<?php

This code is to be tested and adapted to your use case then appended to the appropriate php file (may be at the end of qa-include/qa-base.php) but take the necessary precautions that it will not interfere with the overall layout.

Upvotes: 1

Related Questions