Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Wordpress] Dostęp do listy kategorii
Vill
post
Post #1





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


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.




Ten post edytował Vill 5.09.2014, 11:55:59
Go to the top of the page
+Quote Post
rad11
post
Post #2





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


  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?
Go to the top of the page
+Quote Post
pedro84
post
Post #3





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

Ostrzeżenie: (0%)
-----


Tutaj masz link do Codexa. Ta pętla musi działać.

Pokaż output z
  1. var_dump($categories)
Go to the top of the page
+Quote Post
Vill
post
Post #4





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post
rad11
post
Post #5





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


To pokaz output z var dumpa
Go to the top of the page
+Quote Post
Vill
post
Post #6





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


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)
Go to the top of the page
+Quote Post
pedro84
post
Post #7





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

Ostrzeżenie: (0%)
-----


OK, pokaż output z tego:
  1. $categories = get_categories( array('type' => 'post') );
  2.  
  3. foreach ($categories as $cat) {
  4. var_dump($cat->name);
  5. }
Go to the top of the page
+Quote Post
Vill
post
Post #8





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


Cytat
string 'Bez kategorii' (length=13)
string 'Kategoria 1' (length=11)
string 'Kategoria 2' (length=11)
Go to the top of the page
+Quote Post
pedro84
post
Post #9





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

Ostrzeżenie: (0%)
-----


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?
Go to the top of the page
+Quote Post
Vill
post
Post #10





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


Nie, zwykły tekst się wypisuje bez problemu.

$categories[0]->name też, jedynie indeksy od 1 w górę mają problem z wypisywaniem się
Go to the top of the page
+Quote Post
rad11
post
Post #11





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


A dlaczego masz przeskok w indekach 0,2,3 moze to jest przyczyna. Skprobuj wypisac bez indeksu 1

Ten post edytował rad11 5.09.2014, 12:08:57
Go to the top of the page
+Quote Post
Vill
post
Post #12





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


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 (IMG:style_emoticons/default/smile.gif) No to dopiszę sobie sprawdzanie czy obiekt nie jest pusty i problem rozwiązany (IMG:style_emoticons/default/smile.gif) .

Ten post edytował Vill 5.09.2014, 12:18:05
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.12.2025 - 00:07