Change Portfolio Link to Custom Link

Good afternoon,

I was able to answer part of my question in the forum from this thread: https://theme.co/apex/forums/topic/portfolio-item-page-layout-problem-no-fullwidth-band-possible/#post-37259

However… I have the portfolio items located on my home page, as well as my events calendar page, and when I click the image it doesn’t direct to the custom URL I want it to go to, like it does on the portfolio page.

Here is the page where it’s acting how I want:
http://gnplc.ca/jana/upcoming-programs/
When you click on one of the programs, it links to the page I want it to - that’s great.

However, here, on the home page, it does not. It links to the portfolio info page:
http://gnplc.ca/jana/

Any help would be greatly appreciated!!

Hello @janaroberts,

Thanks for writing in!

The given code will only apply to the portfolio page or in the http://gnplc.ca/jana/upcoming-programs/ in your case.
The code does not apply to the recent posts element which you have used in your homepage.

Please have the code updated and use this instead:

add_filter( 'post_type_link', 'x_remap_portfolio_permalink_to_project_link', 10, 4);
add_filter( 'post_link', 'x_remap_portfolio_permalink_to_project_link', 10, 4);

function x_remap_portfolio_permalink_to_project_link($post_link, $post, $leavename, $sample) {

    if ('x-portfolio' == $post->post_type) {
        $custom = get_post_custom($post->id);

        if (isset($custom['_x_portfolio_project_link']) && $custom['_x_portfolio_project_link'][0]!='')
            return $custom['_x_portfolio_project_link'][0];
    }

    return $post_link;
}

We would love to know if this has worked for you. Thank you.

Good morning!

I tried out the code provided in my child theme functions.php and I received the following error:

Your PHP code changes were rolled back due to an error on line 28 of file wp-content/themes/x-child/functions.php. Please fix and try saving again.

Uncaught ArgumentCountError: Too few arguments to function x_remap_portfolio_permalink_to_project_link(), 3 passed in wp-includes/class-wp-hook.php on line 286 and exactly 4 expected in wp-content/themes/x-child/functions.php:28
Stack trace:
#0 wp-includes/class-wp-hook.php(286): x_remap_portfolio_permalink_to_project_link(‘http://gnplc.ca…’, Object(WP_Post), false)
#1 wp-includes/plugin.php(208): WP_Hook->apply_filters(‘http://gnplc.ca…’, Array)
#2 wp-includes/link-template.php(247): apply_filters(‘post_link’, ‘http://gnplc.ca…’, Object(WP_Post), false)
#3 wp-content/plugins/cornerstone/includes/shortcodes/recent-posts.php(67): get_permalink(Object(WP_Post))
#4 wp-includes/shortcodes.php(325): x_shortcode_recent_posts(Array, ‘’, ‘x_recent_posts’)
#5 [internal function]: do_shortcode_tag(Array)
#6

Hey @janaroberts,

Try replacing the previous code with the following:

add_filter( 'post_type_link', 'x_remap_portfolio_permalink_to_project_link', 10, 3);
add_filter( 'post_link', 'x_remap_portfolio_permalink_to_project_link', 10, 3);

function x_remap_portfolio_permalink_to_project_link($post_link, $post, $leavename=false) {

    if ('x-portfolio' == $post->post_type) {
        $custom = get_post_custom($post->id);

        if (isset($custom['_x_portfolio_project_link']) && $custom['_x_portfolio_project_link'][0]!='')
            return $custom['_x_portfolio_project_link'][0];
    }

    return $post_link;
}

Let us know how this goes!

Thank you for the updated code. That one isn’t throwing any errors, so that is great. However, when I go to test the “portfolio” items on my home page/calendar page, they are still linking to the portfolio landing page for that item, rather than the custom URL I want it to go to… like it does correctly on the “upcoming programs” page.

I was able to find a work around with the following plug-in: https://www.wpbeginner.com/wp-tutorials/how-to-link-to-external-links-from-the-post-title-in-wordpress/

Thanks!

Hello @JanaRoberts,

We are just glad that you have figured it out a way to correct the said issue.
Thanks for letting us know!

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.