WordPress .post() 500 服务器错误


wordpress .post() 500 server error

我还在学习Wordpress,我正在使用.post()函数来获取特定帖子ID的内容。 当我尝试回显 ID 时,我在输出中没有问题。 但是当我尝试获取内容时,控制台总是抛出 500(内部服务器错误)。

我不知道我做错了什么。

这是我的代码。

<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>

这是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>

这是我在 .post() 函数中分配的文件。

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

提前谢谢你。

对不起,我的英语语法不好。

使用这个

$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();