• davidortiz34

    (@davidortiz34)


    Hello I’m trying to turn a function into a shortcode. I’ve read how to create a shortcode however, when I turn my function into a shortcode I get a different output.

    Basically whenever you click on an area of focus all teachers associated with that area will show .

    The code below works perfectly.

    `<?php $schools = $post->ID; // the current post id ?>

    <?php
    $args = array(
    ‘post_type’ => ‘teacher’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘areas_of_focus’,
    ‘value’ => $schools,
    ‘compare’ => ‘LIKE’,
    ),
    ),
    );

    $schools_data_query = new WP_Query($args);

    ?>

    <?php

    if ( $schools_data_query->have_posts() ) {
    echo ‘<ul>’;
    while ( $schools_data_query->have_posts() ) {
    $schools_data_query->the_post();
    //echo ‘<li>’ . get_the_title() . ‘</li>’;
    //echo ‘<li>’ . get_permalink() . ‘</li>’; ?>
    <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>

    <?php
    }
    echo ‘</ul>’;
    } else {
    // no posts found
    }`

    Now I want to turn it into a shortcode. Here is what I have so far

    ` function gm_list_teacher_shortcode($atts){
    $args = array(
    ‘post_type’ => ‘teacher’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘areas_of_focus’,
    ‘value’ => $schools,
    ‘compare’ => ‘LIKE’,
    ),
    ),
    );

    $schools_data_query = new WP_Query($args);

    global $post;
    $schools = $post->ID;

    $content = ”;
    $content .= ‘<ul>’;

    while($schools_data_query->have_posts()) : $schools_data_query->the_post();

    $content .= ‘<li><a href=”‘.get_permalink().'”>’.get_the_title().'</a></li>’;

    endwhile;

    $content .= ‘</ul>’;

    wp_reset_query();

    return $content;
    }

    add_shortcode(‘gm_list_teacher’, ‘gm_list_teacher_shortcode’);`

    However when I use this shortcode no matter what area of focus I click on the same teachers show. I believe when creating the shortcode I’m no addressing the following line correctly

    $schools = $post->ID;

    Any help would be really appreciated. I’m in no way a great programmer. Thank you

  • The topic ‘Creating a shortcode in WordPress’ is closed to new replies.