0

I want to create a custom shortcode for every employee/staff member. I've allready got a custom post type plugin that i made, it creates a custom post type page. What i try to achieve is that i can create a shortcode like this [medewerkers id=5] or [medewerkers id=1,2,3,4,5] so i can choose which staff member to place on a certain page. In the following code that i found i only show the title of all members on a page when i type [medewerkers].

<?php
    add_shortcode( 'medewerkers', 'display_custom_post_type' );

    function display_custom_post_type(){
        $args = array(
            'post_type' => 'employee_infos',
            'posts_per_page' => '10',
            'post_status' => 'publish',
            'post_id' => null,
        );

        $string = '';
        $query = new WP_Query( $args );
        if( $query->have_posts() ){
            $string .= '<ul>';
            while( $query->have_posts() ){
                $query->the_post();
                $string .= '<li>' . get_the_title() . '</li>';
                $meta = get_post_meta(get_the_id(), '');

            }
            $string .= '</ul>';
        }
        wp_reset_postdata();
        return $string;
    }
?>

The things that i'd like to show are the title, the content and the thumbnail image. thanks!

Grtz Willem

  • In the WordPress documentation in an good example of how to use the shortcode API with attributes Shortcode API - codex.wordpress.org – user141080 May 8 '18 at 14:24
  • Thanks! I will check this out for further information about my custom shortcode! – Willem Munts May 8 '18 at 14:27
1

Please replace the following code $string .= '<li>' . get_the_title() . '</li>';.

to:

$string .= '<li>';
$string .= '<figure>' . get_the_post_thumbnail( null, 'thumbnail' ) . '</figure>';
$string .= '<h2>' . get_the_title() . '</h2>';
$string .= '<div>' . apply_filters( 'the_content', get_the_content() ) . '</div>';
$string .= '</li>';

Hope it'll work just fine.

| improve this answer | |
  • First of all thanks for your answer! Fatal error: Maximum function nesting level of '256' reached, aborting! It says this when i replace that. And i don't find the strings that much of a problem. I've got for example a post with id:60. The thing i want is that if i type shortcode [medewerkers id='60'] that the post with id:60 shows. Thanks – Willem Munts May 8 '18 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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