Tumblr V2 API PHP wrapper - php

I am trying to integrate a Tumblr blog into my website, using the TumblrPHP wrapper available at https://github.com/gregavola/tumblrPHP. This is code I'm using to view the posts on my site:
<?php
include ('lib/tumblrPHP.php');
$consumer = 'key';
$secret = 'key';
$tumblr = new Tumblr($consumer, $secret);
$posts = $tumblr->get('/blog/nyhetergaius.tumblr.com/posts');
foreach($posts->response->posts as $posts) {
?>
<h2><?php echo date('Y-m-d', $post->timestamp) ?></h2>
<?php if ($post['type'] == 'regular') { ?>
<?php echo $post{'body'}; ?>
<?php } ?>
<?php
if ($post->type == 'photo') {
foreach ($post->photos as $photo) {
?>
<img src="<?php echo $photo->alt_sizes[1]->url ?>" />
<?php
}
echo $post->caption;
}
else
echo $post->body;
?>
<?php
}
?>
I am using my own key and secret key, but even so I can only retrieve a default date for each post. What am I doing wrong? Is there an easier way to view the posts as they appear on the blog http://nyhetergaius.tumblr.com/? This is how the posts appear on my site: http://test.gaius.nu/om.php.
Thank you for your support.

I was able to make this work by replacing $post['type'] with $post->type etc..
foreach($posts->response->posts as $posts) {
$postDate = date('Y-m-d', $posts->timestamp);
echo "<h2>$postDate</h2>";
if ($posts->type == 'regular') {
echo $posts->body;
}
elseif ($posts->type == 'photo') {
foreach ($posts->photos as $photo) {
$imgURL = $photo->alt_sizes[1]->url;
echo "<img src=\"$imgURL\" />";
}
echo $posts->caption;
}
else
{
echo $posts->body;
}
}

Related

Display an image depending on the current URL

I am setting up a website and want to display a different image depending on the current page URL
If http://webpage.com/test/en/ then display image 1
If http://webpage.com/test/fr/ then display image 2
If http://webpage.com/test/nl/ then display image 3
New to this but managed to do something similar but there were only 2 options! any help appreciated:
<?php if ($autoshowroom_video) { ?>
<iframe src="<?php echo esc_url($autoshowroom_video); ?>
?autoplay=1&loop=1&modestbranding=1&showinfo=0&rel=0&iv_load_policy=3&controls=0" width="310" height="205" frameborder="0"></iframe>
<?php } else { ?>
<video width="100%" height="auto" autoplay loop muted>
<source src="https://test.co.uk/Promo.mp4" type="video/mp4" />
<?php } ?>
I have been searching the net and found this, which although dosnt do exactly what I want would at least help me understand a little more as I am trying to learn.
<?php $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'webpage.com/test/fr/')
{
echo $this->__('French');
}
else
{
echo $this->__('Another Language');
} ?>
I finally made some progress!
I need to do this check several times, I have 5 languages on the site. Can I run several 'ifs'? or will it cancel out after the first or does it wait for a result?
<?php $language = (get_locale()) ?>
<?php if($language == 'en_GB') { ?>
<video width="100%" height="auto" autoplay loop muted>
<source src="https://test.co.uk/Promo.mp4" />
<?php } else { ?>
Not English so no video
<?php } ?>
So... as a noob I am proud of myself! I am sure there is a cleaner way to do this, but the below does what I want! Any feedback appreciated!
<?php $language = (get_locale()) ?>
<?php if($language == 'en_GB') { ?>English<?php }
if($language == 'pl_PL') { ?>Polish<?php }
if($language == 'es_ES') { ?>Spanish<?php }
if($language == 'nl_NL') { ?>Dutch<?php }
if($language == 'tl') { ?>Tagalog<?php }
if($language == 'pl_PL2') { ?>Polish 2<?php }
if($language == 'pt_PT') { ?>Portuguese<?php }
?>
You can get URI (example: http://webpage.com/test/en/)
$uri = $_SERVER['REQUEST_URI'];
Result as: /test/en/. Then, using explode() function:
$items = explode("/", $uri);
Result as an array of string:
["", "test", "en", ""]
Using $items[2]to check and select image
So... as a noob I am proud of myself! I am sure there is a cleaner way to do this, but the below does what I want! Any feedback appreciated!
<?php $language = (get_locale()) ?>
<?php if($language == 'en_GB') { ?>English<?php }
if($language == 'pl_PL') { ?>Polish<?php }
if($language == 'es_ES') { ?>Spanish<?php }
if($language == 'nl_NL') { ?>Dutch<?php }
if($language == 'tl') { ?>Tagalog<?php }
if($language == 'pl_PL2') { ?>Polish 2<?php }
if($language == 'pt_PT') { ?>Portuguese<?php }
?>

Display different content depending on post author

I am trying to display a different image on my page depending on who the Wordpress author of the post is.
So far I have tried a few scripts but none of them work. Any help is greatly appreciated. Here is what I am trying to do.
<?php $author = get_the_author(); ?>
<?php
if ( $author('author1') ) {
echo '
<img src="">;
'
} elseif ( $author('author2') ) {
echo '
<img src="">;
'
} else {
// if neither, echo something else
}
?>
The get_the_author() function return the author's display name as a string. So you have to simply compare the result of get_the_author(). There is no array or object as return value.
So I would go with the following solution using a switch instead of if:
<?php $author = get_the_author(); ?>
<?php
switch($author) {
case 'author1':
echo '<img src="">';
break;
case 'auhtor2':
echo '<img src="">';
break;
default:
// if neither, echo something else
}
?>
In case you want to use the if statement you can use the following:
<?php $author = get_the_author(); ?>
<?php
if ($author === 'author1') {
echo '<img src="">';
} elseif ($author === 'author2') {
echo '<img src="">';
} else {
// if neither, echo something else
}
?>

How to set "class = active" for second tab?

I have some tabs. In that sometimes first tab is "introduction" and sometimes "outline" is first tab. I have set introduction class=active when it comes first. But when outline comes first I am not getting how to set its class as active.
code is like below
<ul class="nav-tabs" role="tablist">
<?php
$tabs = array();
if(!$sessId == "" && !$checkcourse){
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>true, "comment"=>true);
} else if($sessId == "") {
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>false, "comment"=>true);
} else {
$tabs = array("introduction"=>false, "outline"=>true,"announcement"=>true,"discussion"=>true,"review"=>true, "student"=>true, "comment"=>false);
}
?>
<?php if($tabs['introduction']) { ?>
<li class="active">Introduction</li>
<?php } ?>
<?php if($tabs['outline']) { ?>
<li>Outline</li>
<?php } ?>
<?php if($tabs['review']) { ?>
<li>Review</li>
<?php } ?>
<?php if($tabs['student']) { ?>
<li>Student</li>
<?php } ?>
<?php if($tabs['comment']) { ?>
<li>Comment</li>
<?php } ?>
<?php if($tabs['announcement']) { ?>
<li>Announcement</li>
<?php } ?>
<?php if($tabs['discussion']) { ?>
<li>Discussion</li>
<?php } ?>
</ul>
Simple check with ternary operator is:
<?php if($tabs['outline']) {?>
<li <?=$tabs['introduction']? '' : ' class="active"';?>>Outline</li>
<?php } ?>
So you check if $tabs['introduction'] isset then you don't need class="active", else - add this class.
Update:
But as first item of tab can change, I advise to create simple foreach:
$is_first = true;
foreach ($tabs as $tab_name => $value) {
if ($value) {
echo '<li' . ($is_first? ' class="active"' : '') . '>' . $tab_name . '</li>';
$is_first = false; // fix that we have first value here
}
}
Here your main problem is how to link href value and caption.

Adding "Media query" to If/else statements in PHP

Im using the following code to echo content in wordpress based on the size of its title.
<?php
$title = the_title('','',false);
if(strlen($title) > 35):
echo content(20);
else:
echo content(45);
endif;
?>
Is there a simple way of projecting a media query before this to echo an output based on the window width so basically for mobiles and devices
As per a reply i still cant get this to work using:
<?php
if ( wp_is_mobile() ) {
echo content (20);
} else {
$title = the_title('','',false);
if(strlen($title) > 35):
echo content(20);
else:
echo content(45);
endif;
}
?>
Even simplifying the code to following doesn't seem to work:
<?php
if ( wp_is_mobile() ) {
echo content(20);
} else {
echo content(45);
}
?>
and simply uses the "else" value: echo content(45) on mobile
WordPress doesn't have any functionalities to detect window width. PHP itself cannot do that.
The most promising solution is to use wp_is_mobile():
if ( !wp_is_mobile() ) {
echo "this";
} else {
echo "that";
}

about warning invalid for foreach() in php again

Why am I getting this PHP Warning?
Invalid argument supplied for foreach()
I don't know, if my PHP trouble or not compatible, I have been change and update my PHP version.
this the code:
<?php
// Generate a latitude/longitude pair using Google Maps API
list($lat,$lng) = $foursquare->GeoLocate($location);
// Prepare parameters
$params = array("ll"=>"$lat,$lng");
// Perform a request to a public resource
$response = $foursquare->GetPublic("venues/search",$params);
$venues = json_decode($response);
?>
<?php foreach($venues->response->venues as $venue): ?>
<div class="venue">
<?php
if(isset($venue->categories['0']))
{
echo '<image class="icon" src="'.$venue->categories['0']->icon->prefix.'88.png"/>';
}
else
echo '<image class="icon" src="https://foursquare.com/img/categories/building/default_88.png"/>';
echo '<a href="https://foursquare.com/v/'.$venue->id.'" target="_blank"/><b>';
echo $venue->name;
echo "</b></a><br/>";
if(isset($venue->categories['0']))
{
if(property_exists($venue->categories['0'],"name"))
{
echo ' <i> '.$venue->categories['0']->name.'</i><br/>';
}
}
if(property_exists($venue->hereNow,"count"))
{
echo ''.$venue->hereNow->count ." people currently here <br/> ";
}
echo '<b><i>History</i></b> :'.$venue->stats->usersCount." visitors , ".$venue->stats->checkinsCount." visits ";
?>
</div>
<?php endforeach; ?>
i don't know the json data. but try this option. if i have the json may be i can't help you more detail.
<?php
// Generate a latitude/longitude pair using Google Maps API
list($lat,$lng) = $foursquare->GeoLocate($location);
// Prepare parameters
$params = array("ll"=>"$lat,$lng");
// Perform a request to a public resource
$response = $foursquare->GetPublic("venues/search",$params);
$venues = json_decode(json_decode($response, true));
?>
<?php if(!empty($venues['response']['venues']) && is_array($venues['response']['venues'])){ ?>
<?php foreach($venues['response']['venues'] as $venue){ ?>
<div class="venue">
<?php
if(isset($venue['categories']['0']['icon']['prefix']))
{
echo '<image class="icon" src="'.$venue['categories']['0']['icon']['prefix'].'88.png"/>';
}
else
echo '<image class="icon" src="https://foursquare.com/img/categories/building/default_88.png"/>';
echo '<a href="https://foursquare.com/v/'.$venue['id'].'" target="_blank"/><b>';
echo $venue['name'];
echo "</b></a><br/>";
if(isset($venue['categories']['0']))
{
if(property_exists($venue['categories']['0'],"name"))
{
echo ' <i> '.$venue['categories']['0']['name'].'</i><br/>';
}
}
if(property_exists($venue['hereNow'],"count"))
{
echo ''.$venue['hereNow']['count'] ." people currently here <br/> ";
}
echo '<b><i>History</i></b> :'.$venue['stats']['usersCount']." visitors , ".$venue['stats']['checkinsCount']." visits ";
?>
</div>
<?php } ?>
<?php } ?>

Categories