Avatar of drgleockler
drgleockler
Flag for United States of America asked on

PHP - Wordpress Syntax error

Hello all,
I am hoping (begging!) for a little help with my syntax here ;)
I am using Types Custom fields and would like the data to display through another plugin...I have mostly figured out how to get the data for each field that has been filled in, but am STUCK when it comes to the syntax in this area to insert a link if the data exists, and apply a class to it. Here's what I have so far:
function ajax_result($arg, $id,$pagenumber){
		   $apiclass = new ajaxwpqsfclass();
		  // The Query
			$query = new WP_Query( $arg );
			$html ='';
			// The Loop
		if ( $query->have_posts() ) {
			$html .= '<h1>'.__('Search Results :', 'AjWPQSF' ).'</h1>';
			while ( $query->have_posts() ) {
				$query->the_post();
					$html .= '<h2 class="entry-title">'.get_the_title().'</h2>';
					$html .= '<div class="one_third">';
					$html .= '<article>'.get_the_post_thumbnail($post_id, 'large').'';
					$html .= '</div></article>';
					$html .= '<div class="two_third last">'.get_the_content().'';
					$html .= '<article>'.types_render_field("address", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("park-location", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("allowable-uses", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("zoning", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("total-size", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("total-acreage", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("max-ceiling-height", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("rail-served", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article>'.types_render_field("pdf-summary", array("output"=>"HTML","show_name"=>"true"));'</article>';
					$html .= '<article><a class="button small default" href="'.types_render_field("contact-cedc", array("output"=>"raw")).'">'.get_the_title().'</a></article>';
					$html .= '</div>';
					$html .= '<div style="clear:both;"></div>';
					$html .= '<div style="margin-top:40px;margin-bottom:20px" class="demo-sep sep-single"></div>';

			}

			$html .= $apiclass->ajax_pagination($pagenumber,$query->max_num_pages, 4, $id);
		 } else {
					$html .= __( '<h2>No Properties Match Your Criteria</h2><br/>¿Please search again', 'AjWPQSF' );
				}
				/* Restore original Post Data */
				wp_reset_postdata();

			return $html;

	   }	

	}//end class

Open in new window

The problem area is here:
$html .= '<article><a class="button small default" href="'.types_render_field("contact-cedc", array("output"=>"raw")).'">'.get_the_title().'</a></article>';

Open in new window


as you can see.....I am obviously a php idiot, and would GREATLY appreciate any help anyone could offer!

The desired output would be a button that displays the words "Contact CEDC" (which is the field name)which links to the url entered via the custom field "contact-cedc" (which is a single line text field), only if that field has data in it. (I can change this to a url field if necessary.)

PLEASE, any help would be very much appreciated!!!
Thank you so much!
The url where you can see the above code in action is here :

http://www.carolinebusiness.com/newsite/property-search/
then click the "Search" button.

Thanks!
WordPressPHP

Avatar of undefined
Last Comment
drgleockler

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
James Rodgers

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
drgleockler

ASKER
You're awesome!!!
Thank you so much!
One last question....any idea how to tell it to only display the link if the custom field has a value?  So that it's not displaying the link for empty fields?
You completely made my f*****g night!
James Rodgers

before you set
$html .= '<h1>'.__('Search Results :', 'AjWPQSF' ).'</h1>';

add a line

if  ($customfield != ''){

//close after the else
}

where $customfield is the field value you want to check
drgleockler

ASKER
Thank you so much for this, but I think I need more details (I'm really clueless with php...sorry!)

Here's the existing code:

function ajax_result($arg, $id,$pagenumber){
           $apiclass = new ajaxwpqsfclass();
          // The Query
            $query = new WP_Query( $arg );
            $html ='';
            // The Loop
        if ( $query->have_posts() ) {
			
					$html .= '<div class="post-content"><div class="title"><h2>'.__('Search Results :', 'AjWPQSF' ).'</h2><div class="title-sep-container"><div class="title-sep"></div></div></div></div>';
            while ( $query->have_posts() ) {
                $query->the_post();
                    $html .= '<h2 class="entry-title">'.get_the_title().'</h2>';
                    $html .= '<div class="one_third">';
                    $html .= '<article>'.get_the_post_thumbnail($post_id, 'large').'';
                    $html .= '</div></article>';
                    $html .= '<div class="two_third last">'.get_the_content().'';
                    $html .= '<article>'.types_render_field("address", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("park-location", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("allowable-uses", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("zoning", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("total-size", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("total-acreage", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("max-ceiling-height", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article>'.types_render_field("rail-served", array("output"=>"HTML","show_name"=>"true")).'</article>';
                    $html .= '<article><a class="button small default" href="'.types_render_field("pdf-summary", array("output"=>"raw")).'">Download PDF</a><span style="padding-right:10px;"></span><a class="button small default" href="'.types_render_field("contact-cedc", array("output"=>"raw")).'">Contact CEDC</a></article>';
                    $html .= '</div>';
                    $html .= '<div style="clear:both;"></div>';
                    $html .= '<div style="margin-top:40px;margin-bottom:20px" class="demo-sep sep-single"></div>';
 
            }
 
            $html .= $apiclass->ajax_pagination($pagenumber,$query->max_num_pages, 4, $id);
         } else {
                    $html .= __( '<h2>No Properties Match Your Criteria</h2><br/>?Please search again', 'AjWPQSF' );
                }
                /* Restore original Post Data */
                wp_reset_postdata();
 
            return $html;
 
       }    
 
    }//end class
	
}// end if class

Open in new window


Right now, that code displays both "buttons" at the end of the description, one that says "Download PDF" one that says "Contact CEDC."

The Contact field should always be set, so I don't need an if-then for that, but some properties have a PDF summary that has been uploaded into a custom field "pdf-summary" and some properties don't.

My issue is that the above code displays the link to download the PDF when the PDF doesn't exist.  I would like the button to show up exactly how it does above, but only if there's an uploaded file into the custom field "pdf-summary."

If there hasn't been a pdf uploaded, I want the button"download the pdf" to disappear and just keep the contact button as is.

Could you possibly let me know exactly what to insert in the above section to achieve that result?  I'm unclear specifically on the "else" part of your previous instructions.  Do I have to tell it what to do in both cases? If....then do this (all of the above parameters) else (do all parameters except the pdf summary part)???

I cannot tell you how much I appreciate all of your help!
Thanks so much...
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck