function widget($args, $instance) { global $post; // Preserve global $post $preserve = $post; extract($args); // only useful on post pages if (!is_single()) { return; } $title = apply_filters('widget_title', empty($instance['title']) ? __('Read Next', 'largo') : $instance['title'], $instance, $this->id_base); echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } $related = new Largo_Related($instance['qty']); //get the related posts $rel_posts = new WP_Query(array('post__in' => $related->ids(), 'nopaging' => 1, 'posts_per_page' => $instance['qty'], 'ignore_sticky_posts' => 1)); if ($rel_posts->have_posts()) { echo '<ul class="related">'; while ($rel_posts->have_posts()) { $rel_posts->the_post(); echo '<li>'; echo '<a href="' . get_permalink() . '"/>' . get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class' => 'alignleft')) . '</a>'; ?> <h4><a href="<?php the_permalink(); ?> " title="Read: <?php esc_attr(the_title('', '', FALSE)); ?> "><?php the_title(); ?> </a></h4> <h5 class="byline"> <span class="by-author"><?php largo_byline(true, true); ?> </span> <time class="entry-date updated dtstamp pubdate" datetime="<?php echo esc_attr(get_the_date('c')); ?> "><?php largo_time(); ?> </time> </h5> <?php // post excerpt/summary largo_excerpt(get_the_ID(), 2, false, '', true); echo '</li>'; } echo "</ul>"; } echo $after_widget; // Restore global $post wp_reset_postdata(); $post = $preserve; }
function test_largo_time() { $id = $this->factory->post->create(); // Test with $echo = false $result = largo_time(false, $id); $this->assertTrue(!empty($result)); // Test with $echo = true $this->expectOutputRegex('/<span class\\="time\\-ago">.*ago<\\/span>/'); largo_time(true, $id); // Make sure `largo_time` can determine the post id properly global $post; $save = $post; $post = get_post($id); setup_postdata($post); $another_result = largo_time(false); $this->assertEquals($result, $another_result); wp_reset_postdata(); $post = $save; }
function largo_byline($echo = true, $exclude_date = false, $post = null) { if (!empty($post)) { if (is_object($post)) { $post_id = $post->ID; } else { if (is_numeric($post)) { $post_id = $post; } } } else { $post_id = get_the_ID(); } $values = get_post_custom($post_id); if (function_exists('get_coauthors') && !isset($values['largo_byline_text'])) { $coauthors = get_coauthors($post_id); foreach ($coauthors as $author) { $byline_text = $author->display_name; if ($org = $author->organization) { $byline_text .= ' (' . $org . ')'; } $out[] = '<a class="url fn n" href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__('Read All Posts By %s', 'largo'), $author->display_name)) . '" rel="author">' . esc_html($byline_text) . '</a>'; } if (count($out) > 1) { end($out); $key = key($out); reset($out); $authors = implode(', ', array_slice($out, 0, -1)); $authors .= ' <span class="and">' . __('and', 'largo') . '</span> ' . $out[$key]; } else { $authors = $out[0]; } } else { $authors = largo_author_link(false, $post_id); } $output = '<span class="by-author"><span class="by">' . __('By', 'largo') . '</span> <span class="author vcard" itemprop="author">' . $authors . '</span></span>'; if (!$exclude_date) { $output .= '<span class="sep"> | </span><time class="entry-date updated dtstamp pubdate" datetime="' . esc_attr(get_the_date('c', $post_id)) . '">' . largo_time(false, $post_id) . '</time>'; } if (current_user_can('edit_post', $post_id)) { $output .= '<span class="sep"> | </span><span class="edit-link"><a href="' . get_edit_post_link($post_id) . '">' . __('Edit This Post', 'largo') . '</a></span>'; } if (is_single() && of_get_option('clean_read') === 'byline') { $output .= '<a href="#" class="clean-read">' . __('View as "Clean Read"', 'largo') . '</a>'; } if ($echo) { echo $output; } else { return $output; } }
?> " title="Read: <?php esc_attr(the_title('', '', FALSE)); ?> "><?php the_title(); ?> </a></h4> <?php // post thumbnail, by default after headline if (get_the_post_thumbnail() && isset($instance) && isset($instance['thumbnail_location']) && $instance['thumbnail_location'] == 'after') { echo '<a href="' . get_permalink() . '"/>' . get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class' => 'alignleft')) . '</a>'; } // post byline, if indicated if (isset($instance) && isset($instance['show_byline']) && $instance['show_byline']) { ?> <h5 class="byline"><time class="entry-date updated dtstamp pubdate" datetime="<?php echo esc_attr(get_the_date('c')); ?> "><?php largo_time(); ?> </time></h5> <?php } // post excerpt/summary if ($post->post_excerpt) { echo '<p>' . $post->post_excerpt . '</p>'; } else { echo '<p>' . largo_trim_sentences($post->post_content, 2) . '</p>'; }
function largo_byline($echo = true, $exclude_date = false, $post = null) { if (!empty($post)) { if (is_object($post)) { $post_id = $post->ID; } else { if (is_numeric($post)) { $post_id = $post; } } } else { $post_id = get_the_ID(); } $values = get_post_custom($post_id); if (function_exists('get_coauthors') && !isset($values['largo_byline_text'])) { $coauthors = get_coauthors($post_id); foreach ($coauthors as $author) { $byline_text = $author->display_name; if ($org = $author->organization) { $byline_text .= ' (' . $org . ')'; } $out[] = '<a class="url fn n" href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__('Read All Posts By %s', 'largo'), $author->display_name)) . '" rel="author">' . esc_html($byline_text) . '</a>'; } if (count($out) > 1) { end($out); $key = key($out); reset($out); $authors = implode(', ', array_slice($out, 0, -1)); $authors .= ' <span class="and">' . __('and', 'largo') . '</span> ' . $out[$key]; } else { $authors = $out[0]; } } else { $authors = largo_author_link(false, $post_id); } $output = '<span class="by-author"><span class="by">' . __('By', 'largo') . '</span> <span class="author vcard" itemprop="author">' . $authors . '</span></span>'; if (!$exclude_date) { $output .= '<span class="sep"> | </span><time class="entry-date updated dtstamp pubdate" datetime="' . esc_attr(get_the_date('c', $post_id)) . '">' . largo_time(false, $post_id) . '</time>'; } /** * Filter the largo_byline output text to allow adding items at the beginning or the end of the text. * * @since 0.5.4 * @param string $partial The HTML of the output of largo_byline(), before the edit link is added. * @link https://github.com/INN/Largo/issues/1070 */ $output = apply_filters('largo_byline', $output); if (current_user_can('edit_post', $post_id)) { $output .= '<span class="sep"> | </span><span class="edit-link"><a href="' . get_edit_post_link($post_id) . '">' . __('Edit This Post', 'largo') . '</a></span>'; } if ($echo) { echo $output; } else { return $output; } }