{"id":5401,"date":"2020-09-27T13:08:28","date_gmt":"2020-09-27T11:08:28","guid":{"rendered":"https:\/\/whitepapers.grafik-und-redaktion.de\/?p=5401"},"modified":"2020-09-27T13:21:46","modified_gmt":"2020-09-27T11:21:46","slug":"hook-into-the-loop-via-a-plugin-and-output-something-after-every-x-post-wordpress-development-stack-exchange","status":"publish","type":"post","link":"https:\/\/whitepapers.grafik-und-redaktion.de\/?p=5401","title":{"rendered":"Hook into the loop via a plugin, and output something after every X post? &#8211; WordPress Development Stack Exchange"},"content":{"rendered":"<blockquote><p>We can also inject the ads via the the_content filter, in the main loop:<\/p><\/blockquote>\n<pre class=\"lang:default decode:true\">add_action( 'loop_start', 'wpse_141253_loop_start' );\r\nfunction wpse_141253_loop_start( $query )\r\n{\r\nif( $query-&gt;is_main_query() )\r\n{\r\nadd_filter( 'the_content', 'wpse_141253_the_content' );\r\nadd_action( 'loop_end', 'wpse_141253_loop_end' );\r\n}\r\n}\r\n\r\nfunction wpse_141253_the_content( $content )\r\n{\r\nstatic $nr = 0;\r\n\r\nif( 0 === ++$nr % 4 )\r\n$content .= '\r\n&lt;div&gt;------- MY AD HERE -------&lt;\/div&gt;\r\n';\r\n\r\nreturn $content;\r\n}\r\n\r\nfunction wpse_141253_loop_end()\r\n{\r\nremove_action( 'the_post', 'wpse_141253_the_content' );\r\n}<\/pre>\n<p>Quelle: <em><a href=\"https:\/\/wordpress.stackexchange.com\/questions\/141253\/hook-into-the-loop-via-a-plugin-and-output-something-after-every-x-post\">Hook into the loop via a plugin, and output something after every X post? &#8211; WordPress Development Stack Exchange<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can also inject the ads via the the_content filter, in the main loop: add_action( &#8218;loop_start&#8216;, &#8218;wpse_141253_loop_start&#8216; ); function wpse_141253_loop_start( $query ) { if( $query-&gt;is_main_query() ) { add_filter( &#8218;the_content&#8216;, &#8218;wpse_141253_the_content&#8216; ); add_action( &#8218;loop_end&#8216;, &#8218;wpse_141253_loop_end&#8216; ); } } function wpse_141253_the_content( $content &hellip; <a href=\"https:\/\/whitepapers.grafik-und-redaktion.de\/?p=5401\">Weiterlesen &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=\/wp\/v2\/posts\/5401"}],"collection":[{"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5401"}],"version-history":[{"count":4,"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=\/wp\/v2\/posts\/5401\/revisions"}],"predecessor-version":[{"id":5406,"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=\/wp\/v2\/posts\/5401\/revisions\/5406"}],"wp:attachment":[{"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/whitepapers.grafik-und-redaktion.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}