• Resolved Shabir Virk

    (@sakuhetu)


    I am getting a problem when try to run Multiple Loops using Foreach

    The Original Code is

    $getpost = new WP_Query('posts_per_page=-1&femalesubcats=a-name-female-celebrities');
    if ($getpost->have_posts()) :
    	while ($getpost->have_posts()) : $getpost->the_post();
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags[] = $tag->term_id;
    			}
    		}
    	endwhile;
    endif;
    
    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);
    $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array', 'include'   => $tags_str ) );
    
    ?>
    
    <?php
    foreach ($tag as $tag_item) :
    echo $tag_item.'<br />';
    
    endforeach;

    You can see i created a custom Taxonomy named Female Sub Categories (femalesubcats) having these terms

    a-name-female-celebrities
    b-name-female-celebrities

    n-name-female-celebrities
    ..
    z-name-female-celebrities

    Totally A-Z Terms

    Now, i placed all of the terms in array and use a single foreach

    $termarray = array('a-name-female-celebrities', 'b-name-female-celebrities');
    foreach( $termarray as $terms )
    {
    $getpost = new WP_Query('posts_per_page=-1&femalesubcats='.$terms.'');
    if ($getpost->have_posts()) :
    	while ($getpost->have_posts()) : $getpost->the_post();
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags[] = $tag->term_id;
    			}
    		}
    	endwhile;
    endif;
    }
    
    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);
    $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array', 'include'   => $tags_str ) );
    
    ?>
    
    <?php
    foreach ($tag as $tag_item) :
    echo $tag_item.'<br />';
    
    endforeach;
    
    echo '--------------------';

    All of the terms are showing on one page.. But the problem is i need to write the “Seperator Line” after ending the A.

    But right now the line is showing after all the terms.. Means at the end of the page.

    Is there anything iam missing?
    Or is there any other easy way to do this.

    Waiting for Reply

Viewing 11 replies - 16 through 26 (of 26 total)
  • Thread Starter Shabir Virk

    (@sakuhetu)

    I Want the output like

    A.J. Cook
    Aaliyah
    Aarti Chhabria
    Abbey Clancy
    Abbey Lee Kershaw
    Abbie Cornish
    Abby Brammell
    ——-
    Babs de Jongh
    Bailey Nortje
    Bailey Rose
    Bai Ling
    Barbara Guerra
    ——-
    Cameron Diaz
    Camilla Belle
    Candice Accola
    Candice Swanepoel
    Cara Delevingne
    Carice Van Houten

    I really can’t do anything without seeing the code you are using and the live screen that is the result. Please put the code into a pastebin, post a link to it here, and post a link to the site where the problem can be seen (NOT a screenshot).

    Thread Starter Shabir Virk

    (@sakuhetu)

    Oh

    Here is the link for the code

    https://pastebin.com/XGQ9XwW6

    I still need to see what it shows now so I can tell where it needs to be changed. Please post a link to the site where it can be seen.

    Thread Starter Shabir Virk

    (@sakuhetu)

    Direct Link to the site

    https://goo.gl/STtEyR

    Sorry, I am still not clear on what you want. How is the current output different from what you want?

    Thread Starter Shabir Virk

    (@sakuhetu)

    I Want the Title of the index
    A Name Celebrities
    ——-
    A.J. Cook
    Aaliyah
    Aarti Chhabria
    Abbey Clancy
    Abbey Lee Kershaw
    Abbie Cornish
    Abby Brammell

    B Name Celebrities
    ——-
    Babs de Jongh
    Bailey Nortje
    Bailey Rose
    Bai Ling
    Barbara Guerra

    C Name Celebrities
    ——-
    Cameron Diaz
    Camilla Belle
    Candice Accola
    Candice Swanepoel
    Cara Delevingne
    Carice Van Houten

    OK, I’ll work on that.

    Thread Starter Shabir Virk

    (@sakuhetu)

    Thanks Man,

    I tired but the index titles are coming after the index end.

    Can’t get them before the list..

    Hope you will solve this

    I think this is what you want:

    <div class="tag_column">
    <?php  $count=0;
    $all_tags = array();
    foreach ($tag as $tag_item) {
        preg_match('/>([^<]+)<\/a/', $tag_item, $matches );
       $this_letter = strtoupper(substr( $matches[1], 0, 1 ) );
       if ( $this_letter != $current_letter ) :
          echo "$this_letter Name Celebrities<br />";
          echo '--------------------<br />';
       endif;
       $current_letter = $this_letter;
       echo $tag_item.'<br />'; $count++;
       if ($count==$tags_per_col) {
           echo '</div><div class="tag_column">'; $count=0;
       }
    }
    echo '</div></div></div>';
    ?>
    Thread Starter Shabir Virk

    (@sakuhetu)

    Yup its worked..

    Thanks man ??

Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘Multiple Query_Posts Using Foreach’ is closed to new replies.