PHP echo custom fields with $out - php

i am using the Favorites plugin in Wordpress to save posts. I'm trying to adjust the way the information is displayed though. All the styling etc works below and my divs are being included, however i am struggling to pull information through to fill the divs. Fore example, i am trying to pull the excerpt in to the p.details but it is just throwing out an empty result, with no errors. Similarly, i am trying to pull through an acf custom field of 'bath' into p.bath but that is also empty. Any suggestions? Thanks in advance.
if ( is_multisite() ) switch_to_blog($this->site_id);
$out = '<ul class="property-list" data-userid="' . $this->user_id . '" data-links="true" data-siteid="' . $this->site_id . '" ';
$out .= ( $include_button ) ? 'data-includebuttons="true"' : 'data-includebuttons="false"';
$out .= ( $this->links ) ? ' data-includelinks="true"' : ' data-includelinks="false"';
$out .= ' data-nofavoritestext="' . $no_favorites . '"';
$out .= ' data-posttype="' . $post_types . '"';
$out .= '>';
foreach ( $favorites as $key => $favorite ){
$out .= '<li data-postid="' . $favorite . '">';
$out .= '<div class="third-1">';
$out .= '<a class="property-thumb" href="' . get_permalink($favorite) . '">';
$out .= '</a>';
$out .= '</div>';
$out .= '<div class="third-2">';
if ( $this->links ) $out .= '<h3 class="name"><a href="' . get_permalink($favorite) . '">';
$out .= get_the_title($favorite);
if ( $this->links ) $out .= '</a></h3>';
if ( $this->links ) $out .= '<h4 class="price">';
$out .= '£' . '300';
if ( $this->links ) $out .= '</h4>';
if ( $this->links ) $out .= '<p class="details">';
$out .= the_excerpt();
if ( $this->links ) $out .= '</p>';
if ( $this->links ) $out .= '<p class="bed">';
$out .= '1';
if ( $this->links ) $out .= '</p>';
if ( $this->links ) $out .= '<p class="bath">';
$out .= '1';
if ( $this->links ) $out .= '</p>';
if ( $this->links ) $out .= '<a class="full-details" href="' . get_permalink($favorite) . '">';
$out .= 'Full details';
if ( $this->links ) $out .= '</a>';
if ( $this->links ) $out .= '<a class="book-viewing" href="' . get_permalink($favorite) . '">';
$out .= 'Book Viewing';
if ( $this->links ) $out .= '</a>';
$out .= '</div>';
$out .= '</li>';
}

Change the_excerpt(); to get_the_excerpt(); and it should work.

Related

contenation of strings in php

I want to concatenate of multiple strings with condition
I am doing a loadmore in ajax and php and i want to dynamic the response, so i have tried below code
$newList .= '<ul>';
$newList .= '<li>';
$newList .= '<i class="fa fa-calendar"></i>';
$newList .= $this->Common_model->startend_date_format($getEvent['event_stat'], $getEvent['event_end']);
$newList .= '</li>';
$newList .= '<li>';
$newList .= '<i class="fa fa-map-marked"></i>';
if ($city != "") {
$newList .= '<a href=' . base_url() . 'city/' . $this->Common_model->makeSeoUrl($city) . '>' . ucfirst($city) . '</a>, ' ;
}
if ($state != "") {
$newList .= '<a href="' . base_url() . 'state/' . $this->Common_model->makeSeoUrl($state) . '>' . ucfirst($state) . '</a>, &nbsp';
}
if ($get_country_name != "") {
$newList .= '<a href=' . base_url() . 'country/' . $this->Common_model->makeSeoUrl($get_country_name) . '/' . base64_encode($get_country_id) . '>' . ucfirst($get_country_name) . '</a>';
}
$newList .= '</li>';
$newList .= '<li>';
$newList .= '<i class="fa fa-tags"></i>';
$newList .= '<a href=' . base_url() . 'categories' . '#' . $cat_name_event . '>' . $cat_name_event . '</a>';
$newList .= '</li>';
$newList .= '</ul>';
and it is showing something like
but the actual result should be like

PHP Printing JSON Array Shows the Word "Array" At Top Of Values

Have the following Array that functions perfectly on the site, but before the first output the word "Array" is printed.
I figure it has to be in the $json_object or $fbdata query but cannot isolate or eliminate it from showing.
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p>' . $post->message . '</p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><img src="' . $post->full_picture . '"></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
Declare $posts as a String before your foreach loop.
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
$posts = ''; // Add this
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p>' . $post->message . '</p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><img src="' . $post->full_picture . '"></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
Otherwise it is making $posts into an array, so when it is printed, it also prints that it's an array.
Figured it out. I changed my 'posts' variable to 'postst'. The word 'post' was being used in the URL and that seems to have confused the array in some way.
Updated code:
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$postst .= '<div class="col-sm-4">';
$postst .= '<div class="stay-connected-inner">';
$postst .= '<div class="stay-connected-info">';
$postst .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$postst .= '<div class="stay-connected-right">';
$postst .= '<h5>Title</h5>';
$postst .= '<p>' . $post->message . '</p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '<div class="stay-connected-fig">';
$postst .= '<p><img src="' . $post->full_picture . '"></p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '</div>';
}
//Display the posts
print_r ($postst);
?>

How can I echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.

Writing out the category of a product from Duka Press Plugin

I'm trying to modify a wordpress plugin called Duka Press, and I'm a complete noobie to PHP. It uses shortcodes which produces the template for the product grid, and I basically just need to write out all the categories the product has to create jQuery filter function.
To better understand what I need read the HTML comment in the code underneath
CODE:
<ul>
I need this for each category inside the active category set i the shortcode:
<li>[CategoryNameHere]</li>
</ul>
$products = get_posts($order_string . 'numberposts=' . $per_page . '&post_type=' . $type . '&meta_key=price&category=' . $category . $offset);
if (is_array($products) && count($products) > 0) {
$content .= '<div class="dpsc_grid_display">';
$count = 1;
$all_count = 0;
foreach ($products as $product) {
$output = dpsc_get_product_details($product->ID, $p_b_n, $direct_checkout);
if ($output) {
$attachment_images = &get_children('post_type=attachment&post_status=inherit&post_mime_type=image&post_parent=' . $product->ID);
$main_image = '';
foreach ($attachment_images as $image) {
$main_image = $image->guid;
break;
}
$prod_permalink = get_permalink($product->ID);
$content .= '<div class="dpsc_grid_product">';
$content .= '<div class="dpsc_grid_product_image">';
if ($main_image != '') {
$content .= '<img src="' . DP_PLUGIN_URL . '/lib/timthumb.php?src=' . $main_image . '&w=' . $dp_shopping_cart_settings['g_w'] . '&h=' . $dp_shopping_cart_settings['g_h'] . '&zc=1" >';
}
$content .= '</div>';
<!-- In the following line i need to write out all the categories for the current product -->
$content .= '<div class="dpsc_grid_product_detail [Categories here]">';
$content .= '<p class="title">' . __($product->post_title) . '</p>';
$content .= '<p class="detail">' . $product->post_excerpt . '</p>';
$content .= '<p class="price">' . $output['price'] . '</p>';
$content .= $output['start'];
$content .= $output['add_to_cart'];
$content .= $output['end'];
$content .= '</div>';
$content .= '</div>';
if ($count === intval($column)) {
$content .= '<div class="clear"></div>';
$count = 0;
}
$count++;
$all_count++;
}
}
$content .= '<div class="clear"></div>' . $page_links . '<div class="clear"></div>';
$content .= '</div>';
$content .= '<div class="clear"></div>';
}
return $content;
Shortcode that uses the template above:
[dpsc_grid_display category="22" total="100" column="3" per_page="100" type="duka"]

Terms by vocabulary in node.tpl

I've created a variable in template.php that let's me print terms by vocabulary. The problem is that I want to be able to pass in a vocabulary id to select a specific vocabulary. My code looks like this:
function xnalaraart_classic_print_terms($node, $vocabularies){
foreach($vocabularies as $vocabulary){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
}
return $output;
}
and in the preprocess_node function:
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node']);
How do I write it so that I can pass in an id to $vocabularies?
I think you made this more difficult on yourself than it really is. See below for final function.
function xnalaraart_classic_print_vocab_terms($node, $vid){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
return $output;
}
And then call
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node'], 10); //Where 10 is the vocab ID

Categories