different post-class when template is loaded via ajax

my event-item.php

<?php
/**
 * The event template
 */ 
?>

<li>
    <article id="event-<?php the_ID(); ?>" <?php echo event_canceled($post) ? post_class('wrapper canceled') : post_class('wrapper'); ?>>

This leads to this …

Read More
<article id="event-168" class="post-168 type-wr_event status-publish hentry wrapper wr_event schoenebuecher">

However when loading this event-item.php template via ajax the custom-post-type class of wr_event is not added.

Anyone an idea why that could happen or what could lead to it?

Update:

Ajax:

$.get(
    ajax.url, 
    {
        'action': 'get_event_list',
        'order' : 'DSC',
    }, 
    function( response, status ) {
        $('ul.event-items').append(response);
        }
);

functions.php

add_action('wp_enqueue_scripts', 'theme_name_scripts');

function theme_name_scripts() {

    wp_enqueue_script( 'script-name', get_bloginfo('stylesheet_directory') . '/js/min.js', array(), '1.0.0', false );
    wp_localize_script( 'script-name', 'ajax', array( 'url' => admin_url( 'admin-ajax.php' ) ) );
}

add_action('wp_ajax_get_event_list', 'get_event_list');
add_action('wp_ajax_nopriv_get_event_list', 'get_event_list');

function get_event_list( $latest = true, $order = 'ASC', $return = false, $year = NULL, $cat = '' ) {

Now I got rid of my tag that embeds my script in the header.php file.
The admin-ajax and the embeded script works however doesn’t change anything at the behaviour, still no wr_event in the post-class.

Related posts

1 comment

  1. You have to use this format:

    FUNCTIONS.PHP

    function theme_name_scripts() {
    
        wp_enqueue_script( 'script-name', get_bloginfo('stylesheet_directory') . '/js/min.js', array(), '1.0.0', true );
        wp_localize_script( 'script-name', 'ajax', array( 'url' => admin_url( 'admin-ajax.php' ) ) );
    }
    
    function ajax_callback() {
         get_event_list();
    }
    
    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
    add_action('wp_ajax_ajax', 'ajax_callback');
    add_action('wp_ajax_nopriv_ajax', 'ajax_callback');
    

    AJAX:

    $.get(
        ajax.url, 
        {
            'action': 'ajax',
            'order' : 'DSC',
        }, 
        function( response, status ) {
            $('ul.event-items').append(response);
            }
    );
    

    You may use this line to explicitly include the wr_event class:

     <article id="event-<?php the_ID(); ?>" <?php echo event_canceled($post) ? post_class('wrapper canceled wr_event') : post_class('wrapper wr_event'); ?>>
    

    and also add die(); at the end of the template file to avoid the zero displayed.

Comments are closed.