Get thumbnail from page? (Concrete 5.7.4.2) - concrete5

As posted here https://www.concrete5.org/index.php?cID=751287 I want to get a thumbnail from a page using the 'old' way.
Before I could use the code below which included an image helper.
<div class="image-link">
<a <?php if ($target != '') { ?> target="<?php echo $target ?>" <?php } ?> href="<?php echo $url ?>">
<?php
$ts = $page->getBlocks('Thumbnail Image');
if (is_object($ts[0])) {
$tsb = $ts[0]->getInstance();
$thumb = $tsb->getFileObject();
if ($thumb) {
$ih->outputThumbnail($thumb, 170, 80, $title);
}
}
?>
</a>
</div>
From this section of the subpage:
<div id="thumbnail">
<?php
if ($c->isEditMode()) {
print '<br><br>';
$a = new Area('Thumbnail Image');
$a->display($c);
}
?>
</div>
However now this has all changed and the new system uses page attributes for thumbnails. As the site is already setup the 'old' way I want to be able to retrieve the thumbnail the same way again.
Any help would be much appreciated.

I have "thumbnail" page attribute set via the composer, and this is how I retrieve it in the page template:
<?php
$thumbnail = $c->getAttribute('thumbnail');
if($thumbnail) {
$img = Core::make('html/image', array($thumbnail));
$tag = $img->getTag();
print $tag;
}
?>

I dug out my experimentation hat and fixed it.
<div class="image-link">
<a <?php if ($target != '') { ?> target="<?php echo $target ?>" <?php } ?> href="<?php echo $url ?>">
<?php
foreach ($blocks as $block) {
if ($block->getBlockTypeHandle() == "image" && $block->getAreaHandle() == "Thumbnail Image") {
if (is_object($block)) {
$tsb = $block->getInstance();
$thumb = $tsb->getFileObject();
if ($thumb) {
$ih->outputThumbnail($thumb, 170, 80);
}
}
}
}
?>
</a>
</div>

Related

Multiple Images/ Slider with custom post [single.php]

I am looking for a approach to add multiple images option under the Feature image section in wordPress custom post type. I will integrate/implement these images on single.php file where I want to show the slider images. I want to do it using Codex / coding approach, Please NO PLUGIN. Any Idea/help will be appreciated.
EDIT
I have found this code and it has to be pasted in function.php or plugin.php. I have create a new file plug-slider.php and add the below code there & its not working, but the issue is that I have to call each image separately instead like create an array and call the array, also if there is no second/third image set for other post then the slider block is showing empty.. I don't know why, may be I am missing something... can someone please review.
Code Reference: Stack overflow question
//init the meta box
add_action( 'after_setup_theme', 'custom_postimage_setup' );
function custom_postimage_setup(){
add_action( 'add_meta_boxes', 'custom_postimage_meta_box' );
add_action( 'save_post', 'custom_postimage_meta_box_save' );
}
function custom_postimage_meta_box(){
//on which post types should the box appear?
$post_types = array('portfolios','page');
foreach($post_types as $pt){
add_meta_box('custom_postimage_meta_box',__( 'More Featured Images', 'yourdomain'),'custom_postimage_meta_box_func',$pt,'side','low');
}
}
function custom_postimage_meta_box_func($post){
//an array with all the images (ba meta key). The same array has to be in custom_postimage_meta_box_save($post_id) as well.
$meta_keys = array('second_featured_image','third_featured_image');
foreach($meta_keys as $meta_key){
$image_meta_val=get_post_meta( $post->ID, $meta_key, true);
?>
<div class="custom_postimage_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:20px;">
<img src="<?php echo ($image_meta_val!=''?wp_get_attachment_image_src( $image_meta_val)[0]:''); ?>" style="width:100%;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" alt="">
<a class="addimage button" onclick="custom_postimage_add_image('<?php echo $meta_key; ?>');"><?php _e('add image','yourdomain'); ?></a><br>
<a class="removeimage" style="color:#a00;cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" onclick="custom_postimage_remove_image('<?php echo $meta_key; ?>');"><?php _e('remove image','yourdomain'); ?></a>
<input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>" />
</div>
<?php } ?>
<script>
function custom_postimage_add_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
custom_postimage_uploader = wp.media.frames.file_frame = wp.media({
title: '<?php _e('select image','yourdomain'); ?>',
button: {
text: '<?php _e('select image','yourdomain'); ?>'
},
multiple: false
});
custom_postimage_uploader.on('select', function() {
var attachment = custom_postimage_uploader.state().get('selection').first().toJSON();
var img_url = attachment['url'];
var img_id = attachment['id'];
$wrapper.find('input#'+key).val(img_id);
$wrapper.find('img').attr('src',img_url);
$wrapper.find('img').show();
$wrapper.find('a.removeimage').show();
});
custom_postimage_uploader.on('open', function(){
var selection = custom_postimage_uploader.state().get('selection');
var selected = $wrapper.find('input#'+key).val();
if(selected){
selection.add(wp.media.attachment(selected));
}
});
custom_postimage_uploader.open();
return false;
}
function custom_postimage_remove_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
$wrapper.find('input#'+key).val('');
$wrapper.find('img').hide();
$wrapper.find('a.removeimage').hide();
return false;
}
</script>
<?php
wp_nonce_field( 'custom_postimage_meta_box', 'custom_postimage_meta_box_nonce' );
}
function custom_postimage_meta_box_save($post_id){
if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'not permitted'; }
if (isset( $_POST['custom_postimage_meta_box_nonce'] ) && wp_verify_nonce($_POST['custom_postimage_meta_box_nonce'],'custom_postimage_meta_box' )){
//same array as in custom_postimage_meta_box_func($post)
$meta_keys = array('second_featured_image','third_featured_image');
foreach($meta_keys as $meta_key){
if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
}else{
update_post_meta( $post_id, $meta_key, '');
}
}
}
}
My Callback code in single.php
<div class="project-carousel owl-carousel js-project-carousel">
<?php
while(have_posts()) {
the_post(); ?>
<!-- <div class="project-detail-item">
<?php //the_post_thumbnail('portfolio-slider') ?>
</div> -->
<?php
$slider_img=wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
if($slider_img !='')
{?>
<div class="project-detail-item">
<?php
echo wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
?>
</div>
<?php }
else
{
the_post_thumbnail('portfolio-slider');
}
?>
</div>
again the if/else block works for the empty check but only for single instance, like I have to check for each image... or I messed up?
Please do the below change and then try...
in your plugin file change this line.
$meta_keys = array('second_featured_image','third_featured_image');
To
$meta_keys = array('second_featured_image','third_featured_image','fourth_featured_image','fifth_featured_image');
change your call back single.php file code to something like below. Loop through each file and check for empty.
I have modified the code to accept up to 5/6 images so you can use it in the slider.
<div class="project-carousel owl-carousel js-project-carousel">
<?php
while(have_posts()) {
the_post();
$slider_img=the_post_thumbnail('portfolio-slider');
$slider_img1=wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
$slider_img2=wp_get_attachment_image(get_post_meta(get_the_ID(),'third_featured_image', true),'full');
$slider_img3=wp_get_attachment_image(get_post_meta(get_the_ID(),'fourth_featured_image', true),'full');
$slider_img4=wp_get_attachment_image(get_post_meta(get_the_ID(),'fifth_featured_image', true),'full');
$slider_img5=wp_get_attachment_image(get_post_meta(get_the_ID(),'six_featured_image', true),'full');
$slider_img6=wp_get_attachment_image(get_post_meta(get_the_ID(),'svn_featured_image', true),'full');
if($slider_img !='')
{?>
<div class="project-detail-item">
<?php echo $slider_img;?>
</div>
<?php }?>
<?php if($slider_img1 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img1; ?>
</div>
<?php }?>
<?php if($slider_img2 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img2; ?>
</div>
<?php }?>
<!--image-->
<?php if($slider_img3 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img3; ?>
</div>
<?php }?>
<!--image end-->
<!--image-->
<?php if($slider_img4 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img4; ?>
</div>
<?php }?>
<!--image end-->
<!--image-->
<?php if($slider_img5 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img5; ?>
</div>
<?php }?>
<!--image end-->
<!--image-->
<?php if($slider_img6 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img6; ?>
</div>
<?php }?>
<!--image end-->
</div>

Drupal 7: Hard coding layout in Views

In one of my D7 project, I used several tpl files for view templating. Please see below some of the tpl file codes. However, my client has been reviewed the codes and commented:
Hard coding layout in Views. Instead of creating blocks with different display layouts (same display layouts are the same in other places), there is a code with the layout on the Views result that it depends on the key of the result.
I cannot understand the points he saying. If you wish, I can share some additional coding / admin panel screen shots
Here are some tpl files:
1.
<div id="programmes" class="page-section">
<div class="container">
<h1 class="section-heading"><?php echo t('OUR');?> <?php echo count($rows); ?> <?php echo t('key Programmes');?></h1>
<h3><?php echo t('The XXX operate');?> <?php echo count($rows); ?> <?php echo t('key programmes to ensure the greatest global impact:');?></h3>
<div class="programblock" >
<?php
if($rows) {
$count = 1;
foreach($rows as $id => $row)
{
$title = $view->render_field('title', $id);
$description = $view->render_field('field_programme_description', $id);
$image = $view->render_field('field_programme_image', $id);
$icon = $view->render_field('field_programme_icon', $id);
$color = $view->render_field('field_programme_background_color', $id);
?>
<a href="#" class="programcard w-inline-block" style="background-color:<?php echo !empty($color) ? $color : '#FFFFFF' ?>"><img src="<?php echo $image; ?>" class="progimage">
<div class="programtext">
<div class="w-clearfix">
<div class="progtabicon smallpti"><?php echo $icon; ?><br></div>
</div>
<div>
<h2 class="progtitle"><?php echo $title; ?></h2>
<div><?php echo $description; ?></div>
<div class="readmorecard"><?php echo t('Read More...'); ?></div>
</div>
</div>
</a>
<?php
$count++;
}
}
?>
</div>
</div>
</div>
2.
<div id="s5" class="relevant-news">
<div class="container w-clearfix">
<h3 class="section-heading"><?php echo t('Related content'); ?></h3>
<?php
if ($rows) {
foreach ($rows as $id => $row) {
$image = $view->render_field('field_image', $id);
$body = $view->render_field('body', $id);
$title = $view->render_field('title', $id);
$date = $view->render_field('field_news_publist_date', $id);
$formatted_date = date('d F Y', strtotime($date));
$programme = $view->render_field('field_news_programme', $id);
$type = $view->render_field('field_tags', $id);
?>
<div class="_4-block nomar4">
<a href="#" class="nl2 w-inline-block">
<div class="tag" data-ix="tagtext"><span class="cblu"></span><span class="uhcgreen"> </span><span class="jyel"></span><br></div><img src="<?php echo $image; ?>" alt="image" class="cardimage">
<div class="card-text">
<div class="cardtitle"><?php echo $title; ?></div>
<div class="tagtext alt"><?php echo $formatted_date; ?></div>
<div><?php echo $body; ?></div>
<div class="tagtext alt"><?php echo $programme; ?></div>
</div>
<div class="pagetype news">
<div><span class="fa"></span> <?php echo $type; ?></div>
</div>
</a>
</div>
<?php
}
}
?>
</div>
<div class="container"><span class="fa"></span> <?php echo t('View all News & Media'); ?></div>
</div>
3.
<?php global $base_url;?>
<?php
if(!empty($rows)){
$articles = array();
$video = array();
$gallery = array();
$carousel = array();
foreach($rows as $article){
$nid = $article['nid'];
$node = node_load(trim($nid));
if($node->type == 'article'){
$articles[] = $nid;
}elseif($node->type == 'news_video'){
$video[] = $nid;
}elseif($node->type == 'news_gallery'){
$gallery[] = $nid;
}
$slider = 0;
if($node->type != 'article'){
$carousel[] = $nid;
}
}
$items = array();
$count = 0;
if(!empty($carousel)){
$slider = 0;
foreach($carousel as $nid){
$items[$slider][] = $nid;
$count++;
if(($count % 5) == 0) {
$slider++;
}
}
}
?>
<!--Show stiky images-->
<div class="container w-clearfix">
<?php
$i = 0;
for($i = 0; $i < count($articles); $i++){
$nid = (int) $articles[$i];
$node = node_load(trim($nid));
$title = $node->title;
$image = file_create_url($node->field_image[LANGUAGE_NONE][0]['uri']);
$news_type_id = $node->field_tags[LANGUAGE_NONE][0]['tid'];
$news_type = taxonomy_term_load($news_type_id);
$programmes = $node->field_news_programme[LANGUAGE_NONE];
$type_name = $news_type->name;
$type_icon = path_icon($news_type->field_news_icon[LANGUAGE_NONE][0]['icon']);
$description = !empty($node->field_short_description[LANGUAGE_NONE]) ? $node->field_short_description[LANGUAGE_NONE][0]['value'] : "";
$date = $node->field_news_publist_date[LANGUAGE_NONE][0]['value'];
$program_name = array();
$program_short_name = array();
$node_url = drupal_get_path_alias('node/' . $nid);
?>
<div class="_2block nomar special">
<a href="<?php print $node_url;?>" class="nl2 bgimage w-inline-block" style="background-image:linear-gradient(180deg, rgba(21, 36, 49, 0), #152431), url('<?php echo $image;?>');background-repeat:no-repeat;background-size:cover;">
<div class="pagetype">
<div><span class="fa fa-<?php print $type_icon;?>"></span> <?php echo $type_name;?></div>
</div>
<div class="card-text"></div>
<?php if(!empty($programmes)){ ?>
<div class="tag">
<?php foreach($programmes as $key2 => $pro){
$programme = taxonomy_term_load($pro['tid']);
$bundle = $programme->field_icon_programme[LANGUAGE_NONE][0]['bundle'];
$icon = path_icon($programme->field_icon_programme[LANGUAGE_NONE][0]);
$program_name[] = $programme->name;
$program_short_name[] = $programme->field_short_name[LANGUAGE_NONE][0]['value'];
?>
<i class="<?php echo $icon;?>"></i>
<?php } ?>
<br>
</div>
<?php } ?>
<div class="card-text forcedown">
<div class="cardtitle bigct"><?php echo $title;?></div>
<div class="tagtext alt"><?php echo date("d M Y", strtotime($date));?></div>
<div><?php echo $description;?></div>
<?php if(!empty($program_short_name)){?>
<div class="tagtext"><?php echo implode($program_short_name, ', ');?></div>
<?php }?>
</div>
</a>
</div>
<?php
if($i == 1) {
break;
}
}
?>
</div>
First thing , view provide several "Display" for one view created. It let you create some various display of this view to handle and manage data/display.
- You can create a display like a "Page" so your view will have own url.
- You can create another display like a "Block" , so it provide a block you can add to region
Small tutorial for view creation
Second thing is to understand templating hook override available , so you can change html and displaying for a view:
If my view is named "test" and my display is named "block" , i can override with file
views-view--test--block.tpl.php
How theming function work
Third thing , template must not contain functions calls , it can create some performance issues and it's not a good practice. Only $variables must be echo or printed.
You can take a look into views module , under "themes" folder to understand.
Finally , if you need to modify some data before rendering you have several hooks usable into custom modules or template.php which allow you to modify it
List of hooks available
Hope it helps you :)

using wp_get_attachment_image_src in foreach loop

I have 10 images added to a post via Advanced Custom Fields, they're named from 1 to 10 e.g 'image_*', ACF is set to return the ID.
I'm trying to get the full size image URL of each image in the loop and use it as a href attribute to open a full size popup of the image, however I don't understand how wp_get_attachment_image_src works.
Since I am unable to use Advanced Custom Field's Repeater, this is the loop i'm using to get an array of the images with a custom image size of scaled, it works fine to generate the responsive image markup that I need:
// args
$sizeHuge = 'scaled'; // scaled image
$images = array(); // img array
for($x = 1; $x <= 10; $x++) {
$img = get_field('image_' . $x);
if($img) {
$images[] = $img;
} else {
break;
}
}
<?php foreach($images as $image) { ?>
<a href="" class="open-viewer">
<?php echo wp_get_attachment_image( $image, $sizeHuge ); ?>
</a>
<?php } ?>
I need to set the href attribute of the parent a element with the URL of the image. This is what I have tried with wp_get_attachment_image_src, it doesn't work, it sets every href with image_10's URL.
// args
$sizeFull = 'full'; // full size image
$sizeHuge = 'scaled'; // scaled image
$images = array(); // img array
for($x = 1; $x <= 10; $x++) {
$img = get_field('image_' . $x);
$image_array = wp_get_attachment_image_src($img, $sizeFull);
$link = $image_array[0];
if($img) {
$images[] = $img;
} else {
break;
}
}
<?php foreach($images as $image) { ?>
<a href="<?php echo $link; ?>" class="open-viewer">
<?php echo wp_get_attachment_image( $image, $sizeHuge ); ?>
</a>
<?php } ?>
My question is: How can I set the href of a.open-viewer with the correct URL?, and secondly, why does my code fail? (debug is switched on but no errors appear).
I realise I've horribly misunderstood something here, I'm a PHP novice so any advice about my approach would be appreciated.
In your first block, in your loop, you're setting the value of $link to the value of $image_array[0], but you're overwriting it each time. You want array_push here.
$images = array();
for($x = 1; $x <= 10; $x++) {
$img = get_field('image_' . $x);
$image_array = wp_get_attachment_image_src($img, $sizeFull);
if($image_array && $image_array[0]) {
array_push($images,
array(
src => $image_array[0],
id => $img
)
);
} else {
break;
}
}
Now, when you loop over it the second array, you can just do:
<?php foreach($images as $image) { ?>
<a href="<?php echo $image['src']; ?>" class="open-viewer">
<?php echo wp_get_attachment_image( $image['id'], $sizeHuge ); ?>
</a>
<?php } ?>
And the value of href should be the image URL.
Following is the updated/corrected code which will work for you:
<?php
// args
$sizeFull = 'full'; // full size image
$sizeHuge = 'scaled'; // scaled image
$images = array(); // img array
for($x = 1; $x <= 10; $x++) {
$img = get_field('image_' . $x);
if($img) {
$images[] = $img;
} else {
break;
}
}
<?php foreach($images as $image) {
$image_array = wp_get_attachment_image_src($image, $sizeFull);
$link = $image_array[0];
?>
<a href="<?php echo $link; ?>" class="open-viewer">
<?php echo wp_get_attachment_image( $image, $sizeHuge ); ?>
// or Rather than calling above function, why don't you write <img> tag as you already have image url ? Like:
<img src="<?php echo $link; ?>" class="">
</a>
<?php } ?>

Categories and Sub Categories in custom taxonomies

I find and use the following code to display sub categories for default taxonomy 'category' on my site, but I created a custom taxonomy , and can not alter the code so that he was doing the same thing for the new taxonomy, help please
<?php
if(is_category()) {
$subcategories = get_terms('category', 'parent='.get_query_var('cat'));
if(empty($subcategories)) {
$thiscat = get_term(get_query_var('cat'),'category');
$subcategories = get_terms('category', 'parent='.$thiscat->parent.'');
}
if(empty($subcategories)) $subcategories = array();
if(!empty($subcategories)) {
echo '<ul>';
foreach($subcategories as $subcat) {
if(get_query_var('cat') == $subcat->term_id) $current = ' current-cat'; else $current = '';
echo '
<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
'.$subcat->name.'
</li>';
}
echo '</ul>';
}
}
else {
// If no current cat query, just get the top level ones using wp_list_categories.
?>
<ul>
<?php wp_list_categories('title_li=&depth=1');?>
</ul>
<?php
}
?>
It turned out, the code works as I wanted, but if it can be optimize I'll be glad to help ... This code displays the subcategories to the main categories of taxonomies.
If anyone should use, just change 'auto' to the name of your taxonomy. my custom taxonomy is 'auto'
<?php
if(is_tax()) {
$subcategories = get_terms('auto', 'parent='.get_queried_object()->term_id);
if(empty($subcategories)) {
$thiscat = get_term(get_queried_object()->term_id,'auto');
$subcategories = get_terms('auto', 'parent='.$thiscat->parent.'');
}
if(empty($subcategories)) $subcategories = array();
if(!empty($subcategories)) {
echo '<ul>';
foreach($subcategories as $subcat) {
if(get_queried_object()->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
echo '
<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
'.$subcat->name.'
</li>';
}
echo '</ul>';
}
}
else {
// If no current cat query, just get the top level ones using wp_list_categories.
?>
<ul>
<?php wp_list_categories('taxonomy=auto&title_li=&depth=1');?>
</ul>
<?php
}
?>
Thanks for the corrected code.
I have added a bit for people who want to exit of the loop once they come to any end. Instead of showing the parent categories tree i modified a bit to display the image and title of the of the products or services under that specific category. Following is the code
if(is_tax()) {
$subcategories = get_terms('product_categories', 'parent='.get_queried_object()->term_id);
// this is the change which display all the products in the specific category if it does not have child categories
if(empty($subcategories)) {
$thiscat = get_term(get_queried_object()->term_id,'product_categories');
if (have_posts('thiscat')):while (have_posts('thiscat')) : the_post('thiscat');{?>
<div class="box cat-item-<?php $subcat->name.$current ;?>" style="min-height: 231px; margin-bottom:10px">
<?php the_post_thumbnail('thumbnail');?>
<h5><a title="<?php the_title(); ?>" href="<?php the_permalink($post->ID); ?>"><?php the_title(); ?></a></h5></h5></div>
<?php }
endwhile;
else : {
?>
<div class="box cat-item cat-item-<?php $subcat->name.$current; ?>" style="min-height: 231px; margin-bottom:10px">
<h5>Products will be upadated later. Do visit Later </h5 >
</div>
<?php }
endif;
}
if(empty($subcategories)) $subcategories = array();
if(!empty($subcategories)) {
echo '<div class="fixed-row clearfix dynamic-fixedRow-735" id="row_order_2">';
foreach($subcategories as $subcat) {
if(get_queried_object()->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
echo '
<div class="box cat-item cat-item-'.$subcat->name.$current.'" style="min-height: 231px; margin-bottom:10px"><h5>
'.$subcat->name.'
</h5></div>';
}

Wordpress post query php custom field conditional

Here's the situation:
In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.
All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.
Here's my WP_Query:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).
I've marked what I edited with comments (just one line).
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>

Resources