I really need help with this code, because can’t figure out why isn’t working and even does not have syntax error, content is not rendered on frontend, at all.
I created quiz blocks with questions and answers. Now I want to show mixed types of ads after certain number of questions. Sample code below:
add_filter('render_block_buzzeditor/personality-question', 'add_question_ad_place', 10, 2); add_filter('render_block_buzzeditor/trivia-question', 'add_question_ad_place', 10, 2); global $question_count; $question_count = 1; function add_question_ad_place($block_content, $block) { // only after the 2nd question if($question_count === 2) { $block_content .= 'ad code after 2'; } if($question_count === 4) { $block_content .= 'ad code after 4'; } if($question_count === 6) { $block_content .= 'ad code after 6'; } $question_count++; return $block_content; }
Idea is to count question blocks, and after certain number of blocks (2,4,6 in this case) place ads code and have ad after 2nd, 4th and 6th question.
No PHP errors, but just does not render content on frontend. What am I missing here? Thanx, guys.
Advertisement
Answer
You are just incrementing $question_count
in local scope, to fix add
global $question_count;
As the first line of your function add_question_ad_place
Like so
add_filter('render_block_buzzeditor/personality-question', 'add_question_ad_place', 10, 2); add_filter('render_block_buzzeditor/trivia-question', 'add_question_ad_place', 10, 2); $question_count = 1; function add_question_ad_place($block_content, $block) { global $question_count; // only after the 2nd question if($question_count === 2) { $block_content .= 'ad code after 2'; } if($question_count === 4) { $block_content .= 'ad code after 4'; } if($question_count === 6) { $block_content .= 'ad code after 6'; } $question_count++; return $block_content; }