Forums

[resolved] loop in the loop but stay out of the loop? (8 posts)

  1. deepbevel
    Member
    Posted 1 year ago #

    I have this query, It's to be used in the loop, and this code works fine and does exactly what it should.

    "for use in the loop, list post titles related to first tag on current post"

    <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      echo '';
      $first_tag = $tags[0]->term_id;
      $args=array(
       'tag__in' => array($first_tag),
       'showposts'=>0,
       'caller_get_posts'=>0,
       'order'    => 'ASC' );
    $my_query = new WP_Query($args); if( $my_query->have_posts() )
    {while ($my_query->have_posts()) : $my_query->the_post(); ?>
    (output here, just trying to save space) <?php endwhile;} } ?>

    however, if i use it in the loop, (which is the only way it works) it gets picked up by the post as the_content, but not as a list, instead it shows the first full post in the list!
    Any content I add to the post through the post editor is ignored. The only way to avoid it is to include this after the_content, but before the endif, does not work after the endwhile, which makes sence because that closes the main loop.

    I have no problem having this query after the content, except the query displays a post list, and it needs to be at the top of the page... But then it's outside the loop? or does it just need to be after the "if have posts" and followed by "the post" ?

    So I'm in a bind with this, can't help but think there must be a stock way to keep my main loop clear of the query, while keeping the query in the loop...?

  2. deepbevel
    Member
    Posted 1 year ago #

    I tried to get this code to work outside the loop, I can do it, but it won't work in my situation. The content for the posts which are called in the query are shortcodes of other post content. It appears Get_Post (which is what I used to do this outside the loop) calls the original post content from the shortcode, and doesn't see the post which contains the shortcode.
    Thats a problem because the original posts do not have the tags which are called in the query!

  3. deepbevel
    Member
    Posted 1 year ago #

    Solved. Just had to use it after if have posts, but before while have posts. As expected, turns out out be common proceedure for using secondary loops.
    The_Loop

    I'll bet esmi has already pointed this out to me a few times. duh.

  4. deepbevel
    Member
    Posted 1 year ago #

    If anyone has a suggestion for my unique outside the loop issue, I'm all ears. That would be ideal.

  5. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    You lost me about halfway through your first post! Can you start again by describing what it is that you are trying to do?

  6. deepbevel
    Member
    Posted 1 year ago #

    here goes..
    In an attempt to get this outside the loop, I'm using that code as it's written, in the sidebar, but using Get_Posts in front of it to grab the current post and bring it to the sidebar, that way my secondary loop can grab the current posts tag and do it's filter accordingly.

    But I need it to get that initial tag from a post which has another post inside of it. It appears to look for output only from the post within the post. And not the containing post.

    So, the query get's the post in the post, looks for a tag, and doesn't find one. So of course there's no output. If it could see the post which contains the post, it would find a tag and do it's thing.

  7. deepbevel
    Member
    Posted 1 year ago #

    I figure this had to be an unussual situaltion, to get the current post's tag outside the loop, when the current post's content consists of another post, rendered with shortcode. Why on earth would someone need to do that? haha

  8. deepbevel
    Member
    Posted 1 year ago #

    and so, this

    <?php
       global $post;
       foreach(get_the_tags($post->ID) as $tag) {
          echo $tag->name . ', ';
       }
    ?>
    <?php endforeach();?>

    is what I out in front of the WP_Query.

    and it does work, so long as the post has a tag, of course.

    but like I say, the post that has the tag, has another post as it's content.

    that other post is pulled with this shortcode:

    [xilipostinpost showcontent=1 showexcerpt=0 showtitle=0 query='name= san-diego-2']

    inserted into the post content field.

    and so for what ever reason, get_the_tags only works for tags that are associated with the post, inside the post. But not, the post.

    I suppose I did my best to explain, understandable if it couldn't quite get it across. Thanks just the same as always:)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.