• Resolved Mehmet

    (@mhmtozek)


    Here what i do, the following function is created in functions.php. I use that function to check if a video is embedded into post using custol field, if video return false, to check for post thumbnail, and if post thumbnail return false, to call a custom image.

    <?php
    function thumb_video($test_fnname,$test_w,$test_h,$test_align)
    	{
    		$test_video = get_post_meta($post->ID, 'video', TRUE);
    
    		if ($test_video !== '') {  ?>
    			show video
    		<?php } elseif ( has_post_thumbnail() ) { ?>
    			display thumbnail
    		<?php } else {  ?>
    			display custom thumbnail
    		<?php }
    	}
    ?>

    in home.php inside loop, this function is being called as

    <?php thumb_video(‘featured’,’200′,’200′,’aligncenter’); ?>

    The problem is;
    Whether there is a video or not, it will always think that there is a video and i will get show video part as result

    Anyone got an idea?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Try changing:

    if ($test_video !== '') { ?>

    to:

    if ($test_video != '') { ?>

    Thread Starter Mehmet

    (@mhmtozek)

    if ($test_video != '') { ?>
    with this change, i get a false for if call, while actually i had a custom field defined with video key,

    so it just jumped to second div and displayed thumbnail ??

    Thread Starter Mehmet

    (@mhmtozek)

    anybody know how to create functions that contains custom fields in?

    Are you using your function inside the loop?

    If not, then $post->ID may not refer to anything, which will mean you get_post_meta call will always fail.

    Thread Starter Mehmet

    (@mhmtozek)

    Here is complete code that i use

    Functions.php

    function thumb_video($test_fnname,$test_w,$test_h,$test_align)
    	{
    		$test_video = get_post_meta($post->ID, 'video', TRUE); /* Custom field video check */
    		$test_thumb = get_post_meta($post->ID, 'thumbnail', TRUE); /* Custom field thumbnail check */
    
    		if($test_video != '') { /* If a FLV video is attached to post */ ?>
    		display flv
    		<?php } elseif($test_thumb !== '') { /* If custom field thumbnail is defined */ ?>
    			call custom field defined thumbnail
    		<?php } elseif ( has_post_thumbnail() ) { /* If post have thumbnail */ ?>
    			call post_thumbnail
    		<?php } else { /* If there is no video, thumbnail custom field, or post_thumbnail defined, display default category image */ ?>
    			call custom image
    		<?php }
    	}

    and in home.php

    <?php
    $gabquery = new WP_Query();$gabquery->query('showposts='.$npdv_options["feaPostCount"].'&cat='.$npdv_options["featuredCatID"]);
    while ($gabquery->have_posts()) : $gabquery->the_post();
    ?>
    <?php thumb_video('featured','342','256','alignnone'); */ ?>
    <?php the_excerpt(); ?>
    <?php endwhile; wp_reset_query(); ?>

    Print/dump all your variables as you go, and you’ll be able to check they contain what you’re expecting them to contain.

    Thread Starter Mehmet

    (@mhmtozek)

    The problem is, when I dump that code it skips video custom field.

    There is a post that has video key added in custom fields, and a post_thumbnail is set.

    If/else returns false for video when using that code as is, and it returns post thumbnail which is a lower priority in if/else query.

    Everything seems to be correct but something is wrong that i can’t figure it out

    Thread Starter Mehmet

    (@mhmtozek)

    in custom function to place a
    global $post;

    right after

    function thumb_video($test_fnname,$test_w,$test_h,$test_align)
    	{

    fixed it, the issue is resolved.

    Which is what i was getting at in my original reply, inside a function, variables you need, will either have to be declared as global(so they’re inside scope of the function), or passed into the function via one of the arguments.

    You’d arrive at this conclusion alot faster by dumping/printing your data as you write it, as it ensures your variables contain what they should, and it’s easier to backtrace at that point, rather then at the end.

    Perhaps i should of been more clear initially… happy to hear you solved your problem anyway.. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Using custom fields check/call in a function’ is closed to new replies.