divs in the feed?!

Hi all, I have a wierd issue with some divs that are included in the rss feed. As a result the feed does not validate.
I have the following in functions.php to display ads between posts:

/**
 * Insert banners between posts /tsl
 */
function ad_after_post_1( $post ) {
        global $wp_query;

        // Check if we're in the main loop
        if ( $wp_query->post != $post )
                return;

        // Check if we're at the right position
        if ( 1 != $wp_query->current_post )
                return;

        // Display the banner
                echo '<div id="inlinead">
                <script type="text/javascript"><!--// <![CDATA[
                        OA_show(1163);
                // ]]> --></script>
                </div>';
}
add_action( 'the_post', 'ad_after_post_1' );

function ad_after_post_2( $post ) {
        global $wp_query;

        // Check if we're in the main loop
        if ( $wp_query->post != $post )
                return;

        // Check if we're at the right position
        if ( 2 != $wp_query->current_post )
                return;

        // Display the banner
                echo '<div id="inlinead">
                <script type="text/javascript"><!--// <![CDATA[
                        OA_show(1164);
                // ]]> --></script>
                </div>';
}
add_action( 'the_post', 'ad_after_post_2' );

function ad_after_post_3( $post ) {
        global $wp_query;

        // Check if we're in the main loop
        if ( $wp_query->post != $post )
                return;

        // Check if we're at the right position
        if ( 3 != $wp_query->current_post )
                return;

        // Display the banner
                echo '<div id="inlinead">
                <script type="text/javascript"><!--// <![CDATA[
                        OA_show(1165);
                // ]]> --></script>
                </div>';
}
add_action( 'the_post', 'ad_after_post_3' );

Error on feedvalidator.org is:

line 39, column 1: Undefined channel element: div (3 occurrences) [help]

<div id="inlinead">
^

Removing the code from functions.php makes the feed validate...so I think it's not any plugin issue

Any help is much appreciated

Thanks

  • ThePath
    • The Bug Hunter

    Hey Tomas, thanks for being a member and for the post.

    So do these function only put the ads in the RSS feed? Or are they utlised somewhere else?

    My immediate thought is take out the divs and just leave the JS code and then see if it works and still displays the ads.

    Ive never added ads to my feeds this way as I just use feedburner and Google ads in there.

    Let me know how you get on though.

    Cheers

  • Heydar
    • Flash Drive

    Hi, thanks for your reply.
    The function is only meant to place ads between the regular posts views, not in the RSS feed.
    From what I understand a div tag is not supposed to end up in an feed, right?

    So, basically I only want this function to execute on regular views and not in any feeds view.
    Possible? I'm not sure why they appear in the feed, no other html elements do?!

  • ThePath
    • The Bug Hunter

    I'm not sure why they appear in the feed, no other html elements do?!

    Ineed. But I would imagine that the posts are called into the rss feed using the same id's and in a simlar way. You are telling WP to put adds after posts and WP is saying well these are posts in the RSS feed.

    You could add:

    if (is_page('your-page-slug')) {
    
    Your code above
    
    }

    Or maybe try:

    if (in_the_loop()) {
    
    Your code above
    
    }

    Or you could say if NOT feed:

    if (!is_feed()) {
    
    Your code above
    
    }

    That should work. Let me know dude

  • ThePath
    • The Bug Hunter

    Nah try it in your functions.php page where you have the ad code, so it would look like:

    if (!is_feed()) {
    
    /**
     * Insert banners between posts /tsl
     */
    function ad_after_post_1( $post ) {
            global $wp_query;
    
            // Check if we're in the main loop
            if ( $wp_query->post != $post )
                    return;
    
            // Check if we're at the right position
            if ( 1 != $wp_query->current_post )
                    return;
    
            // Display the banner
                    echo '<div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(1163);
                    // ]]> --></script>
                    </div>';
    }
    add_action( 'the_post', 'ad_after_post_1' );
    
    function ad_after_post_2( $post ) {
            global $wp_query;
    
            // Check if we're in the main loop
            if ( $wp_query->post != $post )
                    return;
    
            // Check if we're at the right position
            if ( 2 != $wp_query->current_post )
                    return;
    
            // Display the banner
                    echo '<div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(1164);
                    // ]]> --></script>
                    </div>';
    }
    add_action( 'the_post', 'ad_after_post_2' );
    
    function ad_after_post_3( $post ) {
            global $wp_query;
    
            // Check if we're in the main loop
            if ( $wp_query->post != $post )
                    return;
    
            // Check if we're at the right position
            if ( 3 != $wp_query->current_post )
                    return;
    
            // Display the banner
                    echo '<div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(1165);
                    // ]]> --></script>
                    </div>';
    }
    add_action( 'the_post', 'ad_after_post_3' );
    }
  • Heydar
    • Flash Drive

    Super, have implemented the change but the code from functions.php like:

    <div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(1165);
                    // ]]> --></script>
                    </div>

    still shows upp in the source of the feeds. Cache been dumped but same issue

  • ThePath
    • The Bug Hunter

    Hummm ok well Ive never used is_feed before so perhaps its not the best thing to try at the moment.

    Are your posts displayed on the homepage?

    If so could you try replacing:

    if (!is_feed()) {

    with:

    if ( is_home() ) {

    If you use a different page then find out its ID or page slug and use:

    if ( is_page(id) ) { or

    if ( is_page('page-slug') ) {

  • ThePath
    • The Bug Hunter

    Yarr which is why I thought the best one would be !is_feed

    Nah you cant use !is_single because I dont think the feed is a single page either....could be classed as an archive page too.

    Humm hang on give me a sec do do a quick search of the codex

  • aecnu
    • WP Unicorn

    Greetings Tomas and ThePath,

    Looks like you two are hammering out the code to get this right so I will keep my two cents out since you folks appear to be on a path that I have not yet had the opportunity to tread.

    I shall hang out watching this thread to see if it may be necessary to get a developer in here to look at the coding - though it looks like you two will get it hammered out.

    Thank you both for being WPMU Dev Members!

    Cheers, Joe

  • Heydar
    • Flash Drive

    Yaay, that did it :slight_smile:

    /**
     * Insert banners between posts /tsl
     */
    
    function ad_after_post_1( $post ) {
            global $wp_query;
    
    if (!is_feed()) {
    
            // Check if we're in the main loop
            if ( $wp_query->post != $post )
                    return;
    
            // Check if we're at the right position
            if ( 1 != $wp_query->current_post )
                    return;
    
            // Display the banner
                    echo '<div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(888);
                    // ]]> --></script>
                    </div>';
    }
    }
    add_action( 'the_post', 'ad_after_post_1' );
    add_action( 'the_post', 'ad_after_post_1' );
    
    function ad_after_post_2( $post ) {
            global $wp_query;
    
    if (!is_feed()) {
    
            // Check if we're in the main loop
            if ( $wp_query->post != $post )
                    return;
    
            // Check if we're at the right position
            if ( 2 != $wp_query->current_post )
                    return;
    
            // Display the banner
                    echo '<div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(889);
                    // ]]> --></script>
                    </div>';
    }
    }
    add_action( 'the_post', 'ad_after_post_2' );
    
    function ad_after_post_3( $post ) {
            global $wp_query;
    
    if (!is_feed()) {
    
            // Check if we're in the main loop
            if ( $wp_query->post != $post )
                    return;
    
            // Check if we're at the right position
            if ( 3 != $wp_query->current_post )
                    return;
    
            // Display the banner
                    echo '<div id="inlinead">
                    <script type="text/javascript"><!--// <![CDATA[
                            OA_show(890);
                    // ]]> --></script>
                    </div>';
    }
    }
    add_action( 'the_post', 'ad_after_post_3' );

    So moving it within the 3 function ad_after_post_X made the feed skip the divs and now it's validating.
    Have no clue why though hehe...

  • ThePath
    • The Bug Hunter

    Mmmmmm must have something to do with add_action that I dont know of.

    Thats weird...and annoying.

    So your code now hides the divs from the feed but the add_actions are still called and function is visible yet when it goes through it (if its the feed) its blank.

    Mmmmmm not keen on this to be honest as it is still calculating and thus using server resources etc. But its probably minimal and hey whatever works right! Just wish I understood why that works and not enclosing the whole thing in one IF

    A lsightly shallow victory but a victory none the less eh! hehe

    Ahh well glad its sorted and I have marked this as solved. Well done.

  • ThePath
    • The Bug Hunter

    Maybe we would have to use something like: remove_all_actions() to stop them running within the !is_feed

    See what youve done, lol. Unfortunately time is always against us I dont have the time to investigate how or where to use this.

    Ah well :wink:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.