WordPress post ratings location change of showing - php

I am a new developer. This code has used form plugin for showing rating average and its working well. But its showing rating above of WP content. I want to show rating on my several specific location of blog page and single page. How can I do that?
add_filter( 'the_content', 'ci_comment_rating_display_average_rating' );
function ci_comment_rating_display_average_rating( ) {
global $post;
if ( false === ci_comment_rating_get_average_ratings( $post->ID ) ) {
return $content;
}
$stars = '';
$average = ci_comment_rating_get_average_ratings( $post->ID );
for ( $i = 1; $i <= $average + 1; $i++ ) {
$width = intval( $i - $average > 0 ? 20 - ( ( $i - $average ) * 20 ) : 20 );
if ( 0 === $width ) {
continue;
}
$stars .= '<span style="overflow:hidden; width:' . $width . 'px" class="dashicons dashicons-star-filled"></span>';
if ( $i - $average > 0 ) {
$stars .= '<span style="overflow:hidden; position:relative; left:-' . $width .'px;" class="dashicons dashicons-star-empty"></span>';
}
}
$custom_content = '<p class="average-rating">This post\'s average rating is:-- ' . $average .' ' . $stars .'</p>';
$custom_content .= $content;
return $custom_content;
}

Related

How to use PHP pagination in array's

Since I am trying to learn more about PHP I would like to add Pagination to array's
I have a JSON output that I can read and echo via a foreach. But I am not able to show 10 or 20 records.
I have used the code from this page:
But I miss the place where I can store the echo for the array.
$persons = '[
{"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"},
{"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"},
{"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"},
{"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"},
{"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"},
{"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"},
{"FrstName":"Avery","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Peter","Middlename":"","LastName":"Kap","Online":true,"DeptId":"2"},
{"FrstName":"Denzo","Middlename":"","LastName":"Falc","Online":false,"DeptId":"4"},
{"FrstName":"Eveline","Middlename":"","LastName":"Benzel","Online":false,"DeptId":"3"},
{"FrstName":"Bill","Middlename":"","LastName":"Libuz","Online":false,"DeptId":"3"},
{"FrstName":"April","Middlename":"","LastName":"Gonzo","Online":false,"DeptId":"2"},
{"FrstName":"Harry","Middlename":"","LastName":"Geraldson","Online":true,"DeptId":"3"},
{"FrstName":"Heraldson","Middlename":"","LastName":"McGree","Online":false,"DeptId":"3"}
{"FrstName":"Abraham","Middlename":"","LastName":"Retz","Online":true,"DeptId":"4"},
{"FrstName":"June","Middlename":"","LastName":"Pharee","Online":true,"DeptId":"2"},
{"FrstName":"Anthony","Middlename":"","LastName":"Gonzales","Online":false,"DeptId":"4"},
{"FrstName":"Billy","Middlename":"","LastName":"Scott","Online":false,"DeptId":"3"},
{"FrstName":"Anika","Middlename":"","LastName":"Rose","Online":false,"DeptId":"3"},
{"FrstName":"Kristen","Middlename":"","LastName":"Fontana","Online":false,"DeptId":"2"},
{"FrstName":"Olivia","Middlename":"","LastName":"Menzel","Online":true,"DeptId":"3"},
{"FrstName":"Mark","Middlename":"van","LastName":"Gad","Online":false,"DeptId":"3"}
{"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"}
]';
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $persons ); //total items in array
$limit = 10; //per page
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;
$yourDataArray = array_slice( $persons, $offset, $limit );
$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';
if( $totalPages != 0 )
{
if( $page == 1 )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' « prev page', $page - 1 );
}
$pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>';
if( $page == $totalPages )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' next page » ', $page + 1 );
}
}
$pagerContainer .= '</div>';
echo $pagerContainer;
I would like to know how I can fix this to use pagination for my array.
You need to json_decode() your JSON first to access array methods (count(), array_slice()). Also, your JSON data was invalid, missing some commas, fixed that.
<?php
$persons = '[
{"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"},
{"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"},
{"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"},
{"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"},
{"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"},
{"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"},
{"FrstName":"Avery","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Peter","Middlename":"","LastName":"Kap","Online":true,"DeptId":"2"},
{"FrstName":"Denzo","Middlename":"","LastName":"Falc","Online":false,"DeptId":"4"},
{"FrstName":"Eveline","Middlename":"","LastName":"Benzel","Online":false,"DeptId":"3"},
{"FrstName":"Bill","Middlename":"","LastName":"Libuz","Online":false,"DeptId":"3"},
{"FrstName":"April","Middlename":"","LastName":"Gonzo","Online":false,"DeptId":"2"},
{"FrstName":"Harry","Middlename":"","LastName":"Geraldson","Online":true,"DeptId":"3"},
{"FrstName":"Heraldson","Middlename":"","LastName":"McGree","Online":false,"DeptId":"3"},
{"FrstName":"Abraham","Middlename":"","LastName":"Retz","Online":true,"DeptId":"4"},
{"FrstName":"June","Middlename":"","LastName":"Pharee","Online":true,"DeptId":"2"},
{"FrstName":"Anthony","Middlename":"","LastName":"Gonzales","Online":false,"DeptId":"4"},
{"FrstName":"Billy","Middlename":"","LastName":"Scott","Online":false,"DeptId":"3"},
{"FrstName":"Anika","Middlename":"","LastName":"Rose","Online":false,"DeptId":"3"},
{"FrstName":"Kristen","Middlename":"","LastName":"Fontana","Online":false,"DeptId":"2"},
{"FrstName":"Olivia","Middlename":"","LastName":"Menzel","Online":true,"DeptId":"3"},
{"FrstName":"Mark","Middlename":"van","LastName":"Gad","Online":false,"DeptId":"3"},
{"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"}
]';
// Make the JSON an array, so count() and array_slice() work
$persons = json_decode($persons, true);
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $persons ); //total items in array
// $limit = 10; //per page
// Set limit to 3 for testing:
$limit = 3;
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
// Uncomment this for testing
// $page = 2;
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;
$yourDataArray = array_slice( $persons, $offset, $limit );
$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';
if( $totalPages != 0 )
{
if( $page == 1 )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' « prev page', $page - 1 );
}
$pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>';
if( $page == $totalPages )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' next page » ', $page + 1 );
}
}
$pagerContainer .= '</div>';
echo $pagerContainer;
foreach($yourDataArray as $person) {
echo "\n";
echo "First name: " . $person['FrstName'];
echo " - Middle name: " . $person['Middlename'];
echo " - Last name: " . $person['LastName'];
echo " - Online: " . $person['Online'];
echo " - Dept: " . $person['DeptId'];
}
https://3v4l.org/8hR84

Change the displayed shipping total in WooCommerce

I need to change shipping price programmatically:
<?php
$percentage = 50;
$current_shipping_cost = WC()->cart->get_cart_shipping_total();
echo $current_shipping_cost * $percentage / 100;
?>
Unfortunately it's not working and I always get 0 (zero).
How to change the displayed shipping total with a total based on a calculated discount percentage?
The following will displayed shipping total based on a percentage. There is 2 ways:
1) First way with a custom function.
In the function.php file of your active child theme (or active theme):
function wc_display_cart_shipping_total( $percentage = 100 )
{
$cart = WC()->cart;
$total = __( 'Free!', 'woocommerce' );
if ( 0 < $cart->get_shipping_total() ) {
if ( $cart->display_prices_including_tax() ) {
$total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$total = wc_price( $cart->shipping_total * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
return $totals;
}
Usage:
<?php echo wc_display_cart_shipping_total(50); ?>
2) Second way with a filter hook.
In the function.php file of your active child theme (or active theme):
add_filter( 'woocommerce_cart_shipping_total', 'woocommerce_cart_shipping_total_filter_callback', 11, 2 );
function woocommerce_cart_shipping_total_filter_callback( $total, $cart )
{
// HERE set the percentage
$percentage = 50;
if ( 0 < $cart->get_shipping_total() ) {
if ( $cart->display_prices_including_tax() ) {
$total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$total = wc_price( $cart->shipping_total * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
return $totals;
}
Usage:
<?php echo WC()->cart->get_cart_shipping_total(); ?>

Wordpress CPT Archive Pagination - Displays too many pages

I've been trying to figure out why my custom post type archive is displaying too many pages in the pagination.
My pagination is being displayed by FacetWP using the following code:
<?php echo facetwp_display('pager'); ?>
I've tried customizing how it displays, but each time it still continues to show too many pages. However, when I select a filter, then the pages will be correct.
Is there a way to make it so that it will automatically display the number of pages for each custom post type archive?
EDIT: Since I’m not an advanced php user myself, I've only tried the customization code provided by FacetWP's documentation.
$params = array(
'page' => 1,
'per_page' => 10,
'total_rows' => 205,
'total_pages' => 21 );
And
add_filter( 'facetwp_pager_html', function( $output, $params ) {
$output = '';
if ( 1 < $params['total_pages'] ) {
for ( $i = 1; $i <= $params['total_pages']; $i++ ) {
$is_curr = ( $i === $params['page'] ) ? ' active' : '';
$output .= '<a class="facetwp-page' . $is_curr . '" data-page="' . $i . '">' . $i . '</a>';
}
}
return $output; }, 10, 2 );
And
function my_facetwp_pager_html( $output, $params ) {
$output = '';
$page = $params['page'];
$total_pages = $params['total_pages'];
if ( $page > 1 ) {
$output .= '<a class="facetwp-page" data-page="' . ($page - 1) . '">Previous</a>';
}
if ( $page < $total_pages && $total_pages > 1 ) {
$output .= '<a class="facetwp-page" data-page="' . ($page + 1) . '">Next</a>';
}
return $output; } add_filter( 'facetwp_pager_html', 'my_facetwp_pager_html', 10, 2 );
Sorry if any of that code is messy, let me know if I need to repost for clarification. Thanks in advance for any help!

PHP Table Data Show Results Vertically not Horizontally

I'm really frustrated.. I really need to show this table right. How can I set this code to show table vertically like this:
1 - 4 - 7
2 - 5 - 8
3 - 6 - 9
And not like this:
1 - 2 - 3 - 4
5 - 6 - 7 - 8
9 - ... ect
<?php
$PNG_WEB_DIR = plugin_dir_path( __FILE__ ).'temp/';
wp_mkdir_p($PNG_WEB_DIR);
$cols = $this->settings['cols'];
$rows = $this->settings['rows'];
$label_number = 0;
// die($_GET['order_ids']);
$order_ids = explode(',',$_GET['ids']);
$order_count = count($order_ids);
$labels_per_page = $cols*$rows;
$page_count = ceil(($order_count)/$labels_per_page);
for ($page=0; $page < $page_count; $page++) {
echo '<table class="address-labels" width="100%" height="100%" border="0" cellpadding="0">';
$last_height = 0;
$current_height = $current_width = 0;
$current_row = 0;
for ($label=0; $label < $labels_per_page; $label++) {
$label_number++;
$current_col = (($label_number-1) % $cols)+1;
if ($current_col == 1) {
$last_height = $current_height;
$last_width = 0;
$current_row++;
echo '<tr class="label-row">';
}
if ( $label_number > $this->offset && isset($order_ids[$label_number - $this->offset - 1]) ) {
$order_id = $order_ids[$label_number - $this->offset - 1];
} else {
$order_id = '';
}
$current_width = round( $current_col * (100/$cols) );
$width = $current_width - $last_width;
$last_width = $current_width;
$current_height = round( $current_row * (100/$rows) );
$height = $current_height - $last_height;
printf('<td width="%s%%" height="%s%%" class="label"><div class="label-wrapper">', $width, $height);
// because we are also looping through the empty labels,
// we need to check if there's an order for this label
if (!empty($order_id)) {
// get label data from order
$order = new WC_Order( $order_id );
// replace placeholders
$label_data = isset($this->settings['address_data'])? nl2br( $this->settings['address_data'] ): '[shipping_address]';
$label_data = $this->make_replacements( $label_data, $order );
echo '<div class="address-block">';
echo '<div class="addrress_show">';
// process label template to display content
echo $label_data;
echo '</div>';
echo '<div class="clearb"></div>';
echo '</div>';
} else {
echo ' ';
}
echo '</div></td>';
if ($current_col == $cols) {
echo '</tr>';
}
}
echo '</table>';
}
// shpt_after_page hook
do_action( 'shpt_after_page' );
?>
Since I can't know your raw data structure which you use to loop through it and produce the table, I presumed it as one-dimensional array and you want to print the table as 3 columns as you said, to see in action here's a PHP Fiddle - hit run to execute it - this will work for any array length as long as columns is 3, the rows is dynamic
PHP: Updated for naming purpose
<?php
$table = '<table class="address-labels" width="30%" border="0" cellpadding="0">';
$dataArr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17);
$colNum = 3;
$rowNum = ceil(count($dataArr)/$colNum);
for($i=0; $i < $rowNum; $i++){
$x1 = isset( $dataArr[$i] ) ? $dataArr[$i] : '-';
$x2 = isset( $dataArr[$i + $rowNum] ) ? $dataArr[$i + $rowNum] : '-';
$x3 = isset( $dataArr[$i + 2 * $rowNum] ) ? $dataArr[$i + 2 * $rowNum] : '-';
$table .= '<tr><td>' . $x1 . '</td><td>' . $x2 . '</td><td>' . $x3 . '</td></tr>';
}
$table .= '</table>';
echo $table;
?>
EDIT:
Now if you want to change the column number, like set it to 5 for example, you need to do the following:
1st - set $colNum = 5;
2nd - add more variables to hold the values like, *considering it is 5 columns:
$x4 = isset( $dataArr[$i + 3 * $rowNum] ) ? $dataArr[$i + 3 * $rowNum] : '-';
$x5 = isset( $dataArr[$i + 4 * $rowNum] ) ? $dataArr[$i + 4 * $rowNum] : '-';
and don't forget to increase the multiplier * $rowNum
then for each extra column you add you need to add <td></td> like:
'<td>' . $x4 . '</td><td>' . '</td><td>' . $x5 . '</td><td>' . '</td>'
Here's another PHP Fiddle showing the above for 5 columns.
Try this. It's in pure php
<?php
$cols = 3;
$data = range(1,29);
// classic
$index = 1;
foreach($data as $el)
{
echo $el.' '; if ($index % $cols ==0) echo '<br/>';
$index++;
}
echo '<br/><br/>';
// special
$rows = $cols ;
$cols = floor(count($data)/$cols);
for($row = 0 ; $row < $rows; $row++){
for ($index = $row; $index < count($data); $index += $rows)
echo $data[$index].' ';
echo '<br/>';
}

WordPress pagination fuction in shortcode always displayed on the top

WordPress pagination function in shortcode always displayed on the top. Please have a look on below code
/*-------------------------------------------------------------------------*/
/* Custom Pagination */
/*-------------------------------------------------------------------------*/
function suareztheme_pagination($pages = '', $range = 2){
$showitems = ( $range * 2 ) + 1;
global $paged;
if(empty($paged))
$paged = 1;
if($pages == ''){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){
$pages = 1;
}
}
if( 1 != $pages ){
$pagination_html .= '<div class="pagination">';
if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ){
$pagination_html .= '«';
}
if( $paged > 1 && $showitems < $pages ){
$pagination_html .= '‹';
}
for ( $i = 1; $i <= $pages; $i++ ){
if ( 1 != $pages && ( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
if ( $paged == $i ){
$pagination_html .= '<span class="current">' . $i . '</span>';
} else{
$pagination_html .= '' . $i . '';
}
}
}
if ( $paged < $pages && $showitems < $pages ){
$pagination_html .= '›';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ){
$pagination_html .= '»';
}
$pagination_html .= '</div>';
return $pagination_html;
}
}
Then I called it in the shortcode function.
/*-------------------------------------------------------------------------*/
/* Grid Shortcode */
/*-------------------------------------------------------------------------*/
function RecentBlog($atts, $content = null) {
extract(shortcode_atts(array(
"comments" => 'true',
"date" => 'true',
"columns" => '4',
"limit" => '-1',
"title" => 'true',
"description" => 'true',
"cat_slug" => '',
"post_type" => '',
"excerpt_length" => '15',
"readmore_text" => '',
"pagination" => 'false'
), $atts));
global $post;
$postformat = get_post_format();
....
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
......
if (have_posts()) : while (have_posts()) : the_post();
$postformat = get_post_format();
if( $postformat == "" ) $postformat="standard";
$protected = "";
$portfoliogrid .= '<div class="' . $column_no . ' post grid-post post-' . $postformat . '">';
.....////
$portfoliogrid .= '<div class="summary-info">';
.......
$portfoliogrid .='</div>';
// If either of title and description needs to be displayed.
if ( $title == "true" || $description == "true" ) {
$portfoliogrid .='<div class="work-details">';
......
$portfoliogrid .='</div>';
}
$portfoliogrid .='</div>';
endwhile; endif;
if ( $pagination == "true" ){
if ( isset( $additional_loop ) ){
echo suareztheme_pagination( $additional_loop->max_num_pages );
} else {
echo suareztheme_pagination();
}
if ( function_exists("suareztheme_pagination") ) {
} else {
next_posts_link('«« Older Posts');
previous_posts_link('Newer Posts »»');
}
}
wp_reset_query();
return $portfoliogrid;
}
add_shortcode( "recentblog", "RecentBlog" );
The above code is working fine , but it has one problem, when it display in specific page, it always displayed on the top regardless it's place. So appreciate your support to help me, thanks in advance.
In the shortcode callback, you need to concatenate and return the html, e.g.:
$portfoliogrid .= suareztheme_pagination();
Note that you'll have to use get_next_posts_link() and get_previous_posts_link() instead of next_posts_link() and previous_posts_link().

Categories