javascript - wordpress .post() 500 服务器错误

标签 javascript php jquery wordpress

我仍在学习 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();

关于javascript - wordpress .post() 500 服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125500/

相关文章:

javascript - 上传前更改文件名 dropzone

javascript - 在 'TbButtonColumn' 中使用 'quickdlgs.EJuiDlgsColumn' 。用于在 Yii 中使用 GridView 中的对话框渲染 View

javascript - 让 bootstrap 4 中表单提交按钮的 5 秒加载微调器正常工作

php - 在搜索数据库中插入分页。 PHP MySQL

jquery - 如何检查div是否存在?

php - 检查当前 WooCommerce 产品是否有图片库来运行 jQuery 脚本

javascript - SVG 对象 - 如何正确缩放和拖动它?

javascript - 如何在 html 中一秒钟后将密码字段显示为星号

php - 将数组保存到数据库中

php - 在 PHP 中捕获重复字母 - 正则表达式