3

I have a sticky post on my wordpress blog, in the beginning. I would like to place some random content in it, by simply replacing some HTML in the post content. The random content would come from earlier posts. For doing this I edited my template's "blog.php" file, to interact with the main wordpress loop. The ID of my sticky post is given and fixed: 1469 So I wrote the following in the "blog.php" (unfotunately doesn't work):

while (have_posts()):

    if ($_GET["email"]=="1") {
      $post_id=get_the_ID();
      echo $post_id;
      if ($post_id==1469) {
         echo get_post_field('post_content', $post_id2);
         continue;
      } 
      else {
        the_post();
      }

    } 
    else {
      the_post();
    }
endwhile;

I use a "secret" query string variable "email" to test my attempts and not showing them to the visitors. So I test that query string key at the beginning. The problem is I get the same sticky post again and again. I think omitting function the_post() in the main loop cycle doesn't increase some internal global variable, so in fact I get an infinite loop. My question is: how can I skip one post in the loop ( and echoing it manually after some modification in the content) and not getting into the infinite loop?

Second attempt as I forgot about "the_content()"

function patch_content(c) {
  ...
  return(patched_content)
}


while (have_posts()):

        if ($_GET["email"]=="1") {
          $post_id=get_the_ID();
          echo $post_id;
          if ($post_id==1469) {
             the_post() 
             echo patch_content(get_post_field('post_content', $post_id));
          } 
          else {
            the_post();
            the_content()
          }

        } 
        else {
          the_post();
          the_content()
        }
    endwhile;
  • You said the ID of your sticky post is fixed, but then that you keep getting the same sticky post again and again... what did you want to do, exactly? Hardcoding one ID will obviously get you that same post, but I suspect I'm not following what you want to achieve. – Tim Malone May 30 '16 at 10:29
  • this code sample is in the while loop "while (have_posts())". It queries the actual post ID, and if it is equal to the sticky post's ID then it doesn't call "the_post()". Instead of it calls an echo with the current sticky pot content, later I will inject random content into it by simply replacing some HTML. I tried the statement "continue" after the "echo get_post_field", but no success. – Konstantin May 30 '16 at 10:42
  • can you edit your post to include this information, particularly the full code? It's difficult to picture what you're doing. We need to know what you have, and what you've tried, in order to help you :) Also, I suppose you've searched and looked at posts like this one ? wordpress.stackexchange.com/questions/163419/… – Tim Malone May 30 '16 at 10:48
  • have you considered to use the filter 'the_content' together with some conditional to check for the sticky-ness of the post? – Michael May 30 '16 at 22:25

Your Answer

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

Browse other questions tagged or ask your own question.