• hi Guys

    I am responsible for the website of our non-profit mini golf club. For this we use the Colormag theme. We would like to insert a couple of banners from our sponsoring members in the report overview. But don’t use a plugin. Because of the thief blockers. For this purpose, only image files should be shown and linked.

    Basically I know how to do it. Simply install a counter and let it count up. After the 3rd, 5th and 7th blog post, the picture should then be inserted.

    I wrote the counter in Index.php directly before the loop, set it to zero and let it count up from the loop at the beginning.

    <Code>
    index.php
    <? php $ display = “0” / * set counter to zero * /?>
    <? php if (have_posts ()):?>

    </ Code>

    If I test the counter by echo command jn the content.php then before each post is 1. That shows me that the counter does not count up.

    <Code>
    content.php

    The counter code
    <? php $ display; / * Counter starts counting * /?>
    <article id = “post – <? php the_ID ();?>” <? php post_class (); ? >>

    </ Code>

    The code for the ad looks like this:

    <Code>
    <? Php
    if ($ display == 1) {echo “Ad 1”; }
    elseif ($ display == 3) {echo “Ad 2”; }
    elseif ($ display == 5) {echo “Ad 3”; }
    else {}
    ?>
    </ Code>

    What am I doing wrong? I assume that I inserted the counter in the wrong place in the theme. But which one is the right one. I do not know how to continue.

    Could someone please give me some help?

    With kind regards,

    • This topic was modified 4 years, 9 months ago by hdj-lange.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • note: you can use the ‘CODE’ button when you post code here in the forum.

    generally, you would increment your counter within index.php;
    after the line with the_post();
    <?php $display++; /* Counter starts counting */ ?>

    however, your counter variable is simply not available in content.php. https://www.php.net/manual/en/language.variables.scope.php
    you could try setting it to ‘global’; i.e. add this at the start of your code in content.php (after the <?php tag):
    global $display;
    for example, put the above where you removed $ display; / * Counter starts counting * /

    alternatively, better, you can use the build in loop post counter (this starts with 0 zero for the first post);

    in content.php use:

    <?php
    global $wp_query;
    if( $wp_query->current_post == 0 ) { echo "Ad 1"; }
    elseif ( $wp_query->current_post == 2 ) { echo "Ad 2"; }
    elseif ( $wp_query->current_post == 4 ) { echo "Ad 3"; }
    else {}
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Integrate a counter into the loop’ is closed to new replies.