0

I'm trying to get a thumbnail for my post. Like if the first custom meta box doesn't have any value/content, should use }else if ($img =get_post_custom_values('backdrop_img')) { $imgsrc = $img[0]; } (which is the 2nd metabox) then 3rd, else if it also doesn't have any content/value just use an image from my theme directory.

But my problem is, it only works on the first meta box, which is:

if($img = get_post_custom_values('featured_img')){ $imgsrc = $img[0];

after that line the codes doesn't work and won't get values from 2nd custom meta box or use the defaul image if nothing from the conditions work.

I'm new to php and stil learning so i'm still confused. I'd appreciate any help.

So here's my code so far.

UPDATED: with the entire test code of the loop.

   <?php $year = date ("Y"); query_posts(array(get_option('year') => $year, 'posts_per_page' => 5, 'showposts' => 5)); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php   if (has_post_thumbnail()) {
    $imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
    $imgsrc = $imgsrc[0];
    } elseif ($postimages = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=0")) {
    foreach($postimages as $postimage) {
    $imgsrc = wp_get_attachment_image_src($postimage->ID, 'medium');
    $imgsrc = $imgsrc[0];
    }
    } elseif (preg_match('/<img [^>]*src=["|\']([^"|\']+)/i', get_the_content(), $match) != FALSE) {
    $imgsrc = $match[1];
    } else {
    if($img = get_post_custom_values('featured_img')){
    $imgsrc = $img[0];
    }elseif($img = get_post_custom_values('backdrop_img')){
    $imgsrc = $img;
    } else {
    $img = get_template_directory_uri().'/movies/no_img.png';
    $imgsrc = $img;
    } 
    } ?>
    <div class="post_content">
    <img src="<?php echo $imgsrc; $imgsrc = ''; ?>
    <a href="<?php the_permalink() ?>" class="slide-link" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <p class="sc-desc">  <?php the_excerpt(); ?></p>
    </div>
    <?php } endwhile; ?>    
2

Thanks everyone for all the help! I really appreciate it. I was able to make it work by removing [0] in $imgsrc = $img[0]; and using custom_get_meta()

like:

$img = post_movie_get_meta('featured_img')

and:

$img = post_movie_get_meta('backdrop_img')

  • Nice, you can accept your own answer by the way – David Lee Feb 20 '17 at 14:39
1

I think you are missing the $post ID:

    <?php
    //lets put the default first
    $img = get_template_directory_uri() . '/movie/no_img.png';
    $imgsrc = $img;

    if (has_post_thumbnail()) {
        $imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
        $imgsrc = $imgsrc[0];
    } elseif ($postimages = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=0")) {
        foreach ($postimages as $postimage) {
            $imgsrc = wp_get_attachment_image_src($postimage->ID, 'medium');
            $imgsrc = $imgsrc[0];
        }
    } elseif (preg_match('/<img [^>]*src=["|\']([^"|\']+)/i', get_the_content(), $match) != FALSE) {
        $imgsrc = $match[1];
    } else {
        $img = get_post_custom_values('featured_img', get_the_ID());
        if (isset($img)) {
            $imgsrc = $img[0];
        } else {
            $img = get_post_custom_values('backdrop_img', get_the_ID());
            if (isset($img)) {
                $imgsrc = $img[0];
            } 

        }
    }
    //use it after all the logic
    echo $imgsrc;

    ?>

by the way if this is in the loop you can just use get_the_ID

  • It still doesn't work. And yes it is in the loop. Also i tried using image (example: image.tmdb.org/t/p/w780/l4Xj74NDYzdH7iiOYn1i1FFso0c.jpg) on featured_image custom meta box and it shows, but when i remove it and place it in the "backdrop_img" instead, nothing shows up, even the the no_img.png doesn't shows up so i think the condition stop at the featured_img line, but i can't figure it out why since im just a beginner.. – Archangel17 Feb 20 '17 at 3:51
  • try doing a var_dump(get_post_custom_values('featured_img')) to see if the line is being executed and what is outputting if it does. – David Lee Feb 20 '17 at 3:54
  • I've tried using word to test it, and here's that output is correct: array(1) { [0]=> string(4) "test" } i also tried var_dump on backdrop_img array(1) { [0]=> string(63) "test2" } and the output is correct too. Btw, i placed the var_dump under if ($img = get_post_custom_values('featured_img',$post->ID)) { because when i tried putting it under } else if ($img = get_post_custom_values('backdrop_img',$post->ID)) { or under $img = get_template_directory_uri().'/images/noimg.png'; it doesn't output anything. – Archangel17 Feb 20 '17 at 4:21
  • try using the values you need not only words, they have to return an URL right? – David Lee Feb 20 '17 at 4:28
  • Yes, i tried using the links too and they return the right url too. (both featured_img & backdrop_img meta box) – Archangel17 Feb 20 '17 at 4:36
1

i think you have wrong this else if not right elseif is right so your else part is not execute. document link : http://php.net/manual/en/control-structures.elseif.php

your code like this :

else if ($img =get_post_custom_values('backdrop_img')) {

Replace with :

elseif ($img =get_post_custom_values('backdrop_img')) {

I hope it's helpful.

  • I tried that already but still doesn't work... – Archangel17 Feb 20 '17 at 5:47
0

This is not how you should use elseif. Note that the following elseif conditions will run, not matter the result of your initial if(). You should use nested if() instead.

Take a look at this:

 <?php 
    $year = date ("Y");
    query_posts(
        array(
            get_option('year') => $year,
            'posts_per_page' => 5,
            'showposts' => 5)
        );
    while ( have_posts() ) : the_post(); 
        if ( has_post_thumbnail() ) {
            $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
            $imgsrc = $imgsrc[0];
        } else {
            $postimages = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=0" );
            foreach( $postimages as $postimage ) {
                $imgsrc = wp_get_attachment_image_src($postimage->ID, 'medium');
                $imgsrc = $imgsrc[0];
            }
            if ( !$imgsrc ) {
                preg_match('/<img [^>]*src=["|\']([^"|\']+)/i', get_the_content(), $match);
                $imgsrc = $match[1];
                if (!$imgsrc){
                    $img = get_post_custom_values('featured_img');
                    $imgsrc = $img[0];
                    if (!$imgsrc){
                        $img = get_post_custom_values('backdrop_img');
                        $imgsrc = $img;
                        if (!$imgsrc){
                            $img = get_template_directory_uri().'/movies/no_img.png';
                            $imgsrc = $img;
                            }
                    }
                }
            }
        } ?>
        <div class="post_content">
        <img src="<?php echo $imgsrc; $imgsrc = ''; ?>
        <a href="<?php the_permalink() ?>" class="slide-link" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <p class="sc-desc">  <?php the_excerpt(); ?></p>
        </div>
        <?php } endwhile; ?>    

In this code, we check if the post has thumbnail. If not, then proceed to get the children. Then again we check if the image has been retrieved. If it hasn't then we proceed to next method. Then we check again, and so on.

At the end, if none of the method works, we simply return a png image and end the if().

FYI: This is how elseif works:

if ( //First condition ) { // check if the first condition is true, and do stuff } elseif ( //Second condition ) { //Check second condition too, and do some more stuff, no matter what was the result of the first condition and stuffs }

PS: I didn't validate the WordPress syntax. I just updated the if() structure for you.

PS2: You can also just use if() after if(), row after row. That can also be a solution, like the following:

if ( //First ) { // First stuff } else { If ( //Second ) //Some code here; if ( //Third ) //Some more code; if ( //4th ) //Some extra code; }

Choose based on your needs and style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.