Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Wordpress] Dostęp do listy kategorii
Forum PHP.pl > Forum > Gotowe rozwiązania > Systemy portalowe i CMS'y
Vill
Mam listę kategorii pobraną w ten sposób:

  1. $categories = get_categories( array('type' => 'post') );


Jak się dostać to poszczególnych elementów tablicy po indeksie? Dla testów wypisuje sobie nazwy kategorii w pętli foreach, z tym nie ma żadnych problemów, wypisuje się jak trzeba.

  1. foreach ($categories as $cat) {
  2. echo $cat->name;
  3. }


Problem pojawia się jak próbuję dostać się do tej tablicy po indeksie

  1. echo $categories[0]->name;
  2. echo $categories[1]->name;
  3. echo $categories[2]->name;


Wypisuje się tylko kategoria z indeksem 0. Te z indeksem 1 i 2 się nie wypisują. Jak się dostać do tych elementów? get_categories() zwraca tablicę obiektów.


rad11
  1. foreach ($categories as $i => $cat) {
  2. echo $i[0]->name;
  3. echo $i[1]->name;
  4. echo $i[2]->name;
  5. }
  6.  
  7.  


A tak probowales?
pedro84
Tutaj masz link do Codexa. Ta pętla musi działać.

Pokaż output z
  1. var_dump($categories)
Vill
Cytat(rad11 @ 5.09.2014, 12:46:06 ) *
  1. foreach ($categories as $i => $cat) {
  2. echo $i[0]->name;
  3. echo $i[1]->name;
  4. echo $i[2]->name;
  5. }
  6.  
  7.  


A tak probowales?


Wykonuje tyle iteracji ile trzeba, ale nie wypisuje nic.
rad11
To pokaz output z var dumpa
Vill
Cytat(pedro84 @ 5.09.2014, 12:46:35 ) *
Tutaj masz link do Codexa. Ta pętla musi działać.

Pokaż output z
  1. var_dump($categories)



Cytat
array (size=3)
0 =>
object(stdClass)[1759]
public 'term_id' => &string '1' (length=1)
public 'name' => &string 'Bez kategorii' (length=13)
public 'slug' => &string 'bez-kategorii' (length=13)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '1' (length=1)
public 'taxonomy' => string 'category' (length=8)
public 'description' => &string '' (length=0)
public 'parent' => &string '0' (length=1)
public 'count' => &string '1' (length=1)
public 'cat_ID' => &string '1' (length=1)
public 'category_count' => &string '1' (length=1)
public 'category_description' => &string '' (length=0)
public 'cat_name' => &string 'Bez kategorii' (length=13)
public 'category_nicename' => &string 'bez-kategorii' (length=13)
public 'category_parent' => &string '0' (length=1)
2 =>
object(stdClass)[1761]
public 'term_id' => &string '2' (length=1)
public 'name' => &string 'Kategoria 1' (length=11)
public 'slug' => &string 'kategoria1' (length=10)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '2' (length=1)
public 'taxonomy' => string 'category' (length=8)
public 'description' => &string '' (length=0)
public 'parent' => &string '0' (length=1)
public 'count' => &string '1' (length=1)
public 'cat_ID' => &string '2' (length=1)
public 'category_count' => &string '1' (length=1)
public 'category_description' => &string '' (length=0)
public 'cat_name' => &string 'Kategoria 1' (length=11)
public 'category_nicename' => &string 'kategoria1' (length=10)
public 'category_parent' => &string '0' (length=1)
3 =>
object(stdClass)[1762]
public 'term_id' => &string '4' (length=1)
public 'name' => &string 'Kategoria 2' (length=11)
public 'slug' => &string 'kategoria2' (length=10)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '4' (length=1)
public 'taxonomy' => string 'category' (length=8)
public 'description' => &string '' (length=0)
public 'parent' => &string '0' (length=1)
public 'count' => &string '2' (length=1)
public 'cat_ID' => &string '4' (length=1)
public 'category_count' => &string '2' (length=1)
public 'category_description' => &string '' (length=0)
public 'cat_name' => &string 'Kategoria 2' (length=11)
public 'category_nicename' => &string 'kategoria2' (length=10)
public 'category_parent' => &string '0' (length=1)
pedro84
OK, pokaż output z tego:
  1. $categories = get_categories( array('type' => 'post') );
  2.  
  3. foreach ($categories as $cat) {
  4. var_dump($cat->name);
  5. }
Vill
Cytat
string 'Bez kategorii' (length=13)
string 'Kategoria 1' (length=11)
string 'Kategoria 2' (length=11)
pedro84
Czyli pętla śmiga. A nie masz Ty czasem jakiegoś problemu ze stylem, który powoduje, że przy echo masz tekst ukryty? CSS? Albo niedomknięty tag?
Vill
Nie, zwykły tekst się wypisuje bez problemu.

$categories[0]->name też, jedynie indeksy od 1 w górę mają problem z wypisywaniem się
rad11
A dlaczego masz przeskok w indekach 0,2,3 moze to jest przyczyna. Skprobuj wypisac bez indeksu 1
Vill
Faktycznie, ma pusty obiekt w tablicy. Foreach zwraca same istniejące, a idąc po indeksach natrafia się na puste obiekty, wtedy nic nie wypisuje. Hmm, myślałam że ta funkcja mi załatwi filtrowanie, ale widocznie nie załatwiła smile.gif No to dopiszę sobie sprawdzanie czy obiekt nie jest pusty i problem rozwiązany smile.gif.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.