I have a real-estate website in wordpress.
Under each listed property is displayed a corresponding agent. Everything works fine, but the problem is that i want that agent's featured image to be displayed as well.
Here is the code for displaying the agent under each property
<?php
//// CHECKS TO SEE IF WE HAVE ANY GENTS ASSIGNED TO THIS PROPERTY
if(get_post_meta(get_the_ID(), 'agents', true) != '' || ddp('public_submissions') == 'on') :
?>
<div id="property-agents">
<h2><?php _e('Contact Agent', 'btoa'); ?></h2>
<ul class="list-agents">
<?php
//// LETS LOOP OUR AGENTS
$agents = get_post_meta(get_the_ID(), 'agents', true);
//// IF WE HAVE AN ARRAY
if(is_array($agents)) :
foreach($agents as $agent) :
$this_agent = '';
//// IF IT'S A POST TYPE
if($this_agent = get_post($agent)) {
if($this_agent->post_type == 'agent') {
$name = $this_agent->post_title;
$position = get_post_meta($this_agent->ID, 'position', true);
$email = get_post_meta($this_agent->ID, 'email', true);
$phone = get_post_meta($this_agent->ID, 'phone', true);
} else { $this_agent = 'user'; }
} else { $this_agent = 'user'; }
if($this_agent == '' || $this_agent == 'user') {
if($this_agent = get_user_by('id', $agent)) {
$name = $this_agent->display_name;
$position = esc_attr( get_the_author_meta( 'position', $this_agent->ID ) );
$email = $this_agent->user_email;
$phone = get_the_author_meta( 'phone', $this_agent->ID );
}
}
//// IF WE HAVE GOTTEN THE USERS NAME
if($name) :
?>
<li>
<div class="two-fifths">
<h4><?php echo $name; ?></h4>
<h5><?php echo $position; ?></h5>
</div>
<!-- /.two-fifths/ -->
<div class="three-fifths last">
<?php if($email != '') : ?><div class="three-fifths"><strong><?php _e('Email', 'btoa'); ?></strong><?php echo $email; ?></div><?php endif; ?>
<?php if($phone != '') : ?><div class="two-fifths last"><strong><?php _e('Phone', 'btoa'); ?></strong><?php echo $phone; ?></div><?php endif; ?>
</div>
<!-- /.three-fifths/ -->
</li>
<?php endif; endforeach; endif; ?>
</ul>
</div>
<!-- /#property-agents/ -->
and this is the code for gettin the featured image
function ddTimthumb($img = NULL, $width = NULL, $height = NULL) {
//// IF AN imAGE HAS BEEN PROVIDED
if($img != NULL) {
$image = vt_resize('', $img, $width, $height, true );
//// IF ITS NOT AN ERROR
if(is_array($image)) { return $image['url']; } else { return ''; }
} else { return ''; }
}
function getFeaturedImage($post_id = NULL) {
if($post_id != NULL) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'single-post-thumbnail' );
return $image;
}
}
You can use your function to get featured image.
<?php echo getFeaturedImage($this_agent->ID); ?>
Related
I have 2 functions. One is to calculate fee (here is part from it that is responsible for calculating fee and saving it into global variable):
function pre_checkout()
{
global $woocommerce;
$section2 ='';
$section3 ='';
$section2 = sanitize_text_field($_POST["user_data"]["section2"]);
$section3 = sanitize_text_field($_POST["user_data"]["section3"]);
$how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section1"]);
$boxToFind = '';
if(($section2 == 'toy_yes') || ($section2 == 'toy_no'))
{
$boxToFind = sanitize_text_field($_POST["user_data"]["section1"]);
}
else{
$boxToFind = sanitize_text_field($_POST["user_data"]["section1"].$_POST["user_data"]["section2"]);
}
WC()->cart->empty_cart(true);
$boxesLookupTable = array(
//susbscriptions
'small1month'=>'112',
'medium1month'=>'113',
'large1month'=>'114',
'small6month'=>'115',
'medium6month'=>'116',
'large6month'=>'117',
'small12month'=>'118',
'medium12month'=>'119',
'large12month'=>'120',
//gifts
'gift1month'=>'133',
'gift3month'=>'134',
'gift6month' =>'135',
'gift12month' =>'136'
);
$productId = $boxesLookupTable[$boxToFind];
// echo $productId;
//check if product already in cart
// if no products in cart, add it
WC()->cart->add_to_cart($productId,1);
if($section2 === "toy_yes"){
$product = wc_get_product($productId);
$price = $product->get_price();
$toy_price = 4.99;
unset($_COOKIE['product_price_with_toy']);
setcookie('product_price_with_toy',$toy_price*$how_many_months,time() + (86400 * 30),"/");
// WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
/* "gift"; */
}
if($section3 === "toy_yes"){
$how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section2"]);
$product = wc_get_product($productId);
$price = $product->get_price();
$toy_price = 4.99;
unset($_SESSION['product_price_with_toy']);
$_SESSION['product_price_with_toy'] = $price + $toy_price;
unset($_COOKIE['product_price_with_toy']);
setcookie('product_price_with_toy',$how_many_months * $toy_price,time() + (86400 * 30),"/");
// WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
/*"subscription"; */
};
$items = WC()->cart->get_cart();
global $woocommerce;
$cart_url = wc_get_cart_url();
$checkout_url = wc_get_checkout_url();
foreach($items as $item => $values) {
$_product = $values['data']->post;
?>
<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<?php echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); ?>
</div>
<div class="dropdown-cart-right">
<h5><?php echo $_product->post_title; ?></h5>
<p><strong>Quantity:</strong>
<?php $product = wc_get_product($productId);
woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
?></p>
<?php global $woocommerce;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( $values['product_id'], '_regular_price', true);
$sale = get_post_meta( $values['product_id'], '_sale_price', true);
?>
<?php if($sale) { ?>
<p class="price"><strong>Price:</strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>
<?php } elseif($price) { ?>
<p class="price"><strong>Price:</strong> <?php echo $currency; echo $price; ?></p>
<?php }
?>
<div class="dropdown-cart-right dropdown-checkout-link">
<a class="checkout-submit checkout green-btn text-center" href="#">Order</a>
</div>
</div>
<div class="clear"></div>
</div>
<?php }
die();
}
add_action('wp_ajax_pre_checkout','pre_checkout');
add_action('wp_ajax_nopriv_pre_checkout', 'pre_checkout');
Next i have woocommerce action:
function add_cart_fee() {
$chuj = $GLOBALS['product_price_with_toy'];
WC()->cart->add_fee( __('Extra Gift Fee', 'woocommerce'),$chuj );
}
add_action( 'woocommerce_cart_calculate_fees', 'add_cart_fee' );
And basicaly toy fee is always 0 when im using session, global variables - only through cookies is passing values to action - i dont understand why woocommerce / wordpress is wiping global value that ive set inside my calculation function. Or maybe someone know how to update the total price with using hooks and actions, that would be even better than using fee field.
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 } ?>
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>
I use code like this, but unfortunately did not work. The error is the image failed to load, but the code display on the page.
<?php
$nextthumb = get_the_post_thumbnail($nextPost->ID);
$img_urltest = wp_get_attachment_url( $nextthumb,'full' );
$imagetest = aq_resize( $img_urltest, 500,325, true );
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' );
$image = aq_resize( $img_url, 500,325, true );
?>
<img src="
<?php if($image) {
echo $image;
}
else {
echo $nextthumb;
}
?>
"alt="<?php echo get_bloginfo('name'); ?>"/>
I'm trying to use a metabox checkbox to hide/display on hover. The problem is it shows the div if any of the posts have the check box checked. I need it to toggle the div "on" & "off" depending on if the checkbox is checked. Any help would be greatly appreciated.
Here is the code in my functions.php:
`// Checkbox Meta
add_action("admin_init", "checkbox_init");
function checkbox_init(){
add_meta_box("checkbox", "Check to Show Bubbles", "checkbox", "homefeature", "normal", "high");
}
function checkbox(){
global $post;
$custom = get_post_custom($post->ID);
$field_id = $custom["field_id"][0];
echo '<label>Show Bubbles?</label>';
$field_id_value = get_post_meta($post->ID, 'field_id', true);
if($field_id_value == "yes") {
$field_id_checked = 'checked="checked"';
}
echo ' <input type="checkbox" name="field_id" value="yes" '.$field_id_checked.' />';
}
// Save Meta Details
add_action('save_post', 'save_details');
function save_details(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "field_id", $_POST["field_id"]);
}
function custom_content($id) {
$field_id = get_post_meta($id, 'field_id', true);
if($field_id == yes) {
echo '<script type="text/javascript">';
echo '$("this").mouseover(function() {';
echo "$('#mainFeatureFlashBG').css({'display' : 'block'})";
echo '}</script>';
}
else{
echo '<script type="text/javascript">';
echo '$("this").mouseover(function() {';
echo "$('#mainFeatureFlashBG').css({'display' : 'none'})";
echo '}</script>';
}
}`
Here is my php:
<ul>
<!-- Begin Miller Beer Logo Query-->
<?php
$args=array(
'beerlogo'=>'miller',
'post_type' => 'homefeature',
'post_status' => 'publish',
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li class="image-rollover"><a href="<?php echo esc_url(get_post_meta($post->ID, 'homefeature_custom_link', true));
?>"><?php the_post_thumbnail('full'); ?>
<?php
//Is the Checkbox for Bubbles Checked?
custom_content(get_the_ID());
?>
</a>
</li>
<?php
endwhile;
}
wp_reset_query();
?>
</ul>
You should check with:
<?php
if(get_post_meta($post->ID,'field_id',true) != '') {
// do something, output div
} ?>
See function reference here:
http://codex.wordpress.org/Function_Reference/get_post_meta