0

I want to put a widget area into the content of the first post. My problem though is that is can't figure out how to get only the first post in my function. Here is my function:

    function pietergoosen_insert_content_filter( $content ) {
    ob_start();
    $sidebar = dynamic_sidebar('sidebar-19');
    $new_content = ob_get_clean();
    if ( ! is_admin() ) {
        return pietergoosen_insert_content( $new_content, 2, $content );
    }
    return $content;
}

If i replace

if ( ! is_admin() ) {

with

if ( ! is_admin() || is_paged() || $wp_query->current_post > 0 ) {

it shows the widget in all other posts and not in the first post. Any suggestions on a conditional tag for this

1
  • What do you mean by "only the first post"? Are you trying to add this only to the first post in the loop?
    – s_ha_dum
    Commented Jan 26, 2014 at 4:05

1 Answer 1

1

Assuming you are trying to add content only to the first post in the loop, a static variable should do it:

function pietergoosen_insert_content_filter( $content ) {
    static $first;
    $first = (!isset($first)) ? true : false;
    if (true == $first) {
      ob_start();
      $sidebar = dynamic_sidebar('sidebar-19');
      $new_content = ob_get_clean();
      if ( ! is_admin() ) {
          return pietergoosen_insert_content( $new_content, 2, $content );
      }
    }
    return $content;
}
add_filter('the_content','pietergoosen_insert_content_filter');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.