1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need help with echo

Discussion in 'PHP' started by Divvy, Jun 27, 2017.

  1. #1
    Hello guys,

    Can someone give me a little help here?

    I have this php code:
    
    <?$args = array( 'post_type' => 'videos', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    
    
    echo '<div class="crp_related "><ul><li>'; 
    echo '<a href="';
    the_permalink(); 
    echo '">';
    the_post_thumbnail('related-thumb');
    echo '</a>';
    echo '<span class="crp_title" style="bottom:4px;">';
    if (strlen($post->post_title) > 28) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 28) . '...'; } else {
    the_title();
    }
    echo '</span>';
    echo '</ul></li>';
    echo '<div class="crp_clear"></div></div>';
    
    endwhile;
    ?>
    
    PHP:
    And this is the html that is showing in the source:
    
    <div class="crp_related">
    <ul><li>
    <a href="##URL1##"><img width="200" height="200" src="##IMAGE1##" class="attachment-related-thumb size-related-thumb wp-post-image" alt=""></a>
    <span class="crp_title" style="bottom:4px;">##TITLE1## ...</span>
    </li></ul>
    <div class="crp_clear">
    </div></div>
    
    <div class="crp_related">
    <ul><li>
    <a href="##URL2##"><img width="200" height="200" src="##IMAGE2##" class="attachment-related-thumb size-related-thumb wp-post-image" alt=""></a>
    <span class="crp_title" style="bottom:4px;">##TITLE2## ...</span>
    </li></ul>
    <div class="crp_clear">
    </div></div>
    
    HTML:
    And this is the html that I want to show in the source:
    
    <div class="crp_related">
    <ul><li>
    <a href="##URL1##"><img width="200" height="200" src="##IMAGE1##" class="attachment-related-thumb size-related-thumb wp-post-image" alt=""></a>
    <span class="crp_title" style="bottom:4px;">##TITLE1## ...</span>
    </li>
    <li>
    <a href="##URL2##"><img width="200" height="200" src="##IMAGE2##" class="attachment-related-thumb size-related-thumb wp-post-image" alt=""></a>
    <span class="crp_title" style="bottom:4px;">##TITLE2## ...</span>
    </li>
    </ul>
    <div class="crp_clear">
    </div></div>
    
    HTML:
    Any idea how to do this please? :)

    I think the main problem is that "ul" tag is appearing multiple times and I need to appear just one. And other repeat class codes for example...

    HELP :)
     
    Solved! View solution.
    Divvy, Jun 27, 2017 IP
  2. #2
    Try something like this

    
    <?
    $args = array( 'post_type' => 'videos', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    
    echo '<div class="crp_related "><ul>';
    
    while ( $loop->have_posts() ) : $loop->the_post();
    
    echo '<li><a href="', the_permalink(), '">', the_post_thumbnail('related-thumb'), '</a>';
    echo '<span class="crp_title" style="bottom:4px;">';
    if (strlen($post->post_title) > 28) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 28) . '...'; }
    else {
    the_title();
    }
    echo '</span></li>';
    
    endwhile;
    echo '</ul>';
    echo '<div class="crp_clear"></div></div>';
    ?>
    PHP:
    I couldn't help meddling with the echos but essentially the key was to have the <ul> tag outside the loop and to have the <li> around the list item.
     
    sarahk, Jun 27, 2017 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Hey sarahk,

    Thank you a lot!! Worked perfectly. :D
    Your code makes totally sense to me.
     
    Divvy, Jun 27, 2017 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Probably help even more if you STOP using multiple echo to do one echo's job, STOP using the stupid malfing "endwhile" structure that should NEVER have existed in PHP in the first place, formatted your PHP code with sensible indents, and formatted your output to ease in debugging.

    I'd probably also kill the PHP 5.3/earlier support and use bracketed arrays, and not use the shortest shorttag opening that shouldn't even be ENABLED in your php configuration in the first place!

    Since they're the only span it's unlikely those need classes either, and since UL is a perfectly good block level container, what's the DIV even for? Much less the outdated garbage clearing DIV after like it's still 2003 and static style in the markup... and remember, ternary operators are your friend!

    ... and people wonder why I call it turdpress. Oh, pretty sure that the_title code parameters are gibberish too.

    it is HIGHLY unlikely there's any legitimate reason for your code for that to be much more than:

    <?php
    $args = [
    	'post_type' => 'videos',
    	'posts_per_page' => 10
    ];
    $loop = new WP_Query( $args );
    
    echo '
    	<ul class="crp_related">';
    
    while ($loop->have_posts()) {
    	$loop->the_post();
    	echo '
    		<li>
    			<a href="', the_permalink(), '">
    				', the_post_thumbnail('related-thumb'), '
    			</a>
    			<span>', (
    				strlen($title= the_title('', '', false)) > 28 ?
    				substr($title, 0, 28) :
    				$title
    			), '</span>
    		</li>';
    }
    
    echo '
    	</ul>';
    PHP:
    You want bottom padding? Put it in your bloody EXTERNAL stylesheet. Whatever you were doing with the two DIV, in all but the rarest of cases do it on the UL.

    Hell, depending on how you are styling it, you might not even need the span.

    Stupid huffing while : endwhile -- whoever thought that belongs in PHP needs a kick to the groin followed by a fist to the face -- though to be fair I say the same thing about heredoc and nowdoc.
     
    deathshadow, Jun 29, 2017 IP
  5. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    Hey, thanks for the code cleaning, I already change it :)
     
    Divvy, Jun 29, 2017 IP