Show related posts' categories in WordPress - php

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

Related

Remove Link from author and date meta on Homepage of WordPress blog

On my blog homepage, Author and date meta (Author name and date) are in link form. I want to show them only in text. When I tried to remove the link the text is also removed. Kindly help me to remove the link. Codes of display.php are
if (!function_exists('swift_meta_generator')):
/**
* Generate post meta.
*
* Prints the post meta information based on the options set in theme options page.
* Called around post titles on home page and single pages.
*
* #param array $meta post meta order set in options page.
* #param string $classes html classes for the post meta wrapper.
*
*/
function swift_meta_generator($meta, $classes)
{
$data = '<div class="entry-meta ' . $classes . '">';
$size = count($meta);
for ($i = 0; $i < $size; $i++) {
switch ($meta[$i]) {
case 'text' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$meta[$i + 1] = preg_replace('(on)', '', $meta[$i + 1]);
$data .= $meta[$i + 1];
$i++;
break;
case 'author' :
$data .= '<span class="vcard author fa-user"><a class="" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author"><span class="fn">' . esc_attr(get_the_author()) . '</span></a></span> ';
break;
case 'author_avatar' :
$data .= get_avatar( get_the_author_meta('ID'), 16 );
$data .= ' <span class="vcard author"><a class="" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author"><span class="fn">' . esc_attr(get_the_author()) . '</span></a></span> ';
break;
case 'date' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$date = human_time_diff(get_the_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_date();
$data .= '<span class="date updated fa-clock-o"><a class="" href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_the_time()) . '" rel="bookmark">';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_date('c')) . '">' . esc_html($date) . '</time></a></span> ';
break;
case 'updated_on' :
if ((current_time('timestamp', 1) - get_the_modified_date('U')) < 86400)
$date = human_time_diff(get_post_modified_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_modified_date();
$data .= '<span class="date updated fa-clock-o"><a href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_post_modified_time()) . '" rel="bookmark">';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_modified_date('c')) . '">' . esc_html($date) . '</time></a></span> ';
Replace this code with your code
if (!function_exists('swift_meta_generator')):
/**
* Generate post meta.
*
* Prints the post meta information based on the options set in theme options page.
* Called around post titles on home page and single pages.
*
* #param array $meta post meta order set in options page.
* #param string $classes html classes for the post meta wrapper.
*
*/
function swift_meta_generator($meta, $classes)
{
$data = '<div class="entry-meta ' . $classes . '">';
$size = count($meta);
for ($i = 0; $i < $size; $i++) {
switch ($meta[$i]) {
case 'text' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$meta[$i + 1] = preg_replace('(on)', '', $meta[$i + 1]);
$data .= $meta[$i + 1];
$i++;
break;
case 'author' :
$data .= '<span class="vcard author fa-user"><span class="fn">' . esc_attr(get_the_author()) . '</span></span> ';
break;
case 'author_avatar' :
$data .= get_avatar( get_the_author_meta('ID'), 16 );
$data .= ' <span class="vcard author"><a class="" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author"><span class="fn">' . esc_attr(get_the_author()) . '</span></a></span> ';
break;
case 'date' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$date = human_time_diff(get_the_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_date();
$data .= '<span class="date updated fa-clock-o">' . esc_attr(get_the_time()) . '</span>';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_date('c')) . '">' . esc_html($date) . '</time></span> ';
break;
case 'updated_on' :
if ((current_time('timestamp', 1) - get_the_modified_date('U')) < 86400)
$date = human_time_diff(get_post_modified_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_modified_date();
$data .= '<span class="date updated fa-clock-o"><a href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_post_modified_time()) . '" rel="bookmark">';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_modified_date('c')) . '">' . esc_html($date) . '</time></a></span> ';
This code removed links from your author and date .... Hope that works fine to you.

How do I write a ternary operator for this line?

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>";

Loop through images not working

With this code, the ID is always 1. I want it to add +1 for each times it loops through the messages.
Like ex1, ex2 ex3 etc. etc.
I cant figure it out whats wrong. Could anyone help?
foreach ($message['attachment'] as $attachment)
{
if ($attachment['is_image'])
{
if ($attachment['thumbnail']['has_thumb'])
echo '<img src="', $attachment['thumbnail']['href'], '" alt="" id="thumb_', $attachment['id'], '" border="0" class="opplastetbilde"/><br />';
else
$id = 1;
if ($id < 10) {
echo '<span class="zoom" id="ex' . $id . '"><img src="' . $attachment['href'] . ';image" alt="" width="' . $attachment['width'] . '" height="' . $attachment['height'] . '" border="0" class="opplastetbilde"/></span><br />';
$id++;
}
}
echo '<img src="' . $settings['images_url'] . '/icons/clip.gif" align="middle" alt="*" border="0" /> ' . $attachment['name'] . ' (', $attachment['size'], ($attachment['is_image'] ? ', ' . $attachment['real_width'] . 'x' . $attachment['real_height'] . ' - ' . $txt['attach_viewed'] : ' - ' . $txt['attach_downloaded']) . ' ' . $attachment['downloads'] . ' ' . $txt['attach_times'] . '.)<br />';
}
move this before foreach or it is always reinitialize $id
$id = 1;
foreach ($message['attachment'] as $attachment)
{
Right now, you're setting $id = 1 each time you loop through the array
foreach ($message['attachment'] as $attachment) {
// ...
$id = 1;
// ...
}
In order to increment it properly, you need to place it before the loop
$id = 1;
foreach ($message['attachment'] as $attachment) {
// ...
}

Can someone tell me what's wrong with my PHP code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using latest version of MAMP. PHP 500 error saying unexpected "." on line 7
function get_thumbnail_view_html($thumb) {
$thumb_layout = "";
$thumb_layout = $thumb_layout . '<li class="span3">';
$thumb_layout = $thumb_layout . '<a class="thumbnail" href="' . $thumb["link"] . '">';
$thumb_layout = $thumb_layout . '<img src="' . $thumb["img"] . '" alt="' . $thumb["name"] . '">';
$thumb_layout = $thumb_layout . '<p>' . $thumb["name"] '</p>';
$thumb_layout = $thumb_layout . '<span>' . $thumb["filter"] '</span>';
$thumb_layout = $thumb_layout . "</a>";
$thumb_layout = $thumb_layout . "</li>";
return $thumb_layout;
}
Looks like you're missing a . to concat the string on a few lines:
$thumb_layout = $thumb_layout . '<p>' . $thumb["name"] '</p>';
$thumb_layout = $thumb_layout . '<span>' . $thumb["filter"] '</span>';
should be
$thumb_layout = $thumb_layout . '<p>' . $thumb["name"] . '</p>';
$thumb_layout = $thumb_layout . '<span>' . $thumb["filter"] . '</span>';
You have to add a dot(.) before and at line 6 and 7
the code would be as follow:
function get_thumbnail_view_html($thumb) {
$thumb_layout = "";
$thumb_layout = $thumb_layout . '<li class="span3">';
$thumb_layout = $thumb_layout . '<a class="thumbnail" href="' . $thumb["link"] . '">';
$thumb_layout = $thumb_layout . '<img src="' . $thumb["img"] . '" alt="' . $thumb["name"] . '">';
$thumb_layout = $thumb_layout . '<p>' . $thumb["name"] .'</p>';
$thumb_layout = $thumb_layout . '<span>' . $thumb["filter"]. '</span>';
$thumb_layout = $thumb_layout . "</a>";
$thumb_layout = $thumb_layout . "</li>";
return $thumb_layout;

PHP operator confusion (a beginner's question)

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;

Categories