简体   繁体   中英

PHP errors on get_field() WordPress

I'm trying to get to grips with developing in WordPress. I have a basic understanding of PHP, but I'm aware the syntax is slightly different when developing in WordPress.

Scenario :

To ensure my PHP code works, I ran the following test:

 // define query $args = array('post_type' => 'case-studies' ); $the_query = new WP_Query($args); // The Loop if ( $the_query->have_posts() ) { // check, does the query have posts? echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' .get_the_title() . '</li>'; // spits out page title } echo '</ul>'; /* Restore original Post Data */ wp_reset_postdata(); } else { // no posts found } 

Which works, it echo's the page title on the front end.

Now, In WordPress, I've created a new field group through custom fields :

在此处输入图片说明

In the same file as above, I've written the following

if ( $the_query->have_posts() ) { 
    $case-study-heading = get_field('case_study_heading');
    $case-study-subheading = get_field('case_study_subheading');
    $case-study-backgroung = get_field('case_study_background');
    $case-study-play = get_field('case_study_play_button');

    echo $case-study-heading;
} else {
    echo "no posts";
}

Which shows a Parse error: syntax error, unexpected '=' on this line:

$case-study-heading = get_field('case_study_heading');

Usually in PHP I would do the above query like so:

$query = mysqli_query ("SELECT * FROM table_name"){
    $results = mysqli_fetch_assoc ($query);
        $case-study-heading         = results['field_name'];
        $case-study-subheading      = results['field_name']; 


        if($results!=0) {
                echo "<ul>
                          <<li>$case-study-heading<li>
                          <li>$case-study-subheading</li>
                       </ul>";
        } else{
            echo "no rows in db";
        }
}

Am I executing WP PHP incorrectly? If so, ideas on why I'm getting a parse error?

Ps I've seen the ACF documentation here. I've adapted my code to match it ie $variable = get_field('field_name'); but still get the same error?

Edit : I've amended my WP PHP code after Will's comment:

 if ( $the_query->have_posts() ) { $case_study_heading = get_field('case_study_heading'); $case_study_subheading = get_field('case_study_subheading'); $case_study_backgroung = get_field('case_study_background'); $case_study_play = get_field('case_study_play_button'); echo $case_study_heading; } else { echo "no posts"; } 

Now, upon running this, I don't see any errors (good!) but, nothing shows at all? not even the else , meaning the if statement is working?

I have got text in those fields in WP:

在此处输入图片说明

Ideas?

Try this.

you need to pass the post in get_field in loop. otherwise it will get current page id

if ( $the_query->have_posts() ) { 
    $PostId = get_the_ID();
    $case_study_heading = get_field('case_study_heading', $PostId);
    $case_study_subheading = get_field('case_study_subheading', $PostId);
    $case_study_backgroung = get_field('case_study_background', $PostId);
    $case_study_play = get_field('case_study_play_button', $PostId);

    echo $case_study_heading;
} else {
    echo "no posts";
}

If this is your code

if ( $the_query->have_posts() ) { 
    $case_study_heading = get_field('case_study_heading');
    // etc (for brevity)

    echo $case_study_heading;
} else {
    echo "no posts";
}

then you have not included the loop, so your get_field() s will all be addressing the containing post/page and not the posts within the loop.

You would therefore also need to include the loop:

if ( $the_query->have_posts() ) { 
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $case_study_heading = get_field('case_study_heading');
        // etc (for brevity)

        echo $case_study_heading;
    }
} else {
    echo "no posts";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM