简体   繁体   中英

wordpress .post() 500 server error

I am still learning Wordpress and I am using .post() function to get the content of a specific post id. when ever I try to echo the ID I have no problem in the output. but when I try to fetch the content the console always throws a 500 (internal server error).

I don't know what I'm doing wrong.

here is my code.

 <div class="container inner-container"> <div class="col-sm-3 col-md-3 sidebar"> <h4 class="bold"><?php echo $cat_name; ?></h4> <hr> <nav class="side-nav"> <ul> <?php $posts=q uery_posts( array( 'post_type'=>'post', 'posts_per_page' => 5, 'order' => 'desc', 'orderby' => 'post_date', 'category_name' => $cat_slug) ); ?> <?php foreach($posts as $p): ?> <li class="view-content" value="<?php echo $p->ID; ?>"> <?php echo $p->post_title; ?><span class="hover-arrow-right"><i class="fa fa-chevron-right"></i></span> </li> <?php endforeach; ?> </ul> </nav> </div> <div class="col-sm-9 col-md-9"> <div class="inner-content"> </div> </div> </div> 

this is the jquery.

 jQuery('.view-content').click(function(event) { var content = jQuery(this).val(); event.preventDefault(); jQuery.post('<?php echo get_template_directory_uri(); ?>/content-from-ajax.php', { content_id: content, }, function(res) { jQuery('.inner-content').html(res); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

and here is the file that i assigned in .post() function.

 <?php $content_id = $_POST['content_id']; echo $content_id; ?> 

thank you in advance.

sorry for my bad engish grammar.

Use this

$html = '';
    $posts= new WP_Query(
     array( 'post_type'=>'post',
            'posts_per_page' => 5,
            'order' => 'desc',
            'orderby' => 'post_date',
            'category_name' => $cat_slug )
         ); ?>
    <?php  while ( $posts->have_posts() ) : $posts->the_post();  
    $html .= '<li class="view-content" value="'.get_the_ID().'">'
              .get_the_title().
           '<span class="hover-arrow-right">
            <i class="fa fa-chevron-right"></i>
          </span>
    </li>';
    endwhile;
  echo $html;
  die();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM