[英]PHP-OOP How can I pass an anchor tag inside the <p> tag?
With this WordPress related PHP-OOP Answer , I found my solution for what I's seeking. 有了这个与WordPress相关的PHP-OOP答案 ,我找到了自己想要的解决方案。 But with the same function
my_excerpt();
但是具有相同的功能
my_excerpt();
I want to pass another anchor tag ( <a>
) inside the <p>
tag. 我想在
<p>
标记内传递另一个锚标记( <a>
)。
Right now, calling my_excerpt()
is embracing the db texts with a paragraph tag ( <p>here comes the excerpt</p>
). 现在,调用
my_excerpt()
将使用段落标记( <p>here comes the excerpt</p>
) <p>here comes the excerpt</p>
包含数据库文本。 And if I add my anchor tag like the following: 如果我添加如下所示的锚标记:
// Echoes out the excerpt
public static function output() {
the_excerpt(); ?>
<a class="read-more" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php _e( 'Read More »', 'my-theme'); ?></a>
<?php
}
it's showing the "Read More" just the bottom of the except texts. 它在除文本的底部显示“阅读更多”。 Inspect Element shows like the following:
检查元素显示如下:
<p>here comes the excerpt.</p>
<a href="post-link">Read More</a>
How can I make changes to the function or class so that I can have the "Read More" link inside the paragraph, like: 如何更改函数或类,以便可以在段落内使用“阅读更多”链接,例如:
<p>here comes the excerpt.<a href="post-link">Read More</a></p>
Additionally I'm also trying to remove the [...]
part from the excerpt generating by the my_excerpt();
此外,我还试图从
my_excerpt();
生成的摘录中删除[...]
部分my_excerpt();
. 。 So I tried the following:
所以我尝试了以下方法:
function change_excerpt($content) {
$content = str_replace( '[...]','>',$content ); // remove [...], replace with ...
$content = strip_tags( $content ); // remove HTML
return $content;
}
add_filter('my_excerpt','change_excerpt');
I doesn't do anything to the view. 我什么也没做。 But if I change it to:
但是,如果我将其更改为:
add_filter('the_excerpt','change_excerpt');
Then unaware, I get the previously desired anchor tag [inside paragraph], because the filter removed the paragraph tag completely. 然后不知道,我得到了以前想要的锚标记[在段落内部],因为过滤器已完全删除了段落标记。
here comes the excerpt.<a href="post-link">Read More</a>
But it doesn't do anything with [...]
portion. 不过,这并不做任何事情
[...]
部分。 :( :(
So, my furnished Question is: 因此,我提出的问题是:
How can I put the anchor tag inside the paragraph tag, or remove the paragraph tag, and remove the [...]
part from the my_excerpt()
function? 如何将锚标记放入段落标记中,或删除段落标记,然后从
my_excerpt()
函数中删除[...]
部分?
try this snippet 试试这个片段
Include in your functions.php 包含在您的functions.php中
function kc_excerpt( $length_callback = '', $more_callback = '' ) {
if ( function_exists( $length_callback ) )
add_filter( 'excerpt_length', $length_callback );
if ( function_exists( $more_callback ) )
add_filter( 'excerpt_more', $more_callback );
$output = get_the_excerpt();
$output = apply_filters( 'wptexturize', $output );
$output = apply_filters( 'convert_chars', $output );
$output = '<p>' . $output . '</p>';
echo $output;
}
function kc_excerpt_more( $more ) {
return '<a class="read-more" href="'. get_permalink() .'" title="'. get_the_title() .'" rel="bookmark">Read More</a>';
}
function kc_excerpt_more_2($more) {
return '...';
}
function kc_exdefault($length) {
return 10;
}
function kc_ex100($length) {
return 100;
}
And call this function from your template file 然后从您的模板文件中调用此函数
<?php kc_excerpt('kc_exdefault', 'kc_excerpt_more'); ?>
or 要么
<?php kc_excerpt('kc_ex100', 'kc_excerpt_more_2'); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.