Forums

Determine if a post is the first post on the homepage (5 posts)

  1. Squeebee
    Member
    Posted 7 years ago #

    Hi All;

    I am using an adsense plugin to add adsense ads to each post on my site, and I have hacked the plugin so far to just display ads when on a single post page, but I want to modify it again to show an adsense ad for the first post on the homepage and ONLY the first page. Can anyone tell me how I would check if a given post is the first post?

    Here is my adsense.php hack so far:

    if( is_single() )
    {
    return str_replace( $tag, $adsense_code, $data );
    }
    elseif ( is_home )
    {
    if ( FIRST_POST?? )
    {
    return str_replace( $tag, $adsense_code, $data );
    }
    else
    }
    return str_replace( $tag, '', $data );
    {
    }
    else
    {
    return str_replace( $tag, '', $data );
    }

    I just need a value for FIRST_POST?? for it to work.

    Thanks,
    Mike

  2. Anonymous
    Unregistered
    Posted 7 years ago #

    Try this:

    1. Add <?php $postcount = 0; ?> before the while/foreach
    loop.
    2. Add <?php $postcount++; ?> right after the loop starts.
    3. replace FIRST_POST?? with $postcount == 1.

  3. Kafkaesqui
    Moderator
    Posted 7 years ago #

  4. Squeebee
    Member
    Posted 7 years ago #

    Thanks for the replies. In the end these approaches did not produce data I could detect in my plugin, so I added update_option('FirstPost', TRUE); to the main template before the loop, then in the plugin used get_option() to pull the value and update it to false if it was true.

  5. Ryan Boren
    WordPress Dev
    Posted 7 years ago #

    if ($wp_query->current_post == 0)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.