wordpress category name in shortcode php - php

i want to display the current category name in code below in my sidebar but i cant. Can anyone help me?
Thank you.
<?php
$weather_city = the_category_name();
if ($weather_city)
echo do_shortcode('[forecast location="' . $weather_city . ', Greece" caption="" measurement="C" todaylabel="Σήμερα" datelabel="%%weekday%%" highlow="%%high%%° / %%low%%°" numdays="4" iconset="Incredible" class="css_table_class"]'); ?>

$current_categories = get_the_category(); // array of objects
if (!empty($current_categories)) {
$category = reset($current_categories); // take the first one
$weather_city = $category->cat_name;
echo do_shortcode('[forecast location="' . $weather_city . ', Greece" caption="" measurement="C" todaylabel="Σήμερα" datelabel="%%weekday%%" highlow="%%high%%° / %%low%%°" numdays="4" iconset="Incredible" class="css_table_class"]');
}

Related

WordPress: Get ID of custom post type within nested shortcodes

I'm currently having trouble getting the ID of a custom post type from within a nested shortcode. I'm not sure if this is possible.
Further information to describe my scenario:
I've created an "Activity" custom post type
I've created a shortcode called "activity" which displays the
contents of an activity custom post type based on its ID, this can be
set using the shortcode attributes
I've created a shortcode called "textarea" which displays a textarea
within an acitivty custom post type
The issue that I'm having is that I'm trying to get the "Activity ID" from within the Textarea shortcode but I can't figure a way of doing this.
Please see a diagram via this link to better describe my situation:
https://www.dropbox.com/s/a9bgyjdq9m92qpg/WP-Support---Get-ID-within-Nested-Shortcodes.png?dl=0
Activity Shortcode Code:
function display_activity($atts, $content = null){
$a = shortcode_atts( array(
'id' => null,
), $atts ) ;
if (!is_null($a['id'])){
$activityId = intval($a['id']);
if (is_int($activityId) && $activityId > 0){
$activityObj = get_post($activityId);
$theTitle = sanitize_post_field( 'post_title', $activityObj->post_title, $activityId, 'display' );
$theContent = apply_filters('the_content', $activityObj->post_content);
$html = "<div id='sbusuws-activity-" . $activityId . "' class='sbusuws-activity'>";
$html .= "<h3 class='sbusuws-activity-title'>" . $theTitle . "</h3>";
$html .= "<div class='sbusuws-activity-content'>";
$html .= do_shortcode($theContent);
$html .= "</div>";
$html .= "</div>";
return $html;
}
}
}
Textarea Shortcode:
function display_textarea($atts){
$activityId = ""; // <--- This is the problem
$textareaId = $activityId . "-sbusuws-activity-textarea";
$html = "<textarea id='" . $textareaId . "' rows='5' cols='20'>";
return $html;
}
The idea was to make the textarea shortcode as simple as possible, ie: [textarea]
I'd appreciate any help with this issue.
Thanks!
try this in your display_activity method if it works for you ..
$theContent = str_replace('[textarea]','[textarea id="'.$activityId.'"]', $theContent);
$html .= "<div class='sbusuws-activity-content'>";
$html .= do_shortcode($theContent);
$html .= "</div>";

Showing specific information of current user logged in, in PHP

I'm using this 2 plugins
http://www.wpexplorer.com/wordpress-user-contact-fields/
and
WooCommerce
The 1st parameter, in this case the number "1" refers to the id of the user, how con I change it to be dynamic? So, depending on the user I get its own specific information
<h2>Personal</h2>
<?php
echo '<ul>';
echo '<li>Direccion: ' .get_user_meta(1,'address',true) . '</li>';
echo '<li>Compañia: ' .get_user_meta(1,'company',true) . '</li>';
echo '<li>Birth dAte: ' .get_user_meta(1,'birth',true) . '</li>';
echo '<li>Gender: ' .get_user_meta(1,'gender',true) . '</li>';
echo '<li>phone: ' .get_user_meta(1,'phone',true) . '</li>';
echo '</ul>';
?>
thanks
Don't try to make a function do something it wasn't intended to do. Write your own. Especially for something this simple.
function getUserStuff($id, $item){
$item = mysql_real_escape_string($item);
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT `".$item."` FROM `users` WHERE `id` = '".$id."'");
$z = mysql_fetch_assoc($q);
return (mysql_num_rows($q) > 0) ? $z[$item] : false;
}
This is just an example. I used a deprecated function for simplicity but you should use a different API.

PHP Conditional Field Display Joomla

Basically I need to call a field (wrapped in a div) if the field has a value. I do not want the field or div to display if the field has no value. My PHP knowledge is dire but here is what I have to work with.
Here are instructions provided to me on how to call a custom field by ID:
$cust_1 = $this->fields->getFieldByCaption('Custom Text'); // getFieldByCaption() allow you to get the field by the Caption. This is not the best way to get a field since changing the caption in the back-end will break the reference.
echo '<br />Field ID: ' . $cust_1->getId();
$cust_2 = $this->fields->getFieldById(29); // getFieldById() is the ideal way of getting a field. The ID can be found at 'Custom Fields' section in Mosets Tree's back-end.
echo '<br />Name: ' . $cust_2->getName();
echo '<br />Has Caption? ' . (($cust_2->hasCaption()) ? 'Yes' : 'No');
echo '<br />Caption: ' . $cust_1->getCaption();
echo '<br />Value: ' . $cust_2->getValue();
echo '<br />Output: ' . $cust_2->getOutput(1);
echo '<hr />';
$this->fields->resetPointer();
while( $this->fields->hasNext() ) {
$field = $this->fields->getField();
echo '<br /><strong>' . $field->getCaption() . '</strong>';
echo ': ';
echo $field->getOutput(1); // getOutput() returns the formatted value of the field. ie: For a youtube video, the youtube player will be loaded
// echo $field->getValue(); // getValue() returns the raw value without additional formatting. ie: When getting value from a Online Video field type, it will return the URL.
$this->fields->next();
}
Here is the code I am working with and need help to try and do what I need it to as detailed above:
<?php
if( !is_null($this->fields->getFieldById(35)) ) {
$value = $field->getValue();
$hasValue = $field->hasValue();
$field = $this->fields->getField();
if( $hasValue )
{
echo '<div class="lin" id="lphn">'; {
echo $cust_2 = $this->fields->getFieldById(35);
echo $cust_2->getOutput(1);
echo '</div>';
}
else {
// Print nothing.
}
}
}
?>

Fetch Product Images with BigCommerce API

Long story short a couple months ago i made a plugin for WordPress with the Bigcommerce API to fetch products in the Widget Area.
Now I have updated the Single File "Bigcommerce.php" and now the Function getProductImages () is none existent. And I cant seem to find the new function to get the Product Images. Maybe its just to late and Im tired or just plain Blind.
Please let me know how to fetch now the image for a specific product.
See below for the old code used i reverted back to the Old "Bigcommerce.php" and it works again but would ratehr use the new way.
Bigcommerce::configure(array(
'store_url' => $store_url,
'username' => $username,
'api_key' => $api_key
));
Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer(false);
$countProducts = 0;
$products = Bigcommerce::getProducts();
shuffle($products);
echo '<div class="BCStoreWidget">';
if (!$products) {
echo '<div class="BCerror">';
$error = Bigcommerce::getLastError();
echo $error->code;
echo $error->message;
echo '</div>';
} else {
foreach ($products as $product) {
$productImages = Bigcommerce::getProductImages($product->id);
echo '<h4>' . $product->name . '</h4>';
if ($productImages->image_file){
echo '<div class="pimage">';
echo '<img src="' . $store_url . '/product_images/' . $productImages->image_file . '">';
echo '</div>';
}
// echo '<p>' . substr($product->description,0,100) . ' ...</p>';
echo '<p>' . number_format($product->price, 2, '.', '') . ' USD</p>';
echo '<p> Buy Now </p>';
$countProducts++;
if ($countProducts == $max_show)
break;
}
}
echo '</div>';
Thank you all in Advance
You can get product images just by getting the "images" attribute of the product object. So, all you have to do is:
$productImages = $product->images;
You can also access images directly using the getCollection function:
Bigcommerce::getCollection('/products/'.$product_id.'/images/');

Wordpress : Display Text While a period

I have a question.
How can display a text while 4 days in PHP ?
Example : I wrote a post today, and I want display "New" while 4 days and hide this after that.
Thanks a lot.
This is NOT specific to WordPress, and you'll want to remove the foreach loop for actual use.. but it should give you an idea of how to compare relative dates:
<?php
$sample_dates = array(
'2011-02-17',
'2011-02-18',
'2011-02-19',
'2011-02-20',
'2011-02-21',
'2011-02-22',
'2011-02-23'
);
$post_date = '2011-02-17';
foreach ($sample_dates as $date_today) {
echo '<p>If today is ' . $date_today . ', the post from ' . $post_date . ' is: ';
if (strtotime('+4 days',strtotime($post_date)) >= strtotime($date_today)) {
echo 'NEW';
} else {
echo 'OLD';
}
echo '</p>' . PHP_EOL;
}
?>

Categories