Wordpress Custom Value If/ElseIf - Doesn't Work - wordpress

I'm outside the loop and want to call a custom value "featvideo" and display it. If there isn't "featvideo" then print an image...
The video displays, but when there isn't a video a blank box displays. You can see the the issue here: http://wgl.buildthesis.com
(and yes, $images is a function defined in functions.php and works)
<?php
$feat_catbox_1 = new WP_Query("cat=$tt_feat_id&showposts=$tt_feat_postcount");
while ($feat_catbox_1->have_posts()) : $feat_catbox_1->the_post();
$key = 'featvideo';
$video_url = get_post_custom_values($key);
$featuredvideo = $video_url[0];
?>
<div class="contentdiv">
<div id="featured-thumb">
<?php if ($key=="featvideo")
echo $featuredvideo;
elseif ($key=="")
echo $images('1', '390', '244', 'alignleft', true); ?>
</div>
</div>

Since you have set $key = 'featvideo' and then test if it is set later, it will always return true. You never change the value of $key anywhere in your code except when you set it.
I would suggest something like the following for your if statement:
<?php
if($featuredvideo)
echo $featuredvideo;
else
echo $images('1', '390', '244', 'alignleft', true);
?>

Related

Target the next ID inside the Loop (WP)

Is it possible to target the next section inside the loop? so that it will scroll down to the next section area.
Here is my code:
<section id ="<?php echo $postid; ?>" class="subpage-wrapper fullscreen background parallax" style="background-image: url('<?php echo $image[0]; ?>')" data-img-width="1400" data-img-height="717" data-diff="100" data-oriz-pos="100%">
<a href="#<?php echo $postid; ?>" class="btn-scroll" data-scroll></a>
</section>
Remember that this is inside the WP_Query Loop
Thanks!
I'm not keeping any selections within your displaying of posts in mind, but I figure you can get 'round by using get_next_post(). Something like:
global $post;
$post = get_post($post_id);
$next = get_next_post();
$next_id = $next->id;
What about using your own counter?
$nextAnchorId = 0;
while ( have_posts() ) { //the loop
$nextAnchorId++; //make this actually be a reference to the *next* post
the_post();
if ($nextAnchorId < $post_count) //don't link to the next post if it's the last one
{
echo "<a href='#post-$nextAnchorId'>next post</a>";
}
}

Wordpress - Can't output image (from custom field) for previous and next posts

I am working on a portfolio website. Whenever a portfolio piece is clicked, the user is taken to a page that shows details about that piece (i.e. more photos and information). There will also be previous and next navigation links to get to additional pieces. However, I want the previous and next navigation links to be a thumbnail photo of the next piece (custom field for that is thumbnail_photo). This is what I have so far:
<?php
$previous_post = get_previous_post();
$next_post = get_next_post();
$prev_value = get_post_meta( $previous_post->ID, 'materials', $single = true);
$next_value = get_post_meta( $next_post->ID, 'thumbnail_photo', $single = true);
?>
<p><?php echo $prev_value; ?></p>
<p><?php echo $next_value; ?></p>
I used 'materials' in the call for $prev_value since 'materials' is another custom field. I just wanted to see if it was actually working. It outputs the materials just fine, but it only outputs the ID number of the thumbnail_photo. I can't get it to reference the file name so that I can output the actual image.
I am using the Advanced Custom Fields plugin, so each image is stored as an image object. So this is how I would typically output a thumbnail image:
<?php
$image = get_field('thumbnail_photo);
echo $image[sizes]["thumbnail"]; // thumbnail is a reference to wordpress' thumbnail media size
?>
When you configure the ACF input field, be sure you set the return value to be an image object instead of the image ID after change this, or if you already changed, you have to update the post or posts you're trying to get. Because, if you set the field return value as image ID, created the posts and now you wanna change their values you have to modify all the posts because every post_meta is containing the ID of the image.
In the ACF website has a tutorial how you could get the values an turn into images:
With the ID
<?php
wp_get_attachment_image( $next_value, 'thumbnail' );
?>
Another Example With the ID, but this time you have to create the image HTML, the function returns an array with the url, width and height
<?php
$image = wp_get_attachment_image_src( $next_value, 'thumbnail' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
You could do a check for the returned value and then write the image, example:
<?php
if( is_int( $next_value ) ) {
wp_get_attachment_image( $next_value, 'thumbnail');
} elseif ( is_object( $next_value ) ) {
echo $next_value['sizes']['thumbnail'];
} else { ?>
<img src="<?php the_field('field_name'); ?>" alt="" />
<?php } ?>
I didn't test it, but I think it works fine.
I hope this help and sorry for the bad english

timthumb NOT_FOUND_IMAGE resize

In a wordpress site, i have defined a default NOT_FOUND_IMAGE in timthumb config file:
// "http://example.com/timthumb-config.php".
<?php
if(! defined('NOT_FOUND_IMAGE') ) define ('NOT_FOUND_IMAGE', 'http://example.com/img/default.jpg');
Is there any way to force resizing of this image depending on timthumb request parameters. For instance:
// 404 occurs
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2F404-image.jpg&h=180&w=120
// get resized (cropped) default image
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2Fdefault.jpg&h=180&w=120
Try this code:
if (!defined('NOT_FOUND_IMAGE'))
define ('NOT_FOUND_IMAGE','images/ingredient.jpg');
I don't rate timbthumb, and I think it has security issues. Install and use the ThumbGen plugin instead.
You can then use it really easily with a normal editable conditional php statement.. (Pseudo)
<?php if you have a thumbnail {
Output the img with thumbgen resized
} else {
Output the no image, you can even use thumbgen if you want
}
?>
Real world example:
<?php if ( has_post_thumbnail() )
{
$image_id = get_post_thumbnail_id();
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
?>
<img src='<?php thumbGen($image_url,175,175, "crop=1"); ?>' />
<?php
}
else
{
?>
<img src="<?php bloginfo('template_url');?>/images/no-photo.png" alt="No Photo!">
<?php
}
?>

ElseIf Custom Field different value (Wordpress)

(sorry for my English) and I hope to explain!
I'm looking for a solution to display different output for a custom filed, named "cw". The my theme of much use in custom filed, so I created a function to simplify your calls:
/**
* Get custom field of the current page
* $type = string|int
*/
function my_getcustomfield($filedname, $page_current_id = NULL)
{
if($page_current_id==NULL)
$page_current_id = get_page_id();
$value = get_post_meta($page_current_id, $filedname, true);
return $value;
}
said this, I created the custom filed in my theme:
<?php $video_code = my_getcustomfield('cw',get_the_ID()); if(!empty($video_code)) : ?>
<div class="video_code"><?php echo $video_code; ?></div>
Since the custom value is a URL, type: http://127.0.0.1:4001/?f=cadbe2676d5108523f931a68eedb3582, I need to extract just the ID. using this technique PHP Regex to get youtube video ID?, I modified my custom filed in this way:
<?php $video_code = my_getcustomfield('cw',get_the_ID()); $url = $video_code; parse_str(parse_url($url, PHP_URL_QUERY )); if(isset($video_code[0])){ ?>
<div cacaolink="http://127.0.0.1:4001/?f=<?php echo $f; ?>"></div>
<?php } else { ?>
<?php echo 'Video n.d.'; ?></div>
<?php endif; } ?>
In this way everything works perfectly.
Now, the custom value can be those listed below:
<div cacaolink="http://127.0.0.1:4001/megavideo/megavideo.caml?videoid="></div>
<div cacaolink="http://127.0.0.1:4001/videobb/videobb.caml?videoid="></div>
<div cacaolink="http://127.0.0.1:4001/?f="></div>
**Finally here is the problem: How can I use an elseif according to the custom value?
any help is appreciated :)**
So what you need in short is extracting "video id" part from urls having one of 3 structures defined above. If I understood you right, you can just use the next code:
$video_id = preg_replace("/.*\?(videoid|f)=/", '', $customvalue);
Where $customvalue is the value extracted from your custom field with my_getcustomfield.

not able to retrieve subterm image in page-taxonomy.tpl.php

I want my taxonomy pages to be styled in a particular way. For this I have done the necessary in template.php. Now I have page-taxonomy.tpl.php ready and working. I am using taxonomy image module to upload images to taxonomy terms. I am retrieving the images of a corresponding term using taxonomy_image_get_url($term->tid);and displaying them. Now I want to retrieve the subterms of this term, which I have been able to do using _taxonomy_term_children($term->tid)
The problem is I am not able to retrieve its image.
<?php if ( arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) ) {
$term = taxonomy_get_term(arg(2));
$image_url = taxonomy_image_get_url($term->tid);
print $image_url;
}?>
<div id="taxonomy_term_image"><img src="/sites/default/files<?php print $image_url;?>" /></div>
<div id="taxonomy_term_description"> <?php print $term->description;?> <?php print $images_url;?></div>
<?php print $feed_icons; ?><br/>
Sub Categories: <?php $taxonomy_children = _taxonomy_term_children($term->tid);
foreach ($taxonomy_children as $value) {
$tax_child = taxonomy_get_term($value);
print $tax_child->name;
print $tax_child->description;
$subterm_image = taxonomy_image_get_url($taxchild->tid);
print $subterm_image;
}?>
You're missing the HTML code to render the image. Need to put it in as you did the parent image:
<img src="/sites/default/files<?php print $subterm_image;?>" />

Resources