我知道这段代码不正确,但我想将HTML和PHP注入下面标签的标题中。
<p class="related_articles" title="<a href='<?php the_field(related_articles_1_link); ?>' target="_blank"><?php echo the_field(related_articles_1_title); ?></a>"Related Articles</p>
我使用的是jQuery工具提示插件(http://flowplayer.org/tools/tooltip/index.html)从title标记中获取内容并将其显示为工具提示内容。我有高级自定义字段设置(WordPress插件(,允许我发布自定义字段内容。实际上,我在这些自定义字段中发布的内容最终会出现在工具提示中。
我的目标是当用户悬停在"相关文章"上时生成一个工具提示,显示一个可点击的链接。同样,上面的jQuery工具提示插件从标题中获取内容,这就是为什么这会给
好的,因为我们已经了解了您要做的事情。让我们把一些事情弄清楚。
<p class="related_articles" title="<a href='<?php the_field(related_articles_1_link); ?>' target="_blank"><?php echo the_field(related_articles_1_title); ?></a>"Related Articles</p>
在这么多层面上都错了吗。首先,它在任何方面都是无效的。其次,你并没有结束你的<a>
。您还缺少一个回声和target=""
,在title=""
内部未转义:target='"'"
。
因此,简而言之,直接回答你的问题,这可能会奏效(也许,因为它非常罕见和不标准(
<p class="related_articles" title="<a href='"<?php echo the_field(related_articles_1_link); ?>'" target='"_blank'"><?php echo the_field(related_articles_1_title); ?></a>">Related Articles</p>
此外,正如其中一位用户已经提到的那样。如果您的服务器服务器启用短的打开标签,那么您可以将<?php echo $foo; ?>
缩短:<?= $foo ?>
。所以在你的代码情况下,它看起来像:
<p class="related_articles" title="<a href='"<?= the_field(related_articles_1_link) ?>'" target='"_blank'"><?= the_field(related_articles_1_title) ?></a>">Related Articles</p>
但是,可能已经提到了。这不是推荐的方法,可能会产生各种问题。我建议研究一个更好的解决方案。
如果服务器支持,请使用缩写形式:
<?=the_field(related_articles_1_link)?>
否则,你应该回应它:
<?php echo the_field(related_articles_1_link); ?>
<p class="related_articles" title="<?php echo htmlspecialchars('<a href="'. the_field(related_articles_1_link) .'">'. the_field(related_articles_1_title) .'</a>'); ?>">Related Articles</p>
虽然这应该有效,但我不确定您提到的jQuery插件是否能够将title属性中的给定html解释为html(您需要测试它(。这很可能会被解释为文本,所有标签对最终用户都是可见的,但这不是您想要的。
我发现了一个如何以不同方式实现此功能的示例:http://flowplayer.org/tools/demos/tooltip/any-html.html
这可能是您想要的吗?
<p class="related_articles">
Related Articles:
<a href="<?php echo the_field(related_articles_1_link); ?>" target="_blank">
<?php echo the_field(related_articles_1_title); ?>
</a>
</p>
既然您已经澄清了您的问题是关于jQuery Tooltip插件的,那么您应该执行以下操作:
<p class="related_articles">Related Articles</p>
<a class="tooltip" href="<?php the_field(related_articles_1_link); ?>" target="_blank">
<?php echo the_field(related_articles_1_title); ?>
</a>
Javascript:
$('.related_articles').tooltip();
这样做的原因:
工具提示插件会查看应用.tooltip()
的元素之后的元素。如果下一个元素的类名为tooltip
,它会将其用作工具提示的内容(而不是title属性(。
如果您将html内容放置在html属性中,则应该对其进行转义。正如@Karolis所提到的那样:
<p class="related_articles" title="<?php echo htmlentities('<a href="'.the_field(related_articles_1_link).'" target="_blank">'.the_field(related_articles_1_title).'</a>'); ?>">Related Articles</p>
这应该会生成一个包含链接的工具提示。如果您看到类似"<;a href=">;"在工具提示中,插件没有对html值进行unescape。您可以对值进行unescape,但遗憾的是javascript没有功能。为此,您可以查看php.js它在js中提供php的函数,但这似乎有些过头了。你可以替换像这样麻烦的字符:
<?php
// Generate complete tootltip content
$tootltipContent = '<a href="'.the_field(related_articles_link_1).'">'.the_field(related_articles_link_1).'</a>';
// Search for these
$search = array('<','>','"');
// And replace them with these
$replacements = array('@#','@#',''"');
$tooltipContent = str_replace($search,$replacements,$tooltipContent);
?>
<p class="related_articles" title="<?php echo $tooltipContent; ?>">Related Stuff</p>
然后在插件中查找从元素中提取标题的行。(提示:在插件源代码上搜索"title"。应该有一个地方提取值(它可能看起来像这个tooltipContent=$(someVar(.attr("title"(;现在在js:中运行反向替换
tooltipContent = tooltipContent.replace('@#','<').replace('#@','>').replace(''"','"');