the content - Get link value only from the_content()?

I have my news in posts and I am trying to display the two latest news articles (posts) on my main page. I've got t

I have my news in posts and I am trying to display the two latest news articles (posts) on my main page. I've got that solved by getting the title using the_title() and getting a little bit of the text using the_excerpt().

I want to show a more... link that takes you to the post. However, some of the news articles that I am posting have some text in the post body that's a link, how do I only get the link within the body of a post so that my more... link takes your there directly?

For example, sometimes we will link to news articles in PDFs that are on another server.

I'm using the following code.

<?php query_posts('category_name=news2011'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php /* the_ID(); */ ?> 
    <?php /* the_date('F Y'); */ ?>
    <ol class="news">
        <strong><p><?php the_title()?></p></strong>
        <li><?php the_content(); ?></li>
    </ol>
<?php endwhile; endif; ?>

I have my news in posts and I am trying to display the two latest news articles (posts) on my main page. I've got that solved by getting the title using the_title() and getting a little bit of the text using the_excerpt().

I want to show a more... link that takes you to the post. However, some of the news articles that I am posting have some text in the post body that's a link, how do I only get the link within the body of a post so that my more... link takes your there directly?

For example, sometimes we will link to news articles in PDFs that are on another server.

I'm using the following code.

<?php query_posts('category_name=news2011'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php /* the_ID(); */ ?> 
    <?php /* the_date('F Y'); */ ?>
    <ol class="news">
        <strong><p><?php the_title()?></p></strong>
        <li><?php the_content(); ?></li>
    </ol>
<?php endwhile; endif; ?>
Share Improve this question edited Apr 12, 2013 at 4:41 s_ha_dum 65.6k13 gold badges84 silver badges174 bronze badges asked Mar 29, 2011 at 21:59 BetoBeto 1712 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can scan the content to see whether it contains a link and then parse it to find the href attribute. There are many ways to do this, this example uses the built-in kses functionality, as demonstrated by Otto:

$post_link = get_the_permalink();
if ( preg_match('/<a (.+?)>/', get_the_content(), $match) ) {
    $link = array();
    foreach ( wp_kses_hair($match[1], array('http')) as $attr) {
        $link[$attr['name']] = $attr['value'];
    }
    $post_link = $link['href'];
}

The above solution is perfect but it will only take http links. By using the below code it will take both http and https links.

$post_link = get_the_permalink();
if ( preg_match('/<a (.+?)>/', get_the_content(), $match) ) {
    $link = array();
    foreach ( wp_kses_hair($match[1], array('https','http')) as $attr) {
        $link[$attr['name']] = $attr['value'];
    }
    $post_link = $link['href'];
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744937529a4602117.html

相关推荐

  • the content - Get link value only from the_content()?

    I have my news in posts and I am trying to display the two latest news articles (posts) on my main page. I've got t

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信