Php 在获取当前帖子标题时遇到问题

Php 在获取当前帖子标题时遇到问题,php,jquery,html,ajax,wordpress,Php,Jquery,Html,Ajax,Wordpress,我目前在wordpress工作,我有一个链接,根据文章的标题播放wav文件。因此,如果帖子标题是'one',那么它应该从uploads文件夹播放one.wav。但是,每篇文章的声音文件只播放最新的文章标题。因此,如果我添加了一个名为two的帖子,然后添加了一个名为one的帖子,那么这两个帖子都将播放one.wav 代码如下: HTML JQuery function playSound() { $.ajax({ url: 'sound.php',

我目前在wordpress工作,我有一个链接,根据文章的标题播放
wav
文件。因此,如果帖子标题是
'one'
,那么它应该从uploads文件夹播放
one.wav
。但是,每篇文章的声音文件只播放最新的文章标题。因此,如果我添加了一个名为
two
的帖子,然后添加了一个名为
one
的帖子,那么这两个帖子都将播放
one.wav

代码如下:

HTML


JQuery

function playSound() {
        $.ajax({
            url: 'sound.php',
            data: "getSound",
            type: "GET",
            datatype: "html",
            success: function(data) {
                document.getElementById("dummy").innerHTML= "<embed src=\""+data+"\" hidden=\"true\" autostart=\"true\"loop=\"false\" />";
            }
        });
}
函数playSound(){
$.ajax({
url:'sound.php',
数据:“getSound”,
键入:“获取”,
数据类型:“html”,
成功:功能(数据){
document.getElementById(“dummy”).innerHTML=“”;
}
});
}
还有PHP

<?php
  require('./wp-blog-header.php');

  if(isset($_GET['getSound'])) {
    $file='wp-content/uploads/2015/wav/' . get_the_title() . '.wav';
    echo $file;  
  }
?>


我假设
get\u title()
是正确的调用,但现在我不太确定。我尝试了所有其他的呼叫功能,但仍然没有成功。我认为这一点与加载页面和存储初始帖子标题有关,但我在这一点上迷失了方向

<?php

require('./wp-blog-header.php');

  global $post;
  setup_postdata( $post );

  if(isset($_GET['getSound'])) {
    $file='wp-content/uploads/2015/wav/' . get_the_title( get_the_ID() ) . '.wav';
    echo $file;  
  }
?>

您只需使用第二个php代码,然后使用javascript即可:

function playSound() {
            document.getElementById("dummy").innerHTML= "<embed src='wp-content/uploads/2015/wav/<?php echo get_the_title( get_the_ID() ) ?>.wav' />";
}
函数playSound(){
document.getElementById(“dummy”).innerHTML=“

函数playSound(){

document.getElementById(“dummy”).innerHTML=“所有内容都是不连贯的。JS文件不知道您要针对什么帖子,因此sound.php文件也不知道

将post ID传递给JS文件,然后将其传递给sound.php文件,然后从中提取post

另外,尽量不要使用文章标题,使用ID作为文章标题,可以更改文章名称

例如:

HTML

PHP


或者类似的东西。

获取标题()需要post_id听起来你的php代码可能超出了范围。我已将代码更改为仅使用@poostchi建议的javascript。关于如何将其放入循环中,有什么建议吗?我假设它在运行第一篇post时锁定了该文件名。我对此还是比较陌生。如果必须知道它,我会在y主题函数js文件和content.php文件中的html。我认为循环在content.php中运行,并假设这是一个安全的地方放置它。仍然有相同的问题=(我的意思是,它仍在播放,但不是正确的.wav文件,这在过去会为我节省几个小时。不幸的是,它仍在播放最新发布的wav文件。如果content.php在循环中调用,而我的代码在content.php中,这是否意味着它在循环中?还是我必须将它放在其他地方?
content.php
不是基本文件,所以我不知道加载了什么。可能是因为您的循环正在那里运行,或者不是。但是因为
get_the_title()
没有按预期工作,我想那里没有循环。无论如何,我基于@poostchi更新了我的代码,因为他是对的,你不需要ajax来完成这项工作。我找到了答案!我只是将该函数插入到我的主题头文件中,一切正常。谢谢你的帮助!
function playSound() {
            document.getElementById("dummy").innerHTML= "<embed src='wp-content/uploads/2015/wav/<?php echo get_the_title( get_the_ID() ) ?>.wav' />";
}
<?php 
$sound_query = new WP_Query();
    if ($sound_query->have_posts()):
        while ($sound_query->have_posts()):
        $sound_query->the_post();
?>
<script>
function playSound() {
   document.getElementById("dummy").innerHTML= "<embed src='wp-content/uploads/2015/wav/<?php echo get_the_title() ?>.wav' />";
}
</script>

<?php 
    endwhile;
endif;
wp_reset_postdata();
?>
<a class="playSound" onclick="playSound(<?php echo $post->ID; ?>);" href="#">
  <i class="fa fa-volume-up"></i>
</a>
function playSound(postID) {
  $.ajax({
    url: 'sound.php',
    data: data: {"postID": postID
    type: "GET",
    datatype: "html",
    success: function(data) {
      //Function here
   })
}
if(isset($_GET['postID'])) {
  $file='wp-content/uploads/2015/wav/' . $_GET['postID'] . '.wav';
  echo $file;  
}