Styling Related Posts Horizontally - php

I would like to display related posts horizontally.
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID,array(370, 300, true));
$output .= '</div>';
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}
I have tried the following css, but doesn't works.
.rp-users {display:inline-block;}
.rp-title {font-size:19px; width: 370px;text-align:left;display:block;}

function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
$output .= '<div class="rp-user-entry">';
$output .= get_the_post_thumbnail($authors_post->ID,array(370, 300, true));
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
$output .= '</div>';
}
$output .= '</div>';
return $output;
And the css:
.rp-users {display:block;}
.rp-user-entry { display: inline-block; vertical-align: top; width: 33.333%; }
.rp-title {font-size:19px; width: 370px;text-align:left;display:block;}

I think the code should look like this
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID,array(370, 300, true));
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output;
}

Related

WooCommerce: Display some 5 stars rating products reviews randomly

Based on WooCommerce: Display some reviews randomly on home page answer code, I am using the following code that shows 5 random reviews:
function get_woo_reviews()
{
$comments = get_comments(
array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
)
);
shuffle($comments);
$comments = array_slice( $comments, 0, 5 );
$html = '<ul>';
foreach( $comments as $comment ) :
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
$html .= '<div>' .$comment->comment_content.'</div>';
$html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
$html .= '</li>';
endforeach;
$html .= '</ul>';
ob_start();
echo $html;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
add_shortcode('woo_reviews', 'get_woo_reviews');
But It also shows some comments without any rating.
How to change this code to show only 5 star rating reviews?
A "meta query" is required to the WP_Comment_Query based on "rating" meta key set to 5 stars meta value, to get random reviews that have 5 stars rating, as follows:
function get_random_five_stars_products_reviews( $atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'limit' => 5, // number of reviews to be displayed by default
), $atts, 'woo_reviews' ) );
$comments = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => '5',
) ),
) );
shuffle($comments);
$comments = array_slice( $comments, 0, $limit );
$html = '<ul class="products-reviews">';
foreach( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
$html .= '<div>' .$comment->comment_content.'</div>';
$html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
$html .= '</li>';
}
return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');
Code goes in functions.php file of the active child theme (or active theme). Tested and works
USAGE: [woo_reviews] or [woo_reviews limit="3"] for 3 random reviews for example.

How to add related products to order emails in WooCommerce

I have a question following up on this topic: How to add related products or cross sells to order emails in WooCommerce
The code use to to work until the latest WC / WP update. Do you know why it isn't working anymore? What needs to be changed in the code so it's working again? Thanks!
This is the code:
// product suggestion in order mail
function order_mail_product_suggestion($atts) {
$atts=shortcode_atts(
array(
'id' => '',
), $atts, 'order_mail_product_suggestion');
$orderId = esc_attr($atts['id']);
$order = wc_get_order( (int)$orderId );
$items = $order->get_items();
$cat = array();
$pid = array();
foreach ( $items as $item ) {
$pid[] = $item->get_product_id();
$terms = wp_get_post_terms($item->get_product_id(),'product_cat',array('fields'=>'ids'));
foreach ( $terms as $term ) {
$cat[] = $term;
}
}
$uniuqcat = array_unique($cat);
$uniuqpid = array_unique($pid);
$html = '';
$args = array(
'post_type' => 'product',
'stock' => 1,
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '20',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => implode(',', $uniuqcat),
'operator' => 'IN'
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN'
)
)
);
$loop = new WC_Product_Query($args);
$products = $loop->get_products();
if ($products) {
$html .= '<div id="suggestion" style="padding: 20px 0;border-top: 1px solid #eee;">';
$html .= '<h2 style="text-align: center;font-weight: 400;color: #000;">PLUS, MORE THINGS YOU MAY LIKE</h2>';
$html .= '<table style="width: 100%;table-layout: fixed;border-collapse: collapse;">';
$html .= '<tbody>';
$html .= '<tr>';
$i=0;
foreach ( $products as $product ) {
if (in_array($product->get_id(), $pid)) {
continue;
}
$html .= '<td align="center" style="padding: 5px;border: 1px solid #eee;">';
if (has_post_thumbnail( $product->get_id() )) {
$html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog');
} else {
$html .= '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';
}
$html .= '<h3><a style="color: #000;font-weight: normal;text-decoration: none;font-size: 13px;text-align: center;display: block;line-height: 16px;" href="'.get_permalink( $product->get_id() ).'">'.esc_attr($product->get_title() ? $product->get_title() : $product->get_id()).'</a></h3>';
$html .= '<p><a style="font-weight: normal;text-decoration: none;display: block;margin: 0 auto;max-width: 72px;padding: 4px 9px;text-align: center;background-color: #000;color: #fff;font-size: 13px;" href="'.get_permalink( $product->get_id() ).'" class="shop-now">Shop Now</a></p>';
$i++;
if($i==4) break;
}
$html .= '</tr>';
$html .= '</tbody>';
$html .= '</table>';
$html .= '</div>';
}
// now return the preapred html
return $html;
}
// register shortcode add_shortcode('email_product_suggestion', 'order_mail_product_suggestion');
However, now it shows that there is a fatal error:
Uncaught Error: Call to a member function get_items() on boolean in:
$items = $order->get_items();
What is wrong with the function, has it changed? How can this be fixed? Thank you!
The email template is "customer-completed-order.php". The shortcode used in the email php template is:
// Product suggestions
$order = $email->object;
if ( $order ) {
$id = $order->get_id();
echo do_shortcode( '[email_product_suggestion id="'.$id.'" ]' );
}

Search with um_activity (Ultimate member activity) post with post_meta and show all matching post_meta results

I am using Kleo-child theme. I have an active plugin Ultimate member social activity. I want to search for any keyword from um_shared_link post_meta and see the result that keyword activity what I search for. What exactly should I do for that? Here is my code:
<?php
add_action( 'init', 'update_my_custom_type', 99 );
function update_my_custom_type() {
global $wp_post_types;
if ( post_type_exists( 'um_activity' ) ) {
// exclude from search results
$wp_post_types['um_activity']->exclude_from_search = false;
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
wp_enqueue_style( 'parent-style',
get_template_directory_uri().'/style.css' );
}
add_action( 'wp_ajax_kleo_ajax_search', 'kleo_ajax_search' );
add_action( 'wp_ajax_nopriv_kleo_ajax_search', 'kleo_ajax_search' );
if ( ! function_exists( 'kleo_ajax_search' ) ) {
function kleo_ajax_search() {
//if "s" input is missing exit
if ( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) )
{
die();
}
if ( ! empty( $_REQUEST['bbp_search'] ) ) {
$search_string = $_REQUEST['bbp_search'];
} else {
$search_string = $_REQUEST['s'];
}
$output = '';
$context = 'any';
$defaults = array(
'numberposts' => 4,
'posts_per_page' => 20,
'post_type' => 'any',
'post_status' => array('publish','inherit'),
'post_password' => '',
'suppress_filters' => false,
'meta_query' => array(
array(
'key' => '_shared_link',
'value' => $_REQUEST['s'],
'compare' => 'LIKE'
)
)
);
if ( empty( $defaults['post_type'] ) ) {
$posts = null;
} else {
$defaults = apply_filters( 'kleo_ajax_query_args', $defaults );
$the_query = new WP_Query( $defaults );
$posts = $the_query->get_posts();
// echo "the query is".$the_query->request;
//die();
/*$the_query_meta = new WP_Query($defaults_meta);
$posts_meta = $the_query->get_posts();
$posts = array_merge($posts,$posts_meta);*/
}
$members = array();
$members['total'] = 0;
$groups = array();
$groups['total'] = 0;
$forums = false;
if ( function_exists( 'bp_is_active' ) && ( $context == "any" || in_array( "members", $context ) ) ) {
$members = bp_core_get_users( array(
'search_terms' => $search_string,
'per_page' => $defaults['numberposts'],
'populate_extras' => false,
) );
}
if ( function_exists( 'bp_is_active' ) && bp_is_active( "groups" ) && ( $context == "any" || in_array( "groups", $context ) ) ) {
$groups = groups_get_groups( array(
'search_terms' => $search_string,
'per_page' => $defaults['numberposts'],
'populate_extras' => false,
) );
}
if ( class_exists( 'bbPress' ) && ( $context == "any" || in_array( "forum", $context ) ) ) {
$forums = kleo_bbp_get_replies( $search_string );
}
//if there are no posts, groups nor members
if ( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 0 && ! $forums ) {
$output = "<div class='kleo_ajax_entry ajax_not_found'>";
$output .= "<div class='ajax_search_content'>";
$output .= "<i class='icon icon-attention-circled'></i> ";
$output .= __( "Sorry, we haven't found anything based on your criteria.", 'kleo_framework' );
$output .= "<br>";
$output .= __( "Please try searching by different terms.", 'kleo_framework' );
$output .= "</div>";
$output .= "</div>";
echo $output;
die();
}
//if there are members
if ( $members['total'] != 0 ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-members">';
$output .= '<h4><span>' . __( "Members", 'kleo_framework' ) . '</span></h4>';
foreach ( (array) $members['users'] as $member ) {
$image = '<img src="' . bp_core_fetch_avatar( array(
'item_id' => $member->ID,
'width' => 25,
'height' => 25,
'html' => false
) ) . '" class="kleo-rounded" alt="">';
if ( $update = bp_get_user_meta( $member->ID, 'bp_latest_update', true ) ) {
$latest_activity = char_trim( trim( strip_tags( bp_create_excerpt( $update['content'], 50, "..." ) ) ) );
} else {
$latest_activity = '';
}
$output .= "<div class ='kleo_ajax_entry'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='" . bp_core_get_user_domain( $member->ID ) . "' class='search_title'>";
$output .= $member->display_name;
$output .= "</a>";
$output .= "<span class='search_excerpt'>";
$output .= $latest_activity;
$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
$output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_members_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View member results', 'kleo_framework' ) . "</a>";
$output .= "</div>";
}
//if there are groups
if ( $groups['total'] != 0 ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-groups">';
$output .= '<h4><span>' . __( "Groups", 'kleo_framework' ) . '</span></h4>';
foreach ( (array) $groups['groups'] as $group ) {
$image = '<img src="' . bp_core_fetch_avatar( array(
'item_id' => $group->id,
'object' => 'group',
'width' => 25,
'height' => 25,
'html' => false
) ) . '" class="kleo-rounded" alt="">';
$output .= "<div class ='kleo_ajax_entry'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='" . bp_get_group_permalink( $group ) . "' class='search_title'>";
$output .= $group->name;
$output .= "</a>";
$output .= "</div>";
$output .= "</div>";
}
$output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_groups_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View group results', 'kleo_framework' ) . "</a>";
$output .= "</div>";
}
//if there are posts
if ( ! empty( $posts ) ) {
$post_type_str = array();
$post_types = array();
$post_type_obj = array();
foreach ( $posts as $post ) {
$post_types[ $post->post_type ][] = $post;
if ( empty( $post_type_obj[ $post->post_type ] ) ) {
$post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
}
}
foreach ( $post_types as $ptype => $post_type ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[ $ptype ]->name ) . '">';
if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
$output .= "<h4><span>" . $post_type_obj[ $ptype ]->labels->name . "</span></h4>";
} else {
$output .= "<hr>";
}
$count = 0;
foreach ( $post_type as $post ) {
$post_type_str[$post->post_type] = $post->post_type;
$count ++;
if ( $count > 4 ) {
continue;
}
$format = get_post_format( $post->ID );
if( $post->post_type == 'attachment') {
$img_url = wp_get_attachment_thumb_url( $post->ID );
$image = '<img src="'.aq_resize( $img_url, 44, 44, true, true, true ).'" class="kleo-rounded"/>';
} else {
if ($img_url = kleo_get_post_thumbnail_url($post->ID)) {
$image = aq_resize($img_url, 44, 44, true, true, true);
if (!$image) {
$image = $img_url;
}
$image = '<img src="' . $image . '" class="kleo-rounded">';
} else {
if ($format == 'video') {
$image = "<i class='icon icon-video'></i>";
} elseif ($format == 'image' || $format == 'gallery') {
$image = "<i class='icon icon-picture'></i>";
} else {
$image = "<i class='icon icon-link'></i>";
}
}
}
$excerpt = "";
if ( ! empty( $post->post_content ) ) {
$excerpt = $post->post_content;
$excerpt = preg_replace( "/\[(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", '', $excerpt );
$excerpt = wp_strip_all_tags($excerpt); //added to remove gogole adsense code from search excerpt
$excerpt = char_trim( trim( strip_tags( $excerpt ) ), 40, "..." );
}
$link = 'activity/?wall_post='.$post->ID;
$result = $post->ID;
$classes = "format-" . $format;
$output .= "<div class ='kleo_ajax_entry $classes'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='$link' class='search_title'>$result";
$output .= $excerpt;
$output .= "</a>";
$output .= "<span class='search_excerpt'>";
$output .= $excerpt;
$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
$output .= '</div>';
}
if ( ! empty( $post_type_str ) ) {
if ( count( $post_type_str ) > 1 ) {
$search_str_posts = '&post_type[]=' . implode( ',', $post_type_str );
} else {
$search_str_posts = '&post_type=' . implode( ',', $post_type_str );
}
} else {
$search_str_posts = '';
}
$output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . $search_str_posts . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
}
/* Forums topics search */
if ( ! empty( $forums ) ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
$output .= '<h4><span>' . __( "Forums", 'kleo_framework' ) . '</span></h4>';
$i = 0;
foreach ( $forums as $fk => $forum ) {
$i ++;
if ( $i <= 4 ) {
$image = "<i class='icon icon-chat-1'></i>";
$output .= "<div class ='kleo_ajax_entry'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='" . $forum['url'] . "' class='search_title'>";
$output .= $forum['name'];
$output .= "</a>";
//$output .= "<span class='search_excerpt'>";
//$output .= $latest_activity;
//$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
}
$output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __( 'View forum results', 'kleo_framework' ) . "</a>";
$output .= "</div>";
}
echo $output;
die();
}
}
function searchfilter($query) {
if ($query->is_search) {
$query->set('post_type',array('um_activity'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');

Add pagination in a Wordpress Latest Posts widget

In a Wordpress Latest Posts widget I want to add pagination
public function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
$count = $instance['count'];
$category = $instance['category'];
echo $before_widget;
$output = '';
if ( $title )
echo $before_title . $title . $after_title;
global $post;
if ( isset( $category ) && $category != '' ) {
$args = array(
'category_name' => $category,
'posts_per_page' => $count,
);
} else {
$args = array(
'posts_per_page' => $count,
);
}
$posts = get_posts( $args );
if(count($posts)>0){
$output .='<div class="sp-latest-posts-widget latest-posts">';
foreach ($posts as $post): setup_postdata($post);
$output .='<div class="media">';
if(has_post_thumbnail()):
$output .='<div class="pull-left">';
$output .=''.get_the_post_thumbnail($post->ID, 'xs-thumb', array('class' => 'img-responsive')).'';
$output .='</div>';
endif;
$output .='<div class="media-body">';
$output .= '<h3 class="entry-title">'. get_the_title() .'</h3>';
$output .= '<div class="entry-meta small"><span class="st-lp-time">'. get_the_time() . '</span> <span clss="st-lp-date">' . get_the_date('d M Y') . '</span></div>';
$output .='</div>';
$output .='</div>';
endforeach;
wp_reset_query();
$output .='</div>';
}
echo $output;
echo $after_widget;
}
I tried with pagination_nav(); after adding code in functions.php and I used WP-PageNavi, but without success.
How to add numbered pagination in this case?
Thanks in advance!
Use below code for numbered pagination,
<?php the_posts_pagination( array(
'mid_size' => 1,
'prev_text' => __( 'Prev', 'textdomain' ),
'next_text' => __( 'Next', 'textdomain' ),
'screen_reader_text' => ' ',
) );
} ?>

PHP display sibling pages as a set image

I am trying to customise the following code so it displays all sibling pages of the current page, but instead of outputting them as the title, they are output as bulletpoints (styled bulletpoints so an image, 'images/bulletpoint.gif'). I have the following code that displays them as the names, but can't seem to customise them to display as an image. Any ideas?
Many thanks
<ul class="subnav_right">
<?php
global $post;
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
wp_list_pages( array(
'title_li' => '',
'child_of' => $current_page_parent,
'depth' => '1' )
);
?>
</ul>
You can customize using walker.I have done it using walker. check following code:
Walker Class:
class Custom_Walker extends Walker_Page {
function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if ( !empty($current_page) ) {
$_current_page = get_post( $current_page );
if ( in_array( $page->ID, $_current_page->ancestors ) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
}
elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
$icon_class = get_post_meta($page->ID, 'icon_class', true); //Retrieve stored icon class from post meta
$output .= $indent . '<li class="' . $css_class . '">';
$output .= '<a href="' . get_permalink($page->ID) . '">' . $link_before;
if($icon_class){ //Test if $icon_class exists
$output .= '<span class="' . $icon_class . '"></span>'; //If it exists output a span with the $icon_class attached to it
}
if($depth!=0){
$output .= apply_filters( 'the_title', '', $page->ID );
}else {
$output .= apply_filters( 'the_title', $page->post_title, $page->ID );
}
$output .= $link_after . '</a>';
if ( !empty($show_date) ) {
if ( 'modified' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
}
}
Pass walker in argumnent like:
wp_list_pages( array( 'title_li' => '',
'child_of' => $current_page_parent,
'depth' => '1',
'walker' => new Custom_Walker())
);

Categories