• Resolved kasia89

    (@kasia89)


    Hi, I’m converting a template I made to a wordpress theme. I have a carousel on my front page. I used bootstrap while writing the template, and to integrate the carousel with wordpress, I registered it as a custom post. I want the pictures in the slider to link to specific pages, but they themselves won’t be on those pages.
    I googled how to do something like this, tried several approaches, but nothing worked. In my front page file I’m calling the slider like this:

    <div class="carousel-inner" role="listbox">
    	<?php $slider = get_posts(array('post_type' => 'slider', 'posts_per_page' => 5)); ?>
    	<?php $count = 0; ?>
    	<?php foreach($slider as $slide): ?>
    	    <div class="item <?php echo ($count == 0) ? 'active' : ''; ?>">
    	    <a href=""><img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($slide->ID)) ?>"></a>
    		</div>
    		<?php $count++; ?>
    		<?php endforeach; ?>
    </div>

    and I’m pretty sure that this could be solved by adding relevant php code inside the link tag but I have no idea what would that relevant code be.
    I tired using custom fields for this, but it didn’t work. I tired plugins, also without any success. The closest I got to a solution was by adding <?php echo get_post_meta($Post->ID, "slider-url", true); ?> inside the link tag, but this just links all the images to my front page, and I have no idea how to modify the code so it would do what I want.

    • This topic was modified 7 years, 10 months ago by kasia89.
Viewing 7 replies - 1 through 7 (of 7 total)
  • I see that you have perfect code for link. Just add a custom field named slider-url for each custom slide post, insert the page link. It will work to you.

    Thread Starter kasia89

    (@kasia89)

    Unfortunately, I already tried it, and it doesn’t work ?? I just tried it again, just to be sure I didn’t make any typo or anything the previous time, and it still links to my front page. It’s like something overriding input from the custom filed, but I have no idea what. The only time it linked the image to something else than the front page, was when I removed the true value from the code, just to see what will happen. It then linked the image to Array page (as in https://www.example.com/Array), which of course doesn’t exist, so I got the 404 error.
    But otherwise, it just links to the front page, regardless of any custom fields.

    Thread Starter kasia89

    (@kasia89)

    ok, so I went to phpmyadmin and searched for slider-url in wp_posts and wp_postmeta and it’s not there? So it looks as if wp isn’t “seeing” the field at all? I’m at loss here.

    Just go to edit your custom post in Dashbaord > Scroll Down to get the Box of Custom field > Now add the field named slider-url with its value. Hope I explained well.

    • This reply was modified 7 years, 10 months ago by mdshak.
    Thread Starter kasia89

    (@kasia89)

    I know how to do it, it’s just not working.
    I did create the slider-url custom field. Several times, I might add, while trying out several approaches.
    Now I actually managed to find it in phpmyadmin. But it’s still not working.
    And I made sure I saved and updated everything. That was the first thing I did when it wouldn’t work. I also checked if I didn’t make a typo anywhere, but, no, the field is called slider-url, and I’m calling slider-url. The link it should lead to is also properly saved, as I can see it as meta value in phpmyadmin.
    I have no idea why it’s not working.

    • This reply was modified 7 years, 10 months ago by kasia89.
    • This reply was modified 7 years, 10 months ago by kasia89. Reason: typo, additional info
    Thread Starter kasia89

    (@kasia89)

    Ok, never mind that, I got it to work ?? I forgot to change $post in <?php echo get_post_meta($post->ID, "slider-url", true); ?> to $slide. I literally assigned it to slider posts 2 lines above the link tag. I knew it was some stupid mistake on my part.
    But thank you for the effort to help ??

    Really it was hidden typo error. Anyway You found it. Let me know if You need further assistance at any other issue.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding a link to featured image in a custom post’ is closed to new replies.