用jQuery附加WordPress的帖子标题 [英] append wordpress post title with jquery

查看:83
本文介绍了用jQuery附加WordPress的帖子标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过jquery在选择器之后添加标题<?php the_title(); ?>

i want add the title <?php the_title(); ?> after my selector by jquery

('a img').after('<span>the title here</span>'); 

推荐答案

PHP代码在服务器上解释,而jQuery代码在浏览器中解释(在PHP解释器完成很长时间之后).他们真的看不见对方.

PHP code is interpreted on the server, jQuery code in the browser (long after the PHP interpreter has finished). They can't really see each other.

您可以做的是将the_title()的值放在页面上的某个元素中,然后使用jQuery找到并添加它:

What you can do is put the value of the_title() in some element on the page and then use jQuery to locate it and add it:

var title = $('#the_title').text();
$('a img').after('<span>' + title + '</span>'); 

如果您有多个标题,则必须以某种方式将它们映射到图像.有很多可能的方法可以做到这一点:

If you have several titles, then you must map them to the images somehow. There are many possible ways to do that:

  • 您可以将图像的名称发送到PHP服务器,让其查找标题并将其发送回.查找$().json().

您可以通过将所有标题包装在<div style="display: none;">中,将所有标题放在不可见的HTML中.然后,您可以在jQuery中将图像映射到标题.

You can put all the titles in some piece of HTML that you make invisible by wrapping it in <div style="display: none;">. In your jQuery, you can then map images to titles.

这篇关于用jQuery附加WordPress的帖子标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆