0

I am creating a simple event management plugin with the help of custom post type and custom fields. I am now having trouble to display the single post in the front end with the help of permalink. In the shortcode file, I list all the events(POSTS) with few information and a 'view more' link which redirects to the specified permalink of the post. The shortcode file contains the following code, and I want to display all the information of each post in their own permalink page.

<?php

//List events
function et_list_events($atts, $content = null){
    $atts = shortcode_atts([
        'title' => 'My events',
        'category' => 'all',
        'count' => 2,
        'pagination' => 'on',
    ], $atts);
    $pagination = $atts['pagination'] == 'off' ? false : true;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;

    //Check the category attributes
    if ($atts['category'] == 'all'){
        $terms = ' ';
    } else {
        $terms = [
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $atts['category']
        ];
    }

    //Query Args
    $args = [
        'post_type' => 'event',
        'post_status' => 'publish',
        'order_by' => 'start_date',
        'order' => 'ASC',
        'no_found_rows' => $pagination,
        'post_per_page' => $atts['count'],
        'paged' => $paged,
        'tax_query' => $terms
    ];

    //Get the events from database
    $events = new WP_Query($args);

    //Check for events
    if ($events->have_posts()){
        //Get the category slug
        $category  = str_replace('-', ' ', $atts['category']);
        $category = strtolower($category);
        $output = '<div class="event-list">';
        while ($events->have_posts()){
            $events->the_post();
            $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'single-post-thumbnail');
            $output .= '<div class="event-col">';

            $output .= '<img class="feat-img" src="'.$image[0].'" alt="No event featured image is found"/>';
            $output .= '<h5 class="event-title">'.get_the_title().'</h5><br/>';
            $output .= '<a href="'.get_permalink().'">View Details</a>';
            $output .= '</div>';
        }
        $output .= '</div>';

        //Clear Float
        $output .= '<div style="clear: both;"></div>';

        //Reset Post Data
        wp_reset_postdata();

        //Pagination
        if ($events->max_num_pages > 1 and is_page()){
            $output .= '<nav class="prev-next-posts">';
            $output .= '<div class="nav-previous">';
            $output .= get_next_posts_link(__('<span class="meta-nav">&larr;</span> Previous'), $events->max_num_pages);
            $output .= '</div>';

            $output .= '<div class="next-posts-link">';
            $output .= get_previous_posts_link(__('<span class="meta-nav">&rarr;</span> Next'));
            $output .= '</div>';
            $output .= '</nav>';
        }

        return $output;
    } else {
        return '<p>No events found</p>';
    }

}

add_shortcode('events', 'et_list_events');

I have heard about single page template but I didn't have any information about it for plugin. Can anyone tell about, what file should be created in the plugin folder and what code should be written to display all the field values of that post. The field values are:

  1. Details,
  2. Start date,
  3. End date
  4. Start time
  5. End time
  6. Location
0

Your post type name is event so create a copy of your single.php then rename it as single-event.php and keep it together in the same folder with single.php.

Now you can edit single-event.php file to or display whatever you have in your event posts.

Again, copy and rename single.php file to single-event.php. it will serve as single page for event post type.

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.