• Hi,
    I would like to retrieve the attachments through admin_ajax but doesn’t work…
    Any idea?

    this is the code:
    I got the title and all other fields of the post…
    but not the attachements

    function custom_script() {
        wp_register_script( 'custom-script', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ) );
    	wp_localize_script( 'custom-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234  ) );
        wp_enqueue_script( 'custom-script' );
    }
    add_action( 'wp_enqueue_scripts', 'custom_script' );
    
    add_action( 'wp_ajax_my_action', 'call_custom_post' );
    add_action( 'wp_ajax_nopriv_my_action', 'call_custom_post' );
    
    function call_custom_post(){
    	$post_ID=isset($_POST['post_ID']) ? $_POST['post_ID'] : 0;
    	if($post_ID>0){
    		$categories=get_the_category($post_ID);
    		$recent = new WP_Query("p=$post_ID"); while($recent->have_posts()) : $recent->the_post();
    	?>
    		<h4><span><?php the_title(); ?></span></h4>
    		<?php
    		$attachments = new Attachments( 'attachments');
    		if( $attachments->exist() ) {
    		?>
    			<table cellpadding="0" cellspacing="0" width="100%">
    				<?php while( $attachments->get() ) : ?>
    				<tr>
    					<td><a href="<?php echo $attachments->url(); ?>" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/icon.<?php echo $attachments->type(); ?>" alt="Download" /></a></td>
    					<td><?php echo $attachments->field( 'data_stipulata' ); ?></td>
    					<td><?php echo $attachments->field( 'decorrenza' ); ?></td>
    					<td><?php echo $attachments->field( 'scadenza' ); ?></td>
    				</tr>
    				<?php endwhile; ?>
    			</table>
    		<?php
    		}else{
    		?>
    			<p>Nessun documento disponibile.</p>
    		<?php
    		}
    		endwhile;
    	}
    	die;
    }

    https://ww.wp.xz.cn/plugins/attachments/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Retrieve attachements in function.php and through admin_ajax’ is closed to new replies.