I'm using a little plugin that allows me to add source link to every article. I found the code in this thread: Article source link for posts

It's almost perfect, but I need to make 2 little modifications:

  1. When there's no source link, the word "Source:" is still showing up (here's how it looks like: https://i.imgur.com/orY1Msf.png), but I need to get rid of it if no source link is added. Does anyone have any idea how to do it?

  2. Source link shows on every page of a post. I have some posts splitted into multiple pages, but the link still shows on each one of them. I need to change this - the link has to show up only on the last page of a splitted post. Any ideas how to achieve that?

Here's the code I'm using (pasted in functions.php): Article source link for posts - the first one

And here's what I have in my single.php (single post):

<div class="source-link">Source: <?php echo esc_url( get_post_meta( $post->ID, '_source_link', true ) ); ?></div>

Thanks in advance. Im desperate for help.

Kacper

up vote 0 down vote accepted

You can do it like so: (use this in place of the one in the question)

<?php
global $post, $pages, $page;

$total = count( $pages );
// Show if there's only one page, or that we're on the last page.
if ( $total < 2 || $page === $total ) :
    // Show if the source link was specified.
    if ( $url = get_post_meta( $post->ID, '_source_link', true ) ) :
    ?>
        <div class="source-link">
            Source: <?php echo esc_url( $url ); ?>
        </div>
    <?php endif; // end $url
endif; // end last page check
?>
  • Thank you! Can you just tell me where should I paste it? I don't wanna to break the site (I'm a beginner) – kacper3355 Dec 4 at 1:09
  • Hi. Please check the updated answer. – Sally CJ Dec 4 at 1:36
  • It's perfect! Thank you so much :) – kacper3355 Dec 4 at 1:50
  • You're welcome, and I appreciate it if you could accept the answer. :) – Sally CJ Dec 4 at 1:55
  • Done :) Thank you once again! – kacper3355 Dec 4 at 1:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.