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

  • 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 Jan 26 '14 at 4:05
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');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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