2011-10-12 93 views
2

我对PHP非常陌生,真的不知道从哪里开始编写它。Wordpress:将函数内部的链接添加到函数中

我发现这个功能的WordPress我使用其中:

function excerpt($limit) { 
    $excerpt = explode(' ', get_the_excerpt(), $limit); 
    if (count($excerpt)>=$limit) { 
    array_pop($excerpt); 
    $excerpt = implode(" ",$excerpt).'<a href="<?php the_permalink(); ?>">Read In Full</a>'; 
    } else { 
    $excerpt = implode(" ",$excerpt); 
    } 
    $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt); 
    return $excerpt; 
} 

function content($limit) { 
    $content = explode(' ', get_the_content(), $limit); 
    if (count($content)>=$limit) { 
    array_pop($content); 
    $content = implode(" ",$content).'[...]'; 
    } else { 
    $content = implode(" ",$content); 
    } 
    $content = preg_replace('/\[.+\]/','', $content); 
    $content = apply_filters('the_content', $content); 
    $content = str_replace(']]>', ']]&gt;', $content); 
    return $content; 

}

在上面的代码中,我放置

"<?php the_permalink(); ?>" 

到HREF。它只是显示为一串文字,而不是创建对帖子链接的调用。

有人帮我解决吗? 谢谢!

+0

你叫这里面的“循环”? http://codex.wordpress.org/The_Loop –

+0

是的,这里是代码: '<?php $ firmnews = new WP_Query(); \t \t \t \t \t $ firmnews-> query('showposts = 2'); ($ firmnews-> have_posts()):$ firmnews-> the_post(); ?>

<?php endwhile; ?> 该函数正在创建 – Devon

+0

摘录中的“Read In Full”链接,尝试用'get_permalink()'替换'the_permalink()'。 the_permalink()回显链接,而get_permalink()将返回链接。在你的情况下,你正在追加链接,所以第二个是首选 – sbrajesh

回答

2

我有这个确切的问题,并发现sbrajesh是正确的 - 我们需要使用get_permalink()。但它只适用于你正确追加php - 当我使用<?php ?>它根本没有处理PHP(只是吐出html)。

这里的工作版本我结束了:echo implode(' ', $words)."<span class='more'><a href='" . get_permalink() . "'>read more</a></span>"; }

0

试试这样说:

$excerpt = implode(" ",$excerpt).'<a href="'<?php the_permalink(); ?>'">Read In Full</a>'; 

你忘了之前和PHP之后把“。希望这可以帮助你。

后来编辑:

我想你的整个代码是写在PHP中。那么我认为你应该尝试:

$excerpt = implode(" ",$excerpt).'<a href="'.the_permalink().'">Read In Full</a>'; 

希望这个时间能工作。

+0

感谢您的回复:D 但它仍然无法正常工作。当我这样做时,它会打破整个功能。 – Devon

+0

那仍​​然是破解功能>< – Devon

+0

然后我就没有想法了。抱歉... – sticksu