Categories

Linkify Posts

Author: Scott Reilly
Version: 2.3.5
First released: 2010-08-28
Last update: 2020-08-18
Compatibility: WP 3.3 – 5.5.1
Download: [ zip ]
Description:

Turn a string, list, or array of post IDs and/or slugs into a list of…

Extended Description

The plugin provides a widget called “Linkify Posts” as well as a template tag, c2c_linkify_posts(), which allow you to easily specify posts to list and how to list them. Posts are specified by either ID or slug. See other parts of the documentation for example usage and capabilities.

Particularly handy when used in conjunction with the post custom field feature of WordPress. You could define a custom field for “Related Posts” or “Additional Products” and manually list out post IDs, then utilize the function provided by this plugin to display links to those posts in a custom manner.

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Template Tags

The plugin provides one template tag for use in your theme templates, functions.php, or plugins.

Functions

  • <?php c2c_linkify_posts($posts, $before = '', $after = '', $between = ', ', $before_last = '', $none = '') ?>
    Displays links to each of any number of posts specified via post IDs

Arguments

  • $posts
    A single post ID/slug, or multiple post IDs/slugs defined via an array, or multiple posts IDs/slugs defined via a comma-separated and/or space-separated string

  • $before
    (optional) To appear before the entire post listing (if posts exist or if ‘none’ setting is specified)

  • $after
    (optional) To appear after the entire post listing (if posts exist or if ‘none’ setting is specified)

  • $between
    (optional) To appear between posts

  • $before_last
    (optional) To appear between the second-to-last and last element, if not specified, ‘between’ value is used

  • $none
    (optional) To appear when no posts have been found. If blank, then the entire function doesn’t display anything

Examples

  • These are all valid calls:

  • <?php c2c_linkify_posts("43 92"); ?>

Outputs something like:

<a href="https://example.com/archive/2008/01/15/some-post">Some Post</a>,
<a href="https://example.com/archive/2008/01/15/another-post">Another Post</a>
  • Assume that you have a custom field with a key of “Related Posts” that happens to have a value of “43 92” defined (and you’re in-the-loop).

    ID, ‘Related Posts’, true), “Related posts: “); ?>

Outputs something like:

Related posts: <a href="https://example.com/archive/2008/01/15/some-post">Some Post</a>,
<a href="https://example.com/archive/2008/01/15/another-post">Another Post</a>
  • <ul><?php c2c_linkify_posts("43, 92", "<li>", "</li>", "</li><li>"); ?></ul>

Outputs something like:

<ul><li><a href="https://example.com/archive/2008/01/15/some-post">Some Post</a></li>
<li><a href="https://example.com/archive/2008/01/15/another-post">Another Post</a></li></ul>
  • <?php c2c_linkify_posts(""); // Assume you passed an empty string as the first value ?>

Displays nothing.

  • <?php c2c_linkify_posts("", "", "", "", "", "No posts found."); // Assume you passed an empty string as the first value ?>

Outputs:

No posts found.<h3>Hooks</h3>

The plugin exposes one action for hooking.

c2c_linkify_posts (action)

The ‘c2c_linkify_posts’ hook allows you to use an alternative approach to safely invoke c2c_linkify_posts() in such a way that if the plugin were to be deactivated or deleted, then your calls to the function won’t cause errors in your site.

Arguments:

  • same as for c2c_linkify_posts()

Example:

Instead of:

<?php c2c_linkify_posts( "112, 176", 'Posts: ' ); ?>

Do:

<?php do_action( 'c2c_linkify_posts', "112, 176", 'Posts: ' ); ?>

Find out more at the plugin’s WordPress Plugin Repository page.

Screenshots

Click to see full-size image.

  1. The plugin's widget configuration.

    The plugin’s widget configuration.

Installation

  1. Install via the built-in WordPress plugin installer. Or download and unzip linkify-posts.zip inside the plugins directory for your site (typically wp-content/plugins/)
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. Optional: Use the c2c_linkify_posts() template tag in one of your templates (be sure to pass it at least the first argument indicating what post IDs and/or slugs to linkify — the argument can be an array, a space-separate list, or a comma-separated list). Other optional arguments are available to customize the output.
  4. Optional: Use the “Linkify Posts” widget in one of the sidebar provided by your theme.

Frequently Asked Questions

Q. What happens if I tell it to list something that I have mistyped, haven’t created yet, or have deleted?
A. If a given ID/slug doesn’t match up with an existing post then that item is ignored without error.

Q. How do I get items to appear as a list (using HTML tags)?
A. Whether you use the template tag or the widget, specify the following information for the appropriate fields/arguments:

  • Before text: <ul><li> (or <ol><li>)
  • After text: </li></ul> (or </li></ol>)
  • Between posts: </li><li>

Q. Does this plugin include unit tests?
A. Yes.

Release Log

2.3.5 (2020-08-17)

  • New: Add TODO.md and move existing TODO list from top of main plugin file into it (and add another item)
  • Change: Restructure unit test file structure
    • New: Create new subdirectory phpunit/ to house all files related to unit testing
    • Change: Move bin/ to phpunit/bin/
    • Change: Move tests/bootstrap.php to phpunit/
    • Change: Move tests/ to phpunit/tests/
    • Change: Rename phpunit.xml to phpunit.xml.dist per best practices
  • Change: Note compatibility through WP 5.5+

2.3.4 (2020-05-07)

  • Change: Use HTTPS for link to WP SVN repository in bin script for configuring unit tests
  • Change: Note compatibility through WP 5.4+
  • Change: Update links to coffee2code.com to be HTTPS
  • Change: Update examples in documentation to use a proper example URL

2.3.3 (2019-11-27)

  • New: Add CHANGELOG.md and move all but most recent changelog entries into it
  • Change: Update unit test install script and bootstrap to use latest WP unit test repo
  • Change: Note compatibility through WP 5.3+
  • Change: Update copyright date (2020)
  • Change: Split paragraph in README.md’s “Support” section into two

Copyright & Disclaimer

Copyright © 2010-2020 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.

2 replies on “Linkify Posts”

Leave a Reply

Your email address will not be published.