0

I've created a new custom post type called doctors. I've been using ACF and pulling these fields to help create my banner. I was wondering what a post would look like if no fields were filled out, so I created a second test post, but I noticed it has all of the same content as my first post.

I feel like I'm writing my function incorrectly, obviously.

function doctor_banner_shortcode() {
    $args = array(
        'posts_per_page'    => 1,
        'post_type'     => 'doctors',
        'post_status'       => 'publish'
    );
    $doctors_query = new WP_Query( $args );
    if ( $doctors_query->have_posts() ) :
        while ( $doctors_query->have_posts() ) :
            $doctors_query->the_post();

            $dr_name = get_field( 'practitioner_name' );
            $dr_degree = get_field( 'practitioner_title' );
            $dr_bio = get_field( 'practitioner_short_bio' );
            $dr_cv = get_field( 'practitioner_cv' );

            $html_out = '<h1>Dr. ' . $dr_name . '</h1>
                         <h3">' . $dr_degree . '</h3>
                         <p>' . $dr_bio . '</p>
                         <a href="' . $dr_cv . '">' . 'Read CV' . '</a>';
        endwhile;
    else : // No results
        $html_out = "No Doctors Found.";
    endif;
    wp_reset_query();
    return $html_out;
}

add_shortcode( 'doctor_banner', 'doctor_banner_shortcode' );

Somehow, I think, it's not reading the proper ID of that post to output the correct data. I have two links for my two posts here and here.

2 Answers 2

1

You are querying the entire posts of doctors type, and setting the number of posts per page to one. This will obviously always return the same post.

You need to either set the id of the post, or navigate through the loop to find a match. If you are calling this function in single post page, to filter the results based on post id, you should use the function this way:

function doctor_banner_shortcode($doctor_id) {
$args = array(
    'posts_per_page'    => 1,
    'post_type'     => 'doctors',
    'post_status'       => 'publish',
    'p'        => $doctor_id
);
$doctors_query = new WP_Query( $args );
if ( $doctors_query->have_posts() ) :
    while ( $doctors_query->have_posts() ) :
        $doctors_query->the_post();

        $dr_name = get_field( 'practitioner_name' );
        $dr_degree = get_field( 'practitioner_title' );
        $dr_bio = get_field( 'practitioner_short_bio' );
        $dr_cv = get_field( 'practitioner_cv' );

        $html_out = '<h1>Dr. ' . $dr_name . '</h1>
                     <h3">' . $dr_degree . '</h3>
                     <p>' . $dr_bio . '</p>
                     <a href="' . $dr_cv . '">' . 'Read CV' . '</a>';
    endwhile;
else : // No results
    $html_out = "No Doctors Found.";
endif;
wp_reset_query();
return $html_out;
}

add_shortcode( 'doctor_banner', 'doctor_banner_shortcode' );

Then you can call your function in the single.php template in this way : function doctor_banner_shortcode($post->ID)

If you want to call the function manually in some other page, you should set the ID while calling the function.

0

It looks like you are not supplying the ID into the loop, forcing it to get the first item chronologically (which is always going to be the latest, regardless of where you call the shortcode).

If your shorcode runs inside the_content you don't even have to run another loop, just use the get_field directly.

function doctor_banner_shortcode() {

    $dr_name = get_field( 'practitioner_name' );
    $dr_degree = get_field( 'practitioner_title' );
    $dr_bio = get_field( 'practitioner_short_bio' );
    $dr_cv = get_field( 'practitioner_cv' );

    $html_out = '<h1>Dr. ' . $dr_name . '</h1>
                 <h3">' . $dr_degree . '</h3>
                 <p>' . $dr_bio . '</p>
                 <a href="' . $dr_cv . '">' . 'Read CV' . '</a>';

    return $html_out;
}

Lemme know if it works!

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.