简体   繁体   English

PHP-OOP如何在内部传递锚标记 <p> 标签?

[英]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 &raquo;', '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 另外

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.

相关问题 如何将 $_POST/$_GET 处理代码与 html 表单/url 分离? (PHP-面向对象) - How can I decouple $_POST/$_GET handling code from html forms/urls? (PHP-OOP) PHP,CakePHP,HTML:锚标签附加在代码标签内,如何分隔此锚标签? - PHP,CakePHP,HTML: The anchor tag is appended inside a code tag, how can seperate this anchor tag? 如何在内部提取ul标签内容和p标签内容 <dd> 标记在PHP中使用常规表达 - How can i extract ul tag content and p tag content inside <dd> tag using regular expresstion in php 在php循环内的锚标记中传递php变量 - pass a php variable in an anchor tag inside a php loop 如何在php的锚标签中插入alt标签? - how to insert alt tag inside anchor tag in php? 如何仅在PHP中获得ap标签的alt属性? - How can I only get the alt attribute of the a p tag in PHP? 如何在锚标记中提取所有img标记? - How can I extract all img tag within an anchor tag? 在提交时,我需要在php中传递锚标记值 - On submit i need to pass anchor tag value in php 如何在锚标签href中将php值作为参数传递 - how to pass php value as parameter in anchor tag href 我如何在onclick函数中传递3个参数。这些参数位于php标签内 - How can i pass 3 parameters in onclick function .The parameters are inside php tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM