I can't understand why the thumbnail isn't in the code. It's the get_the_post_thumbnail('thumbnail') that isn't working. I'm confused an have spent to many hours to try to solve this by my self, but I don't see the error. Do you?
function show_posts() {
$html = "";
$extra_css = "";
$latestPosts = new WP_Query('cat=5&posts_per_page=1');
if($latestPosts->have_posts()): while($latestPosts->have_posts()):
$latestPosts->the_post();
if (has_post_thumbnail) $extra_css = "has-thumbnail";
$html = "<article class='post {$extra_css}'>";
$html .= "<div class='post-thumbnail'>";
$html .= "<a href='" . get_the_permalink() . "'>" . get_the_post_thumbnail('thumbnail') . "</a>";
$html .= "<div class='post-thumbnail-date'></h4>" . get_the_time('Y-m-d') . "<h4></div>";
$html .= "</div><!-- end post-thumbnail -->";
$html .= "<h3><a href='" . get_the_permalink() . "'>" . get_the_title() . "</a></h3>";
$html .= "<p class='post-info'>" . get_the_time('Y-m-d') . " | Av " . get_the_author() . "</p>";
$html .= "<p class='post-text'>" . get_the_excerpt() . "<a href='" . get_the_permalink() . "'> Läs mer...</a>";
$html .= "</p>";
$html .= "</article>";
endwhile;
else:
// Error message
endif;
wp_reset_postdata();
return $html;
}
The first parameter of get_the_post_thumbnail function is optional post ID and second parameter is thumbnail size...
Try use this: get_the_post_thumbnail(null, 'thumbnail') or instead of null custom/global $post->ID...
Related
I have a function that group the products and I want to sort in ascending order by the price. I even tried to use "sort() function but display any output. I have several options with Array_multisort(), asort().
function display_model($postid) {
$price = get_field ('starting_price',$postid);
print_r(asort($price));
$sqft = get_field ('square_footage',$postid);
$garages = get_field ('garages',$postid);
$bathrooms = get_field ('bathrooms',$postid);
$bedrooms = get_field ('bedrooms',$postid);
$retContentModel = "<div class='img-hover-zoom'>";
//$retContentModel = "<div class='col-xs-3'>";
$retContentModel .= "<a href='" . get_permalink($postid) . "'>";
$retContentModel .= "<img class='model-image-slider' src='" . get_the_post_thumbnail_url($postid) . "' alt='" . get_the_title($postid) . "' />";
$retContentModel .= "</a>";
$retContentModel .= "</div>";
$retContentModel .= "<h4 class='model_name'><a href='" . get_permalink($postid) . "'>" . get_the_title($postid) . "</a></h4>";
$retContentModel .= "<div class='row home-stats'>";
$retContentModel .= "<div class='col-sm-7 price'><p>Starting at $" . number_format($price) . "</p></div>";
$retContentModel .= "<div class='col-sm-5 sqft'><p>" . $sqft . " Sq. Ft.</p></div>";
$retContentModel .= "</div>";
$retContentModel .= "<div class='row home-icons'>";
$retContentModel .= "<div class='col-sm-4'>";
$retContentModel .= "<img src='/wp-content/themes/activa/img/icon-garage.png' />";
$retContentModel .= "<p>" . $garages . "</p>";
$retContentModel .= "</div>";
$retContentModel .= "<div class='col-sm-4'>";
$retContentModel .= "<img src='/wp-content/themes/activa/img/icon-bathrooms.png' />";
$retContentModel .= "<p>" . $bathrooms->name . "</p>";
$retContentModel .= "</div>";
$retContentModel .= "<div class='col-sm-4'>";
$retContentModel .= "<img src='/wp-content/themes/activa/img/icon-bedrooms.png' />";
$retContentModel .= "<p>" . $bedrooms->name . "</p>";
$retContentModel .= "</div>";
$retContentModel .= "</div>";
return $retContentModel;
}
The function that calls the DISPLAY_MODEL function
function add_homes_for_home_type( $postid, $hometype, $counter) {
$retContent = "";
$myCount = 0;
$carousel_counter = 4; // number of items to show on screen at once
$posts1 = get_field('homes',$postid);
// $modListPrice = get_field('starting_price',$postid);
// print $modListPrice;
//print $postid;
if( have_rows('homes', $postid) ) {
$retContent .= "<div id='" . $hometype . "' class='tab-pane fade";
if ($counter < 1) { $retContent .= " active in"; }
$retContent .= "' aria-labelledby='" . $hometype . "-tab' role='tabpanel'>";
$retContent .= add_site_plan_button_for_community_home_type (get_field('home_type_list'), $hometype);
$retContent .= add_price_list_button_for_community_home_type (get_field('home_type_list'), $hometype);
$retContent .= add_amenities_button_for_community_home_type (get_field('home_type_list'), $hometype);
$retContent .= "<div id='swiper-container-" . $hometype . "' class='swiper-container swiper-container-" . $hometype . "'>";
$retContent .= "<ul class=\"swiper-wrapper\">\n";
//$active = 'active';
foreach( $posts1 as $p ) {
$thisType = get_field('home_type',$p->ID);
if ($thisType->slug == $hometype) {
//$active = 'active';
$homecounter = 1;
$firstrun = 1;
$retContent .= "<li class='swiper-slide'>";
$retContent .= display_model($p->ID);
$retContent .= "</li>";
}
$myList[] = $p->ID;
}
// End Slider
$retContent .= "</ul>";
$retContent .= "<div class='swiper-pagination swiper-pagination-" . $hometype . "' ></div>";
$retContent .= "<div class='swiper-button-next swiper-button-next-" . $hometype . "'></div>";
$retContent .= "<div class='swiper-button-prev swiper-button-prev-" . $hometype . "'></div>";
$retContent .= "</div>\n";
$hometypeClean = str_replace("-","",$hometype);
$retContent .= "<script>\n";
$retContent .= "var modelsSwiper" . $hometypeClean . ";\n";
$retContent .= "var self = this;\n";
$retContent .= "jQuery(document).ready(function ($) {\n";
$retContent .= "modelsSwiper" . $hometypeClean . " = new Swiper('.swiper-container-" . $hometype . "', {\n";
$retContent .= "containerModifierClass: 'swiper-container-" . $hometype . "-',\n";
//$retContent .= "slideClass: 'swiper-slide-" . $hometype . "',\n";
//$retContent .= "slideActiveClass: 'swiper-slide-active-" . $hometype . "',\n";
//$retContent .= "slideDuplicateActiveClass: 'swiper-slide-duplicate-active-" . $hometype . "',\n";
//$retContent .= "slideVisibleClass: 'swiper-slide-visible" . $hometype . "',\n";
//$retContent .= "wrapperClass: 'swiper-wrapper-" . $hometype . "',\n";
$retContent .= "slidesPerView: 4,\n";
$retContent .= "slidesPerGroup: 4,\n";
$retContent .= "spaceBetween: 40,\n";
$retContent .= "breakpoints: {\n";
$retContent .= "520: {\n";
$retContent .= "slidesPerView: 1,\n";
$retContent .= "slidesPerGroup: 1,\n";
$retContent .= "spaceBetween: 40},\n";
$retContent .= "960: {\n";
$retContent .= "slidesPerView: 2,\n";
$retContent .= "slidesPerGroup: 2,\n";
$retContent .= "spaceBetween: 40},\n";
$retContent .= "1200: {\n";
$retContent .= "slidesPerView: 3,\n";
$retContent .= "slidesPerGroup: 3,\n";
$retContent .= "spaceBetween: 40}\n";
$retContent .= "},\n";
$retContent .= "watchOverflow: true,\n";
$retContent .= "observer: true,\n";
$retContent .= "observeParents: true,\n";
$retContent .= "pagination: {\n";
$retContent .= "el: '.swiper-pagination-" . $hometype . "',\n";
$retContent .= "clickable: true\n";
$retContent .= "},\n";
$retContent .= "navigation: {\n";
$retContent .= "nextEl: '.swiper-button-next-" . $hometype . "',\n";
$retContent .= "prevEl: '.swiper-button-prev-" . $hometype . "',\n";
$retContent .= "},";
$retContent .= "});\n";
$retContent .= "});\n";
$retContent .= "jQuery('a[data-toggle=\"tab\"]').on('shown.bs.tab', function(e) {\n";
$retContent .= "setTimeout(function() {\n";
$retContent .= "modelsSwiper" . $hometypeClean . ".update();\n";
//$retContent .= "alert('loaded".$hometypeClean."');";
$retContent .= "}, 400);\n";
$retContent .= "});\n";
$retContent .= "</script>\n";
// AGENTS CODE
$retContent .= add_homes_agents($postid, $hometype);
// End Tab
$retContent .= "</div>\n";
$myCount++;
}
$myList[] = $p->ID;
//print $retContent;
return array($retContent,$myCount);
}
From the page template, I called the function add_homes_for_home_types()
The code below:
<div class="tab-content col-sm-12" id="myTabContent">
<?php
$tabCounter = 0;
foreach ($homesResp[1] as &$homes) {
$homesList = add_homes_for_home_type(get_the_id(),$homes,$tabCounter);
print $homesList[0];
$tabCounter++;
//print $tabCounter;
}
?>
</div>
I will appreciate if anyone can help out.
I have tried to handle the sorting at each level but not working. That is why I tried to look elsewhere.
Thank you
You can sort the output using JQuery, it is really simple and straightforward. The code below will only work if you make some adjustments in your function. Don't worry, I will guide you to achieving that.
jQuery(document).ready(function($) {
$(window).load(function() {
$('#condo #swiper-container-condo .swiper-wrapper li.swiper-slide').sort(function(a, b) {
var $field = $('#condo div#swiper-container-condo .swiper-wrapper li.swiper-slide .home-stats .price p span');
var aValue = parseFloat($(a).find($field).text());
var bValue = parseFloat($(b).find($field).text());
if (aValue > bValue)
return 1;
if (aValue < bValue)
return -1;
return 0;
}).appendTo('#swiper-container-condo .swiper-wrapper');
});
});
Back to your function, look for this line and replace with this; (you have the line in the first function pasted above)
$retContentModel .= "<div class='col-sm-7 price'><p>Starting at $ <span style='font-size:16px;'>" . number_format($price) . "</span></p></div>";
I am trying to also display the category of a related post, below one article.
I am using Newsmag theme.
The code that builds my related posts is this:
class td_module {
var $post;
var $title_attribute;
var $title;
var $href;
var $td_review; //review meta
var $category;
//constructor
function __construct($post) {
//this filter is used by td_unique_posts.php - to add unique posts to the array for the datasource
apply_filters("td_wp_boost_new_module", $post);
$this->post = $post;
$this->title = get_the_title($post->ID);
$this->title_attribute = esc_attr(strip_tags($this->title));
$this->href = esc_url(get_permalink($post->ID));
$this->category = '';
if (has_post_thumbnail($this->post->ID)) {
$this->post_has_thumb = true;
} else {
$this->post_has_thumb = false;
}
//get the review metadata
$this->td_review = get_post_meta($this->post->ID, 'td_review', true);
}
and the part that displays the related articles is this:
$buffy .= '<div class="td-module-thumb">';
if (current_user_can('edit_posts')) {
$buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
}
$buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
$buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
$buffy .= '<span class="td-module-thumb-category">'.$this->category.'</span>';
....................................
$this->category was added by me. I am trying to get the data from wp_terms table and display the categories of each of the related posts.
I am new to WordPress(actually, this is the first time I touch WordPress code).
Thank you
This should work with your existing code:
$buffy .= '<div class="td-module-thumb">';
$related_category = get_the_category($this->post->ID);
if (current_user_can('edit_posts')) {
$buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
}
$buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
$buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
$buffy .= '<span class="td-module-thumb-category">'.$related_category[0]->cat_name.'</span>';
Or if you need the category to be a link use:
$buffy .= '<div class="td-module-thumb">';
$related_category = get_the_category($this->post->ID);
if (current_user_can('edit_posts')) {
$buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
}
$buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
$buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
$buffy .= '<span class="td-module-thumb-category">'.$related_category[0]->cat_name.'</span>';
You will probably want to move $related_category = get_the_category($this->post->ID); to where you have $this->category = ''; in your first pasted code segment
I can't figure out how to write a ternary operator for the ending of this line (i.e. "location" and "description"). I've defined the $location and $description variables but I'm getting an error that says unexpected $location variable.
Basically, for both location and description, I'm trying to say: If the field has been filled out, display the field. If not, don't print anything.
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
Here is the full context:
if( have_rows('slides', $frontpage_id) ):
while ( have_rows('slides', $frontpage_id) ) :
the_row();
$group = get_sub_field('group');
$count = 1;
$i = 0;
$nav .= '<ul id="nav_'.$group.'" style="display: none;">';
$content .= '<div class="image-data-container image-container-'. $group .'">';
while( have_rows('images') ) : the_row(); $count++;
$image_url = get_sub_field('image');
$location = get_sub_field('location');
$description = get_sub_field('description');
$isize = #getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
$nav .= '<li >' . get_sub_field('title') . '</li>';
$i++;
endwhile;
$content .= '</div>';
$nav .= '</ul>';
endwhile;
endif;
?>
Other the fact that your $content variable is a formatting nightmare. The ternary operator syntax returns result based on the condition and it should be separated from your $content attribute
$someVariable = (get_sub_field('location')) ? '<em>'.$location.'</em>' : '';
Now use that some variable inside your $content variable instead
Here is your code with comments:
while( have_rows('images') ) : the_row(); $count++;
$image_url = get_sub_field('image');
// you have already defined the location variable here
// It also means and I am assuming they your get_sub_field is returning a string
// If you are receiving a boolean or null value than and than only than you can do your ternary condition call of
// get_sub_field('location') ? '<em>'$location'</em>' : ''
// and if you are receiving a string field then you will have to explicitly check the condition
// something like this (get_sub_field('location') === '') ? '<em>'$location'</em>' : ''
// I am not that familiar with wordpress framework or the ACF plugin but it looks like get_sub_field will return something so you actually wont need the ternary condition
$location = get_sub_field('location');
// Similar case with description
$description = get_sub_field('description');
$isize = #getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
// After removing the ternary condition your content variable will look like this
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . '<em>' . $location . '</em>' . '<hr><span class="details">Details [+]</span><div class="details-display">' . $description . '</div>' . '</div></div></div>';
$nav .= '<li >' . get_sub_field('title') . '</li>';
$i++;
endwhile;
You know you can always make it more readable
// You know instead of all the concatenation you can do this instead
$templateDirURI = get_template_directory_uri();
$groupLower = strtolower($group);
$title = get_sub_field('title');
$content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong><hr><em>{$location}</em><hr><span class='details'>Details [+]</span><div class='details-display'>{$description}</div></div></div></div>";
Updated Code
$content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong>";
// Assuming the user did not submit location information
if($location !== "")
$content .= "<hr><em>{$location}</em><hr>";
if($description !== "")
$content .= "<span class='details'>Details [+]</span><div class='details-display'>{$description}</div>";
$content .= "</div></div></div>";
I don't know PHP all that well so I hope I will be showing enough code when I ask this question. I have a part of my homepage that is going to show the latest 5 blog posts and so I set it up like this:
<?php
function get_latest_post_html() {
$content = "";
query_posts('showposts=5');
while (have_posts()){
the_post();
$content .= "<p class='title'><a href='" . get_permalink() . "'>" . get_the_title() . "</a></p>\n" .
"<p class='excerpt'><a href='" . get_permalink() . "'><img src='" . wp_get_attachment_url( get_post_thumbnail_id($post->ID) ) . "' class='rt-image img-left wp-post-image' style='max-width:175px;'/></a>" . get_the_excerpt() . "</p><br/><hr/>";
}
wp_reset_query();
return "<div class='latest-post'>\n$content\n</div>";
}
add_shortcode('get_latest_post', 'get_latest_post_html');
?>
It calls the last 5 posts just fine, but I don't want to have it display the <hr/> on the bottom of the 5th post.
Setup some logic in your while loop to conditionally display the <hr >.
For example:
$i = 0;
while (have_posts()) {
++$i;
the_post();
// ...
if ($i < 5) {
$content .= '<hr />';
}
}
Note: WordPress may not return 5 posts, so you should consider that path. I would also discourage string concatenation in tight loops. Refactor your code and use echo.
Since you only need to get rid of the last <hr/>.
Try use substr()
So in your case, add this after while loop ends
$content = substr($content, 0, -5)
<?php
function get_latest_post_html() {
$content = "";
query_posts('showposts=5');
$i = 0;
while (have_posts()){
i++;
if(i < 5){
the_post();
$content .= "<p class='title'><a href='" . get_permalink() . "'>" . get_the_title() . "</a></p>\n" .
"<p class='excerpt'><a href='" . get_permalink() . "'><img src='" . wp_get_attachment_url( get_post_thumbnail_id($post->ID) ) . "' class='rt-image img-left wp-post-image' style='max-width:175px;'/></a>" . get_the_excerpt() . "</p><br/><hr/>";
}
else{
$i = 0;
//do something
}
}
wp_reset_query();
return "<div class='latest-post'>\n$content\n</div>";
}
add_shortcode('get_latest_post', 'get_latest_post_html');
?>
I'm trying to figure out someone else's code and have come across this piece of code:
$html = '<div class="event">' . "\n";
if (get ( 'Event_Image' ))
{
$html .= '<a href="' . get ( 'Event_Image' ) . '">'
. '<img src="' . pt () . '?src=' . get ( 'Event_Image' ) . '&w=100" alt="' . get_the_title () . '" />'
. '</a><br />' . "\n";
}
$html .= '<a href="' . get_permalink ( $eventId ) . '">' . // title="Permanent Link to ' . get_the_title_attribute() . '"
get_the_title () . '</a><br />' . "\n";
if (get ( 'Event_Time' ))
{
$html .= get ( 'Event_Time' ) . '<br />' . "\n";
}
if (get ( 'Store_Location' ))
{
$html .= get ( 'Store_Location' );
}
$html .= '</div><!-- event -->' . "\n";
$eventsArr [$dateArr] [$eventId] = $html;
}
My question: What does the .= mean? Does it add to the variable (in this case $html)?
Yes. See http://www.php.net/manual/en/language.operators.string.php.
It means concatenate/append the value on the right hand to the value stored in the variable:
$a = 'str';
$a .= 'ing';
echo $a; // string
Yes, you got it right, here is an example:
$str = 'Hello ';
$str .= 'World';
echo $str;
Result:
Hello World
It means concatinate equals. So
$var = 'foo';
$var .= 'bar';
echo $var;
// output is 'foobar'
It is concatenate, then assign.
Same as:
$html = $html . $someString;