Support » Fixing WordPress » Custom URL for image and “Learn More” links

  • Hey!

    I want to include Ads on my website that are loaded randomly.
    For now, I will be manually creating posts with the image and description (check the link [ redundant link removed ], the “ad” below where it says “Follow Wavebix”).

    My idea is to have a custom field named “URL” and then whatever link I put there, both the image and the “Learn More” will be sending the visitor to that link, instead of seeing the post.

    I will also create a Custom Post Type called “Ads” so everything is organized.

    How can I do this? I know how to create CPT and CF. I need help with the code that I need in order to make the CF be associated with the image and the Learn More link.

    So basically right now it send the visitor to either /ad1 or /ad2, but I want to send them to, for example, and

    Hope it makes sense…

    Thank you so much! 🙂

    • This topic was modified 5 months, 1 week ago by Tiago "3ple".
    • This topic was modified 4 months, 2 weeks ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not a Developing with WordPress topic

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • You could try adding the external URL as outlined here.

    Hi! Thanks for the reply.
    Unfortunately that technique doesn’t do the trick.
    It works when you visit the blog post itself, but not outside (for example, home page with all the posts). You can even check on their website. If you find the post and you click the featured image, it sends you to the post. Then, once inside, the image actually has the custom link, but that’s not what I’m trying to achieve. I can do that with a plugin I just downloaded, but I want my image to be a custom link without people visiting the post itself :/

    Any way I can do it?

    You can override the page template (use a child theme) and use a custom query to populate the ad area from a custom post type.

    Sorry, that’s too technical for me :/
    I’m ok with small changes, but that sounds too confusing.

    Can you please explain what I need to do, maybe pasting some code and where I should paste it?

    I already have a child theme (step one: check!)

    What should I do next?

    Moderator bcworkz


    Your URL field will be easier to code for if it were instead just the ad post’s slug instead of the entire URL. If the URL were saved, you need extra code to extract the slug.

    Anyway, the first thing you should do is determine where on which template you want the ads to appear. For convenience, you may want to create a custom function in your child’s functions.php which does the heavy lifting. On the templates you simply call your custom function to cause ads to appear. The same function can be called on various templates as desired.

    Ideally, this function would only be called within the standard WP loop so the current post ID can easily be determined with get_the_ID(). The next thing your function would do after getting the ID is to get the saved ad slug from the custom field. With that information you can call get_posts() with appropriate arguments that would return the appropriate ad post. Typical code for this is here. Some editing will be required to meet your needs.

    The get_posts() function actually does the query majaid is referring to. Doing a query sounds scary and complicated, but it doesn’t need to be all that complex. The above linked example is a relatively simple way to make a query.

    You will get from get_posts() an array containing a single post, so to access the post data you need to include its index into the array. The first array item index is always [0]. You can use the ad post’s ID to get its featured image, which is called the post thumbnail in code. Do something like
    echo get_the_post_thumbnail( $my_posts[0]->ID, 'medium');

    Use the same ID to get the ad’s permalink, with which you can construct the learn more link. Something like

    $link = esc_url( get_permalink( $my_posts[0]->ID ));
    echo "<a href=\"$link\">Learn More</a>";

    Thanks bcworkz, but that’s also too technical for me… :/

    The issue is that I use Elementor, so it’s not a regular template.
    I don’t think those codes would interact with Elementor the same way it would with, let’s say, “normal” themes.

    As I mentioned on my post, my idea (which I believe is the simplest one), would be to have a Custom Post Type called “Ads”, then create a Custom Field using Advanced Custom Fields so each post would be customizable. The only issue right now is that I don’t want just the image when I’m reading the post to redirect people. I actually want the image when more posts (in this case, ads) are visible.

    Thanks 🙂

    Moderator bcworkz


    I agree that a post type for ads is a good concept. The changes required for this go on theme templates whose code is external to Elementor. Elementor dramatically changes how you create post content, but it does not affect theme template code. The same templates can be used with or without Elementor.

    I’m not sure that a custom solution can be made any simpler. You might try searching for a plugin that inserts custom ads based on some criteria. You may need to change your concept in order to conform to plugin requirements, but hopefully the end result is similar.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.