Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Umieszczenie pętli w return
AboutMe
post
Post #1





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Dzień doby

Muszę umieścić poniższy kod w return, czy w ogóle da się to zrobić przez łączenie stringów czy może przyposać to jakoś do zmiennej, jak to ugryźć?

  1. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) ) :
  2. while ( bp_members() ) : bp_the_member(); ?>
  3. <a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name(); ?> (<?php bp_member_last_active(); ?>)"><?php bp_member_avatar('width=150&height=150') ?></a>
  4. <?php endwhile;
  5. endif;


Generalnie potrzebuję tego do shortcode gdzie kod musi być umieszczony w return, w innym wypadku shortcode zostanie wyświetlony poza wrapperem.

Ten post edytował AboutMe 2.01.2017, 17:15:29
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 15)
Tomplus
post
Post #2





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


  1. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) ) :
  2. while ( bp_members() ) : bp_the_member();
  3. $content .= '<a href="'.bp_member_permalink().'" title="'.bp_member_name().' ('.bp_member_last_active().')">'.bp_member_avatar('width=150&height=150').'</a>
  4. endwhile;
  5. return $content;
  6. endif;


Pewnie to coś do wordpress? Powinno to działać. Ale ogólnie jeżeli coś prezentujesz jako echo najpierw, chcesz jako echo w innym miejscu to przypisz do zmiennej.


Go to the top of the page
+Quote Post
AboutMe
post
Post #3





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Tak to wordpress. Dziwna sprawa bo teraz wewnątrz wrappera wyświetla dokładnie to

  1. <a href="" title=" ()"></a>


a poza wrapperem to - http://pastebin.com/jKwDMGG0
Go to the top of the page
+Quote Post
Tomplus
post
Post #4





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Daj poniższe dwie linijki w {} a nie w : i endwhile. Zrób tak jak w byś pisał pętle w PHP

bp_the_member();
$content .= '<a>...';
Go to the top of the page
+Quote Post
AboutMe
post
Post #5





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Dzięki, tylko jak to zrobić z tym dwukropkiem po while?

  1. function popular_bp_members($atts, $content = null) {
  2. extract(shortcode_atts(array(
  3. "number_of_members" => '20'
  4. ), $atts));
  5.  
  6. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) )
  7. while ( bp_members() ) : bp_the_member() {
  8. $content .= '<a href="'.bp_member_permalink().'" title="'.bp_member_name().' ('.bp_member_last_active().')">'.bp_member_avatar('width=150&height=150').'</a>';
  9. return $content;
  10. }
  11. endif; ?>
  12. <div class="clear"></div><?php
  13.  
  14. }
  15.  
  16. add_shortcode("popular-bp-members", "popular_bp_members");
Go to the top of the page
+Quote Post
Tomplus
post
Post #6





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Dwukropek substytut nawiasów

Przykład:
  1. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


To samo tylko rozpisane:

  1. <?php
  2. if ( have_posts() ) {
  3. while ( have_posts() ) {
  4. the_post();
  5. }
  6. }
  7. ?>


Więcej na ten temat masz tutaj: https://codex.wordpress.org/The_Loop

Ten post edytował Tomplus 3.01.2017, 18:10:58
Go to the top of the page
+Quote Post
AboutMe
post
Post #7





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Wywala mi błąd Parse error: syntax error, unexpected '{' tutaj while ( bp_members() ) : bp_the_member() {

  1. function popular_bp_members($atts, $content = null) {
  2. extract(shortcode_atts(array(
  3. "number_of_members" => '20'
  4. ), $atts));
  5.  
  6. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) )
  7. while ( bp_members() ) : bp_the_member() {
  8. $content .= '<a href="'.bp_member_permalink().'" title="'.bp_member_name().' ('.bp_member_last_active().')">'.bp_member_avatar('width=150&height=150').'</a>';
  9. return $content;
  10. }
  11. endif; ?>
  12. <div class="clear"></div><?php
  13.  
  14. }
  15.  
  16. add_shortcode("popular-bp-members", "popular_bp_members");


Ten post edytował AboutMe 4.01.2017, 12:20:30
Go to the top of the page
+Quote Post
Tomplus
post
Post #8





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Nie ma co się dziwić.

po ma być:

while ( bp_members() ) {
bp_the_member()


czyli bez dwukropka.
Go to the top of the page
+Quote Post
AboutMe
post
Post #9





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Hmm ciągle nie działa, ten sam błąd

  1. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) )
  2. while ( bp_members() ) {
  3. bp_the_member() {
  4. $content .= '<a href="'.bp_member_permalink().'" title="'.bp_member_name().' ('.bp_member_last_active().')">'.bp_member_avatar('width=150&height=150').'</a>';
  5. return $content;
  6. }
  7. }
  8. endif;


Ten post edytował AboutMe 4.01.2017, 16:48:22
Go to the top of the page
+Quote Post
nospor
post
Post #10





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Gdy pozbywasz sie dwukropkow, to i pozbyc sie masz wszelkich endow. A na dal w kodzie masz
endif;

No i co to jest
bp_the_member() {
?
Go to the top of the page
+Quote Post
AboutMe
post
Post #11





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


bp_the_member() to część pętli bez której nic nie wyciągniesz

Taki kod niestety również nie działa Parse error: syntax error, unexpected '{'

  1. function popular_bp_members($atts, $content = null) {
  2. extract(shortcode_atts(array(
  3. "number_of_members" => '20'
  4. ), $atts));
  5.  
  6. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) )
  7. while ( bp_members() ) {
  8. bp_the_member() {
  9. $content .= '<a href="'.bp_member_permalink().'" title="'.bp_member_name().' ('.bp_member_last_active().')">'.bp_member_avatar('width=150&height=150').'</a>';
  10. return $content;
  11. } }
  12. ?>
  13. <div class="clear"></div><?php
  14.  
  15. }
  16.  
  17. add_shortcode("popular-bp-members", "popular_bp_members");
Go to the top of the page
+Quote Post
nospor
post
Post #12





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
bp_the_member() to część pętli bez której nic nie wyciągniesz

Nie chodzilo mi co robi funkcja bp_the_member() tylko chodzuli mi o bzdurny kod
bp_the_member() {
To jest zle temu masz bledy... widze aluzja nie dotarla (IMG:style_emoticons/default/tongue.gif)

nie
bp_the_member() {

a:
bp_the_member();
I masz usunac } ktory byl dla tego {
Go to the top of the page
+Quote Post
AboutMe
post
Post #13





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Dzięki ale taki kod robi dokładnie do samo jak to http://forum.php.pl/index.php?showtopic=25...p;#entry1207615 z tą różnicą że wyświetla tylko jeden profil. Chyba jednak zrobię to w widgecie zamiast w shortcode.

  1. function popular_bp_members($atts, $content = null) {
  2. extract(shortcode_atts(array(
  3. "number_of_members" => '20'
  4. ), $atts));
  5.  
  6. if ( bp_has_members( 'type=popular&max=' . $number_of_members . '' ) )
  7. while ( bp_members() ) {
  8. bp_the_member();
  9. $content .= '<a href="'.bp_member_permalink().'" title="'.bp_member_name().' ('.bp_member_last_active().')">'.bp_member_avatar('width=150&height=150').'</a>';
  10. return $content;
  11. }
  12. ?>
  13. <div class="clear"></div><?php
  14.  
  15. }
  16.  
  17. add_shortcode("popular-bp-members", "popular_bp_members");
Go to the top of the page
+Quote Post
nospor
post
Post #14





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Nie wiem, nie znam sie na WP. Ja ci jedynie poprawilem PARSE ERRORy (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
Tomplus
post
Post #15





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Korzystałeś w ogóle z tego manuala dla pętli do Wordpressa co podałem w trzecim moim poście?
W sumie tam logicznie wszystko wytłumaczyli, z przykładami.
Go to the top of the page
+Quote Post
AboutMe
post
Post #16





Grupa: Zarejestrowani
Postów: 261
Pomógł: 0
Dołączył: 24.02.2008

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


Tak, pętla działa ok gdy stworzy się z niej widget ale znalazłem już rozwiązanie, wystarczy na końcu pętli użyć

  1. $myvariable = ob_get_clean();
  2. return $myvariable;


https://code.tutsplus.com/tutorials/create-...eters--wp-32199
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: 25.08.2025 - 14:43