【问题标题】:Having trouble acquiring the current posts title获取当前帖子标题时遇到问题
【发布时间】:2015-06-13 20:32:58
【问题描述】:

我目前正在使用 wordpress,并且我有一个链接应该可以播放 wav 文件,具体取决于帖子的标题。所以如果帖子标题是'one',那么它应该从上传文件夹播放one.wav。但是,每个帖子上的声音文件只播放最新的帖子标题。因此,如果我添加了一个名为 two 的帖子,然后添加了一个名为 one 的帖子,这两个帖子都会播放 one.wav

代码如下:

HTML

<span id="dummy"></span>
<a class="playSound" onclick="playSound();" href="#">
  <i class="fa fa-volume-up"></i>
</a>  

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\" />";
            }
        });
}

和 PHP

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

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

我认为get_the_title() 是正确的电话,但在这一点上我不太确定。我已经尝试了所有其他调用功能,但仍然没有运气。我认为此时与加载页面和存储初始帖子标题有关,但此时我只是迷路了。

【问题讨论】:

  • 听起来你的php代码不在the loop
  • 可能。我已将代码更改为仅使用 @poostchi 建议的 javascript。关于如何让它进入循环的任何建议?我假设它在第一篇文章中运行时锁定了该文件名。我对此还是很陌生。
  • 如果必须知道,我的主题函数 js 文件中有函数,content.php 文件中有 html。我认为每个帖子的循环都会遍历 content.php,并认为这是一个安全的地方。

标签: php jquery html ajax wordpress


【解决方案1】:
<?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();
?>

【讨论】:

  • 如果 content.php 在循环中被调用并且我的代码在 content.php 中,这是否意味着它在循环中?还是我必须把它放在别的地方?
  • content.php 不是基本文件 codex.wordpress.org/Theme_Development 所以我不知道那里加载了什么。可能是您的循环在那里运行,也可能不是。但是因为get_the_title() 没有按预期工作,我想那里没有循环。无论如何,我根据@poostchi 更新了我的代码,因为他是对的,你不需要 ajax
  • 想通了!我只是将函数插入到我的主题头文件中,一切正常。感谢您的帮助!
【解决方案2】:

更改您的 sound.php 文件代码,如

<?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;  
  }
?>

【讨论】:

  • 仍然有同样的问题 =( 我的意思是它仍然可以播放,但不是正确的 .wav 文件
【解决方案3】:

您可以简单地使用您的第二个 php 代码并使用 javascript:

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

【讨论】:

  • 这样可以节省我几个小时的时间。不幸的是,它仍在播放最新的帖子 wav 文件
【解决方案4】:

一切都是脱节的。 JS 文件不知道您要定位的帖子,因此 sound.php 文件也不知道。

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

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

例子:

HTML

<a class="playSound" onclick="playSound(<?php echo $post->ID; ?>);" href="#">
  <i class="fa fa-volume-up"></i>
</a>

JS

function playSound(postID) {
  $.ajax({
    url: 'sound.php',
    data: data: {"postID": postID
    type: "GET",
    datatype: "html",
    success: function(data) {
      //Function here
   })
}

PHP

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

或者类似的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2010-12-04
    • 2017-02-06
    • 2018-10-16
    • 2021-09-26
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多