How to display a placeholder image if there's no image set up in WordPress? The picture is displayed by the $property_image var. It is a featured image from a custom post (not a WordPress original one).
It is defined by the code below, displays the featured image of the post if set up, if not, only shows the alt tag content ("Upcoming picture..."):
$property_image = dreamvilla_mp_get_device_image($property_ID);
Below my code from a WordPress theme:
$html .= '<div class="property-list-list property-listing-list-full">
<div class="col-xs-12 col-sm-4 col-md-4 property-list-list-image">
<a href=' . esc_url(get_permalink($property_ID)) . '>
<img '. $property_image . ' alt="Upcoming picture..." class="img-responsive">
</a>
' . $featured_proeprty_label . '
' . $featured_proeprty_label_icon . '
' . dreamvilla_mp_agent_favorites_property_icon($property_ID) . '
</div>
I'd place this:
if( empty( $property_image ) ) {
$property_image = 'src="your_fallback_image"';
}
directly after:
$property_image = dreamvilla_mp_get_device_image($property_ID);
What to be used to determine if the variable is empty depends on what is stored in the variable.
Directly before the img tag, you could insert this:
<?php
if($property_image == '') {
$property_image ="scr='http://placehold.it/300x200/f0a'";
}
?>
It checks whether that variable is empty and if yes, puts a links to a placeholder image into it, including the src attribute.
Related
I was trying to pull posts from one of my wordpress site to another using the WP REST API, I have successfully done that and the posts are displaying pretty well on the other site, but the issue now is that I want each post to be clickable, such that it opens the full article (post) when its been clicked on....
$json = file_get_contents('http://mywebsite.com/blog/wp-json/posts?filter[posts_per_page]=4');
// Convert the JSON to an array of posts
$posts = json_decode($json);
foreach ($posts as $p){
echo '<div style="color: #fff; float: left;" class="col-md-3 col-sm-3 col-lg-3">';
// Output the featured image (if there is one)
echo $p->featured_image ? '<img src="' . $p->featured_image->guid . '">' : '';
echo '<h5>Title: ' . $p->title . '</h5>';
// echo '<p>Date: ' . date('F jS', strtotime($p->date)) . '</p>';
$summary = $p->excerpt;
$pos=strpos($summary, ' ', 100);
$summary = substr($summary, 0, 100);
echo '<p>';
echo $summary;
echo '</p>';
echo '</div>';
}
So, what i need right now is to pull the link to each posts alongside....
I am just thinking of something like this: echo '<p>Link: ' . $p->link. '</p>';
You should have $p->link or $p->guid available.
You should be able to do print_r($p); within foreach and see exactly what data you have- would be useful if you'd copy that response here as well as that might help.
Try this it will work for you :
<?php
$link = get_permalink($p->ID); // This will get the link of the post from post ID.
echo '<p>Link: ' . $link. '</p>';
?>
What I want to do is have images display on each blogposts, the images that will be displayed will be what the post was tagged with, for example I will tag 1 post with 2 tags, design and print, I would like 2 small images to appear on that posts page.
This is the code that I have managed to scrape together, the else statement is displaying so a post will have the same amount of default-author.jpg showing as there are tags.
This makes me think that something in the foreach is not working but I'm still trying to learn and this is puzzling me, any help would be much appreciated.
<div class="image_wrap">
<?php
$posttags = get_the_tags(); // Get articles tags
$home = get_bloginfo('url'); // Get homepage URL
// Check if tag-slug.png exists and display it.
if ($posttags) {
foreach($posttags as $tag) {
$upload_dir = wp_upload_dir();
$image_relative_path = "/wp-content/uploads/2017/06/" . $tag->slug .".png";
$image_path = $upload_dir['basedir'] . $image_relative_path;
$image_url = $upload_dir['baseurl'] . $image_relative_path;
if (file_exists($image_path)) {
echo '<a href="' . $home . '/' . $tag->slug . '" /><img title="' . $tag->name . '" alt="' . $tag->name . '" src="' . $image_url . '" /></a>';
// If no image found, output something else.
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/default-author.jpg" title="image missing"/>
<?php }
}
}
?>
Does anyone have any ideas on how I can achieve the effect I want, images to be displayed on a blogpost [in wordpress] depending on what tag it has?
Below is the code for displaying WhatsApp sharing icon, condition used wp_is_mobile:
<?php
if (wp_is_mobile()) {
echo '<a href="whatsapp://send?text=' . the_title() . ', from ' . get_bloginfo('name') . ' ' . wp_get_shortlink() . '" data-action="share/whatsapp/share">';
echo '<i class="fa fa-whatsapp"></i></a>';
}
?>
The the_title is showing the Post title on the screen before WhatsApp icon, instead in the URL.
How do I make the post title is part of the URL and not showing in the text on screen
You need to use get_the_title(), but besides that I'd urlencode() the whole thing:
<?php
if (wp_is_mobile()) {
echo '<a href="whatsapp://send?text=' . urlencode(get_the_title() . ', from ' . get_bloginfo('name') . ' ' . wp_get_shortlink()) . '" data-action="share/whatsapp/share">';
echo '<i class="fa fa-whatsapp"></i></a>';
}
?>
As described here:
(the_title()) Displays or returns the title of the current post. This tag may only be used within The Loop, to get the title of a post outside of the loop use get_the_title. If the post is protected or private, this will be noted by the words "Protected: " or "Private: " prepended to the title.
we had a developer code something for a Custom Post Type in WordPress that we need to tweak just a touch. They're not available right now, but I think it's a pretty simple PHP issue if knowing the proper syntax. Unfortunately, I'm not completely PHP fluent, so hoping to get some help.
We have a posts landing page where we are just displaying a post thumbnail if there is one, and a link to the full post page (single) ONLY if an email address has been entered into the custom post type. If no email address was entered for the post, no link to the final post. Here's the main chunk of code that is currently working properly for this:
$team_query = new WP_Query($args);
while ($team_query->have_posts()) {
$team_query->the_post();
$member_email = get_post_meta(
get_the_ID(), '_base_team_email', true
);
$html .= '<div class="span-6 team-member">';
if (has_post_thumbnail()) {
$html .= '<div class="member-photo-wrap">'
. get_the_post_thumbnail(get_the_ID(), 'medium')
. '</div>';
}
if (!empty($member_email)) {
$html .= '<p class="member-email">'
. '<a class="linkIcon" href="' . get_permalink() . '#member-top">'
. 'Email & Bio »'
. '</a></p>';
}
$html .= '</div>';
}
All we need to do is tweak so that IF an email address has been entered, the same hyperlink is added around the thumbnail image so it can link to the final post along with the 'Email & Bio' text link. But if NO email address is found, the thumbnail image displays as-is (e.g, no link added).
THANKS!
This would add a link to the thumbnail as well, if the $member_email is not empty.
$team_query = new WP_Query($args);
while ($team_query->have_posts()) {
$team_query->the_post();
$member_email = get_post_meta(
get_the_ID(), '_base_team_email', true
);
// Create anchor tag to use when member email is not empty
$memberEmailAnchorStart = '';
$memberEmailAnchorEnd = '';
if (!empty($member_email)) {
$memberEmailAnchorStart =
'<a class="linkIcon" href="' . get_permalink() . '#member-top">';
$memberEmailAnchorEnd = '</a>';
}
$html .= '<div class="span-6 team-member">';
if (has_post_thumbnail()) {
$html .= '<div class="member-photo-wrap">'
. $memberEmailAnchorStart
. get_the_post_thumbnail(get_the_ID(), 'medium')
. $memberEmailAnchorEnd
. '</div>';
}
if (!empty($member_email)) {
$html .= '<p class="member-email">'
. $memberEmailAnchorStart
. 'Email & Bio »'
. $memberEmailAnchorEnd
. '</p>';
}
$html .= '</div>';
}
Depending on what the linkIcon class does, you might want to remove it from the thumbnail anchor tag.
I'm using Wordpress and the PODS plugin and created the pod project. In project i've created a set of fields. One of those is project_slide. If this field has a value, I want to show this in a RoyalSlider on my frontpage.
I've started with this piece of code (= one slide).
<?php
$project = pods('project', array('limit' => -1));
while ($project->fetch()) {
echo '<div class="rsContent">';
echo '<img class="rsImg" src="' . $project->display('project_slide') . '" alt="' . $project->display('project_title') . '">';
echo '</div>';
}
?>
The problem is: each project I add, displays in the slider. Even if project_slide has no value. Any idea how to resolve this?
Just add an if-statement to the loop.
Hide Image example
In my example, I only add the slide when the value isn't [empty string]
while ($project->fetch()) {
if( $project->display('project_slide') !== ""){
echo '<div class="rsContent">';
echo '<img class="rsImg" src="' . $project->display('project_slide') . '" alt="' .$project->display('project_title') . '">';
echo '</div>';
}
}
Expand with a detection if the image actually exists
You could expand it with a file_exists to also check if it exists (for the items with value, but the image itself doesnt exists). Just make sure the file_exists is 2nd, that way it only checks if the file is present if the string is not empty (if-statements go from left to right)
$project->display('project_slide') !== "" && file_exists($_SERVER['DOCUMENT_ROOT'].$project->display('project_slide')`
Default image method:
Instead of showing it only when there is an image, you could always show it, but build a default-image logic:
$defaultImage = '/some/image.jpg'; // set a default image outside of the loop
while ($project->fetch()) {
$src= $project->display('project_slide') !== "" ? $project->display('project_slide') : $defaultImage; // set a default umage
echo '<div class="rsContent">';
echo '<img class="rsImg" src="' . $src . '" alt="' .$project->display('project_title') . '">';
echo '</div>';
}