2015-06-03 68 views
-1

我有这样的PHP代码片段:如何在html中正确插入此php代码片段?

<div class="overlay-content"> 
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
<i class="fa fa-search"></i></a> 
</div> 

我想删除:

<i class="fa fa-search"></i> 

,而是,插入title属性,通过PHP,在目前的字体 - 真棒图标称为坐在超链接内。这样,当访问者悬停在项目上时,他们将看到标题而不是Font Awesome搜索图标。

所以我已经试过:

<div class="overlay-content"> 
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
<?php the_title_attribute(); ?></a> 
</div> 

,那就是未成功的原因也许是PHP开发人员明显。我正在尝试做什么?任何指导,以帮助我正确实现这一点将不胜感激!

回答

4

正确的代码,不重复echo,应该如下:

<div class="overlay-content"> 
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
     <?php the_title(); ?> 
    </a> 
</div> 

正如我在我的意见已经提到,the_permalink()the_title_attribute()已经echo内容。无需在每个之前添加另一个echo

您还应该(通常)倾向于the_title()而不是the_title_attribute()以实际显示标题。

+1

虽然我同意,这正是OP有... – jeroen

+2

@ jeroen,的确如此。但是,如果在每个函数之前添加'echo'(但是删除它们不会),那就意味着其他东西是错误的。 – rnevius

+1

是的,但是如何复制OP的确切代码来回答这个问题呢?我必须在这里错过一些东西,因为投票结果都是在这个地方...... – jeroen

-1

你你<?

尝试后失踪=迹象:

<div class="overlay-content"> 
<a href="<?= the_permalink(); ?>" title="<?= the_title_attribute(); ?>"> 
<?= the_title_attribute(); ?></a> 
</div> 
+3

'the_permalink()''已经输出echo'es一样,'the_title_attribute()' – rnevius

+0

https://codex.wordpress.org/Function_Reference/the_title_attribute –

+0

嘿thelastshadow。您的解决方案确实达到了我期待的效果。感谢您花时间回复! –

-1

它看起来对我来说,你需要使用echo

<div class="overlay-content"> 
<a href="<?php echo the_permalink(); ?>" title="<?php echo the_title_attribute(); ?>"> 
<?php echo the_title_attribute(); ?></a> 
</div> 
+1

'the_permalink()'already'echo'es – rnevius

+0

嘿大卫;您的解决方案就像Lamashadow的解决方案一样工作。感谢您花时间回复! –

+0

嗨rnevius,这不取决于上下文吗? HTTPS://codex.wordpress。org/Function_Reference/get_permalink –

0

尝试echo the_title_attribute();。你想实际打印到html中。

+1

该功能已经'回声'。 – rnevius

1

使用<?php echo the_title_attribute('','',false)?>

的第三个参数是echo默认情况下它是true,如果你想在函数返回值,将其设置为false如上图所示。

Wordpress codex

+1

[@rnevious](http://stackoverflow.com/users/3518452/rnevius)建议也是类似的,如果你不想'echo' – Viral