-1

I am trying to get the permalink and title into variables to use outside of a loop - I have made it work for the thumbnail but for some reason can t get it to work for these two.

Here is my code:

<div class="medium-12 medium-centered columns">

                <?php global $post; // required
                $args = array('category' => 5,
                             'posts_per_page'=> 7); // include category 5 (Action)
                $custom_posts = get_posts($args); 
               $count3=1;                  


                foreach($custom_posts as $post) : setup_postdata($post); 
               $v =  get_the_post_thumbnail($post->ID, 'medium');
               $var = "img".$count3;
                $$var = $v;

                $l =  get_the_permalink();
               $link = "link".$count3;
                $$l = $l;

                $t = get_the_title();
                 $tt =  "title".$count3;
                $$t = $t;


               ?>


               <?php $count3++; ?>

               <?php   endforeach; ?>

                <div class="row collapse imageGrid photogrid" data-equalizer="fullRow">

                        <div class="medium-4 columns" data-equalizer-watch="fullRow">
                              <a href="<?php echo $link1; ?>">
                                <span class="centertitle"><?php echo $title1; ?></span>  
                            <?php echo $img1; ?>
                                   </a>
                       </div>


                       <div class="medium-4 columns" data-equalizer-watch="fullRow"  data-equalizer="watchColumn">
                           <div class="row collapse"  data-equalizer-watch="watchColumn">
                            <?php echo $img2; ?>
                           </div>
                            <div class="row collapse" >
                                <div class="medium-6 columns" data-equalizer-watch="watchColumn"> <?php echo $img3; ?></div>
                                <div class="medium-6 columns" data-equalizer-watch="watchColumn"><?php echo $img4; ?></div>
                           </div>
                       </div>

                        <div class="medium-4 columns" data-equalizer-watch="fullRow"  data-equalizer="watchColumn2">

                            <div class="row collapse">
                                <div class="medium-6 columns" ><?php echo $img5; ?></div>
                                <div class="medium-6 columns" ><?php echo $img6; ?></div>
                           </div>
                             <div class="row collapse" data-equalizer-watch="watchColumn2">
                            <?php echo $img7; ?>
                           </div>
                       </div>

                        <?php   wp_reset_query(); ?>   

             </div> 
         </div>

I have tried different ways of putting the title including:

$t = the_title('', '', false);

as $t = the_title() echos the title straight away.

Any help in how to put them into the variable would be much appreciated

1
  • can i ask what the down vote was for?
    – anna
    Aug 15, 2017 at 13:48

2 Answers 2

0

Creating variables on the fly like that is not needed. Just use an array instead.

$myvars = [];

inside your foreach loop you can:

$myvars[] = array('title'=>$t,'image'=>$v);

After your loop you can:

//First title, First image
echo $myvars[0]['title'];
echo $myvars[0]['image'];

//Second title, Second image
echo $myvars[1]['title'];
echo $myvars[1]['image'];

This way you can always print_r($myvars) and see the content inside of it.

0

Please Use This

$t = get_the_title('');
3
  • That doesn't seem to work either - the following is empty - <?php echo $title1; ?>
    – anna
    Aug 8, 2017 at 11:44
  • you want to title add in a variable and that variable want to check title was empty or not?
    – Vishit
    Aug 8, 2017 at 12:02
  • no i need the title to be put in a variable with the count3 so that i can then retrieve the variable outside of the loop (if you see what i have done with the thumbnail this works ok)
    – anna
    Aug 8, 2017 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.