PHP sprintf
Page 1 of 1
Interinactive
VIP Member



Posts: 29467

PostPosted: Tue, 11th Nov 2014 07:33    Post subject: PHP sprintf
Code:
   function portfolios( $atts, $content )
   {
      extract( shortcode_atts( array(
         'title_wrap'    => 1,
         'title'         => '',
         'format_icon'   => 1,
         'total_columns' => 12,
         'number'        => 3,
         'cat'           => '',
      ), $atts ) );

      $args = array(
         'posts_per_page' => $number,
         'post_type'      => 'portfolio',
      );
      if ( $cat )
         $args['portfolio_category'] = $cat;
      $query = new WP_Query( $args );
      if ( !$query->have_posts() )
         return '';

      $html = '<div class="shortcode-portfolios">';
      if ( $title_wrap )
      {
         $html .= sprintf( '
            <div class="clearfix">
               <section class="section-title">
                  <h4>%s</h4>
                  <a class="more-link" href="%s">%s</a>
               </section>',
            $title,
            get_post_type_archive_link( 'portfolio' ),
            __( 'View all projects <span>></span>', 'whisper' )
         );
      }

      $i = 1;
      while ( $query->have_posts() ):
         $query->the_post();

         $link = get_permalink();
         list( $thumb_full ) = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
         $html .= sprintf(
            '<section class="grid_%s%s">
               <figure class="portfolio">
                  <div class="portfolio-image">
                     %s
                     <span class="portfolio-button portfolio-zoom"><a href="%s" rel="prettyPhoto[pp_gallery]" title="%s">&nbsp;</a></span>
                     <span class="portfolio-button portfolio-single"><a href="%s">&nbsp;</a></span>
                  </div>

                  <figcaption class="clearfix">
                     %s
                     <div class="caption-title">
                        <p class="title">%s</p>
                        <span class="subtitle">%s</span>
                     </div>
                  </figcaption>
               </figure>
            </section>',
            $total_columns / $number,
            $i == 1 ? ' alpha' : ( $i == $number ? ' omega' : '' ),
            get_the_post_thumbnail( null, 'portfolio-thumbs' ),
            $thumb_full,
            the_title_attribute( 'echo=0' ),
            $link,
            $format_icon ? whisper_format_icon( false, false ) : '',
            get_the_title(),
            whisper_meta( 'subtitle' )
         );

         $i = $i == $number ? 1 : ( $i + 1 );
      endwhile;
      wp_reset_postdata();

      if ( $title_wrap )
         $html .= '</div>';

      $html .= '</div>';

      return $html;
   }


See .portfolio-image? Directly inside that class is an image, the '%s'. I'm trying to link that image AND p.title with $link, but no matter what I try, it screws up one way or another. I'm not a programmer, and I'm not sure how this is working exactly. I'm definitely not sure how '%s' can represent everything in that section.

Can anyone give any tips as to how I would go about linking that image and title? Thanks.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Tue, 11th Nov 2014 09:08    Post subject:
%s represents one of the following parameters that you put in your execution there.

Without knowing where those parameters are coming from, there is not much I can do for you.

Replace the
Code:

$html .= sprintf(
...
);

with this, maybe this'll make it a bit easier for you, to actually see where your problem lies
Code:

$html .= '<section class="grid_'.($total_columns / $number).($i == 1 ? ' alpha' : ( $i == $number ? ' omega' : '' )).'">'.PHP_EOL.
'      <figure class="portfolio">'.PHP_EOL.
'         <div class="portfolio-image">'.PHP_EOL.
'             '.get_the_post_thumbnail( null, 'portfolio-thumbs' ).PHP_EOL.
'             <span class="portfolio-button portfolio-zoom"><a href="'.$thumb_full.'" rel="prettyPhoto[pp_gallery]" title="'.the_title_attribute( 'echo=0' ).'">&nbsp;</a></span>'.PHP_EOL.
'             <span class="portfolio-button portfolio-single"><a href="'.$link.'">&nbsp;</a></span>'.PHP_EOL.
'         </div>'.PHP_EOL.PHP_EOL.
'         <figcaption class="clearfix">'.PHP_EOL.
'             '.($format_icon ? whisper_format_icon( false, false ) : '').PHP_EOL.
'             <div class="caption-title">'.PHP_EOL.
'                  <p class="title">'.get_the_title().'</p>'.PHP_EOL.
'                  <span class="subtitle">'.whisper_meta( 'subtitle' ).'</span>'.PHP_EOL.
'             </div>'.PHP_EOL.
'         </figcaption>'.PHP_EOL.
'    </figure>'.PHP_EOL.
'</section>'.PHP_EOL;



Can you post what your PHP actually generates?

Also keep in mind that $total_columns / $number can produce a float number (3.1415)


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Interinactive
VIP Member



Posts: 29467

PostPosted: Tue, 11th Nov 2014 09:15    Post subject:
⁢⁢


Last edited by Interinactive on Tue, 5th Oct 2021 01:33; edited 1 time in total
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Tue, 11th Nov 2014 09:20    Post subject:
Come to slack mate Very Happy


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Tue, 11th Nov 2014 09:56    Post subject:
PHP's sprintf is identical to sprintf everywhere.

The "%s" just means it's a string placeholder ("%d" would be digit for example; an integer).

You need as many arguments to the function as you have placeholders, it can't automagically figure out which is which. Any extra arguments you send aren't used.

A few examples:
- sprintf('%s %s %s', 'one', 'two', 'three') produces 'one two three'
- sprintf('%s %s %s', 'one', 'two', 'three', 'four', 'five') produces 'one two three'
- sprintf('%s %s %s', 'one', 'one', 'three') produces 'one one three'

So in your case, this is what each argument replaces right now (see the comments):
Code:

$html .= sprintf(
    '<section class="grid_%s%s">
       <figure class="portfolio">
          <div class="portfolio-image">
             %s
             <span class="portfolio-button portfolio-zoom">
                <a href="%s" rel="prettyPhoto[pp_gallery]" title="%s">&nbsp;</a>
             </span>
             <span class="portfolio-button portfolio-single">
                <a href="%s">&nbsp;</a>
             </span>
          </div>

          <figcaption class="clearfix">
             %s
             <div class="caption-title">
                <p class="title">%s</p>
                <span class="subtitle">%s</span>
             </div>
          </figcaption>
       </figure>
    </section>',
    $total_columns / $number,                            // the first %s in grid_%s%s
    $i == 1 ? ' alpha' : ( $i == $number ? ' omega' : '' ),    // the second %s in grid_%s%s
    get_the_post_thumbnail( null, 'portfolio-thumbs' ),       // the first %s in .portfolio-image
    $thumb_full,                                     // .portfolio-zoom > a[href]
    the_title_attribute( 'echo=0' ),                      // .portfolio-zoom > a[title]
    $link,                                           // .portfolio-single > a[href]
    $format_icon ? whisper_format_icon( false, false ) : '',    // the first %s in figcaption
    get_the_title(),                                  // the %s in .caption-title > .title
    whisper_meta( 'subtitle' )                            // the %s in .caption-title > .subtitle
);


By the sound of it, you want this (note the changed HTML and the extra $link argument):
Code:

$html .= sprintf(
    '<section class="grid_%s%s">
       <figure class="portfolio">
          <div class="portfolio-image">
             %s
             <span class="portfolio-button portfolio-zoom">
                <a href="%s" rel="prettyPhoto[pp_gallery]" title="%s">&nbsp;</a>
             </span>
             <span class="portfolio-button portfolio-single">
                <a href="%s">&nbsp;</a>
             </span>
          </div>

          <figcaption class="clearfix">
             %s
             <div class="caption-title">
                <p class="title">
                   <a href="%s">%s</a>
                </p>
                <span class="subtitle">%s</span>
             </div>
          </figcaption>
       </figure>
    </section>',
    $total_columns / $number,                            // the first %s in grid_%s%s
    $i == 1 ? ' alpha' : ( $i == $number ? ' omega' : '' ),    // the second %s in grid_%s%s
    get_the_post_thumbnail( null, 'portfolio-thumbs' ),       // the first %s in .portfolio-image
    $thumb_full,                                     // .portfolio-zoom > a[href]
    the_title_attribute( 'echo=0' ),                      // .portfolio-zoom > a[title]
    $link,                                           // .portfolio-single > a[href]
    $format_icon ? whisper_format_icon( false, false ) : '',    // the first %s in figcaption
    $link,                                           // the %s in .caption-title > .title > a[href]
    get_the_title(),                                  // the %s in .caption-title > .title > a >
    whisper_meta( 'subtitle' )                            // the %s in .caption-title > .subtitle
);



Where these parameters come from is irrelevant, not sure why Pumpy wants to know that.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group