$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Post source link plugin - small modification|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Post source link plugin - small modification

matteradmin8PV0评论

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: .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

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.sstatic/qOt5T.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

Share Improve this question asked Dec 3, 2018 at 18:49 kacper3355kacper3355 772 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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
?>
Post a comment

comment list (0)

  1. No comments so far