• Resolved Bootjesgek HK

    (@hankooiman)


    https://www.bootjesgek.com
    All our posts contain one or more images. For articles with more than one image, a thumbnail is displayed on the home and archive pages; and a big picture in the carousel on the homepage. I seem to have no contol over what image is chosen. I would like to be able to chose the right image myself. The code that I think is responsable is in functions.php:
    ‘# Displays post image attachment (sizes: thumbnail, medium, full)
    function dp_attachment_image($postid=0, $size=’articleimg’, $attributes=”) {
    if ($postid<1) $postid = get_the_ID();
    if ($images = get_children(array(
    ‘post_parent’ => $postid,
    ‘post_type’ => ‘attachment’,
    ‘numberposts’ => 1,
    ‘post_mime_type’ => ‘image’,)))
    foreach($images as $image) {
    $attachment=wp_get_attachment_image_src($image->ID, $size);
    ?><img src=”<?php echo $attachment[0]; ?>” <?php echo $attributes; ?> /><?php
    }
    }’

Viewing 15 replies - 1 through 15 (of 15 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    If you make a custom field of “post image” with a value of 2 on your post this new function will show the second image in the post. Overwrite the old function with this one in your functions.php

    function dp_attachment_image($postid=0, $size='articleimg', $attributes='') {
    if ($postid<1) { $postid = get_the_ID(); }
    $images = get_children(array(
    'post_parent' => $postid,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'));
    $images = array_reverse($images);
    $images = array_values($images);
    if (!empty($images)) {
    $postimg = get_post_meta($postid, 'post image', true);
    $postimg = ($postimg != '') ? $postimg-1 : 0;
    $postimg = ($postimg >= 0) ? $postimg : 0;
    $attachment= wp_get_attachment_image_src($images[$postimg]->ID, $size);
    if (!empty($attachment)) {
    echo '<img src="' .$attachment[0] .'" ' . $attributes .' />';
    }
    }
    }

    Thread Starter Bootjesgek HK

    (@hankooiman)

    Thanks. I’ll try that.
    Still, I think it would be easier if something could be arranged in the gallery of the post; or if maybe always the first of last image published in the article was chosen.
    Can you give me some clue on what basis the choice is made now?
    And… maybe we could do this in dutch?

    Moderator keesiemeijer

    (@keesiemeijer)

    If you give no custom field (or value of 1) to the post the first picture in the post is chosen. Value 2 will show the second image, 3 the third etc. etc. Yes we could do this in Dutch, maar this is an English forum.

    Moderator keesiemeijer

    (@keesiemeijer)

    “Still, I think it would be easier if something could be arranged in the gallery of the post”

    There is a feature called Post Thumbnail Images. This tutorial is very good if you want to implement this: https://markjaquith.wordpress.com/2009/12/23/new-in-wordpress-2-9-post-thumbnail-images/

    Thread Starter Bootjesgek HK

    (@hankooiman)

    I tried the code you gave on my Wampserver. There is a problem: The carousel does not work anymore. I just get the intended images of the carousel displayed in a row under eachother.

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you provide a link to your theme please? The carousel seems to use the same function as the post images.

    Thread Starter Bootjesgek HK

    (@hankooiman)

    Sorry, I jumped to conclusions to fast. The bug came from the newly installed plugin of wordTube; not from the code you gave me. It is correct that the caroussel uses the same function as the post thumbnail.
    Link to the Business Magazine that I used to adapt: https://www.promojunkie.com/forum/free-templates/23528-business-magazine-wordpress-theme.html

    Moderator keesiemeijer

    (@keesiemeijer)

    Just to be sure, it works like you intended now? If so can you make this topic “resolved” please.

    Thread Starter Bootjesgek HK

    (@hankooiman)

    It does the trick, but not completely as intended. I can influence the image to be displayed, but there still is a big gamble: f.e. post image value 1 produced image nr 3, 3>7, 4>2, 5>4, 6>6, 7>5, 8>no image which is correct because there were only 7 images in this article: https://www.bootjesgek.com/?p=811 (tested on my wampserver still)

    Moderator keesiemeijer

    (@keesiemeijer)

    You mean that if you give a post image value value higher than the actual images in the post no picture is shown. I changed the function a little bit so now if you give a value that is to high it will show the first image of the post.

    function dp_attachment_image($postid=0, $size='articleimg', $attributes='') {
    if ($postid<1) { $postid = get_the_ID(); }
    $images = get_children(array(
    'post_parent' => $postid,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'));
    $images = array_reverse($images);
    $images = array_values($images);
    if (!empty($images)) {
    $postimg = get_post_meta($postid, 'post image', true);
    $postimg = ($postimg != '') ? $postimg-1 : 0;
    $postimg = ($postimg >= 0) ? $postimg : 0;
    $postimg = ($postimg <= count($images)) ? $postimg : 0;
    $attachment= wp_get_attachment_image_src($images[$postimg]->ID, $size);
    if (!empty($attachment)) {
    echo '<img src="' .$attachment[0] .'" ' . $attributes .' />';
    }
    }
    }

    Thread Starter Bootjesgek HK

    (@hankooiman)

    Your last contibution brings me to an other idea: It would be very practical if the default was to display the last picture of the post. That is much nicer then the first picture because the opening of the post comes more as a surprise in that case! My impression is that you are able to change the code in that way. Is that true? It would be a big help to have a code like that, even without having to make a custom field.

    Moderator keesiemeijer

    (@keesiemeijer)

    Here you go:

    function dp_attachment_image($postid=0, $size='articleimg', $attributes='') {
    if ($postid<1) { $postid = get_the_ID(); }
    $images = get_children(array(
    'post_parent' => $postid,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'));
    $images = array_reverse($images);
    $images = array_values($images);
    if (!empty($images)) {
    $postimg = get_post_meta($postid, 'post image', true);
    $last_image = (int) count($images)-1;
    $postimg = ($postimg != '') ? $postimg-1 : $last_image;
    $postimg = ($postimg >= 0) ? $postimg : $last_image;
    $postimg = ($postimg <= ($last_image)) ? $postimg : $last_image;
    $attachment= wp_get_attachment_image_src($images[$postimg]->ID, $size);
    if (!empty($attachment)) {
    echo '<img src="' .$attachment[0] .'" ' . $attributes .' />';
    }
    }
    }

    Thread Starter Bootjesgek HK

    (@hankooiman)

    Thanks.
    The code still does not work as I intended but I found out that the ‘last image’ does not mean the last image shown in the article, but the image with the highest attachment ID, so the last image that was uploaded. Also the ‘first image’ is not the one shown in the head of the post, but the one first uploaded (so the one with the smallest attachment ID). Makes sense, of course if you know.
    Respect!
    I will put this topic on resolved.

    Thread Starter Bootjesgek HK

    (@hankooiman)

    On second thought: is it not easy and realy a lot of fuzz to find the attachment ID’s. Is there a way to avoid this, or to find the ID’s easily? In my Wampserver config it is easy: just clicking on the image gives a picture page and the ID in the address bar. But on the Bootjesgek.com server this seems to work differently.

    Thread Starter Bootjesgek HK

    (@hankooiman)

    The practical solution is to upload the chosen image again. This works fine but of course only if the code says to show the last image uploaded (he highest attachment ID).

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘choose image to be displayed’ is closed to new replies.