Limited Wordpress Custom Pagination - php

Hi to all I have a funtion to create a numeric pagination for blog posts:
function fb_paging_bar( $args = array() ) {
$defaults = array(
'range' => 4,
'custom_query' => FALSE,
'previous_string' => __( 'PREVIOUS', 'themename' ),
'next_string' => __( 'NEXT PAGE', 'themename' ),
'view_fp' => TRUE,
'view_lp' => TRUE,
'before_output' => '<div class="paginator" id="movie_paginator">',
'after_output' => '</div>'
);
$args = wp_parse_args(
$args,
apply_filters( 'fb_paging_bar_defaults', $defaults )
);
$args['range'] = (int) $args['range'] - 1;
if ( !$args['custom_query'] )
$args['custom_query'] = #$GLOBALS['wp_query'];
$count = (int) $args['custom_query']->max_num_pages;
$page = intval( get_query_var( 'paged' ) );
$ceil = ceil( $args['range'] / 2 );
if ( $count <= 1 )
return FALSE;
if ( !$page )
$page = 1;
if ( $count > $args['range'] ) {
if ( $page <= $args['range'] ) {
$min = 1;
$max = $args['range'] + 1;
} elseif ( $page >= ($count - $ceil) ) {
$min = $count - $args['range'];
$max = $count;
} elseif ( $page >= $args['range'] && $page < ($count - $ceil) ) {
$min = $page - $ceil;
$max = $page + $ceil;
}
} else {
$min = 1;
$max = $count;
}
$echo = '';
$previous = intval($page) - 1;
$previous = esc_attr( get_pagenum_link($previous) );
if ( $previous && (1 != $page) )
$echo .= '<a class="page page-prev" href="' . $previous . '" title="' . __( 'PREVIOUS', 'themename' ) . '">' . $args['previous_string'] . '</a>';
$firstpage = esc_attr( get_pagenum_link(1) );
if ( $args['view_fp'] && $firstpage && (1 != $page) )
$echo .= '<a class="page" href="' . $firstpage . '">' . __( '1', 'themename' ) . '</a>';
if ( !empty($min) && !empty($max) ) {
for( $i = $min; $i <= $max; $i++ ) {
if ($page == $i) {
$echo .= '<span class="page page-mobile disabled">' . str_pad( (int)$i, 1, '0', STR_PAD_LEFT ) . '</span>';
} else {
$echo .= sprintf( '%002d', esc_attr( get_pagenum_link($i) ), $i );
}
}
}
if ($args['view_lp']) {
$lastpage = esc_attr( get_pagenum_link($count) );
if ( $lastpage && ($count != $page) ) {
$count = str_pad( (int)$count, 2, '0', STR_PAD_LEFT );
$echo .= '<span class="page no-page page-mobile disabled">...</span><a class="page" href="' . $lastpage . '">' . $count . '</a>';
}
}
$next = intval($page) + 1;
$next = esc_attr( get_pagenum_link($next) );
if ($next && ($count != $page) )
$echo .= '<a class="page page-next" href="' . $next . '" title="' . __( 'NEXT PAGE', 'themename') . '">' . $args['next_string'] . '</a>';
if ( isset($echo) )
echo $args['before_output'] . $echo . $args['after_output'];
}
The result of this code above is like the first image
But Im trying to find the right way to add more numers because as u see this one shows only first,current and last number
is there any way to make it with more numbers like the second screenshot?

Related

Display related post instead of recent post

I am trying to figure out a Jupiter theme blog > components > blog-similar-posts.php code. I would like to show Related Posts instead of Recent Posts on the blog pages. What should I change in the following codes?
Here is the original code I have, thanks! I think I couldn't figure out the logistic between the $recent and $related. There might be something I changed wrong during the process
<?php
global $post, $mk_options;
if ($mk_options['enable_single_related_posts'] == 'true' && get_post_meta($post->ID, '_disable_related_posts', true) != 'false') :
$backup = $post;
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
$related_post_found = false;
$layout = get_post_meta($post->ID, '_layout', true);
$layout = !empty($layout) ? $layout : 'full';
$layout = ($layout == 'default') ? $mk_options['single_layout'] : $layout;
if ($layout == 'full') {
$showposts = 3; /* 4 before */
$width = ($mk_options['grid_width'] / 3 /* 4 before */) - 30;
$height = ($mk_options['grid_width'] / 3 /* 4 before */) - 80;
$column_css = 'three-cols' /* four-cols before */;
}
else {
$showposts = 3;
$width = (($mk_options['content_width'] / 100) * $mk_options['grid_width']) / 3;
$height = ((($mk_options['content_width'] / 100) * $mk_options['grid_width']) / 3) - 40;
$column_css = 'three-cols';
}
if (!function_exists('mk_similar_posts_html')) {
function mk_similar_posts_html($title, $width, $height, $column_css, $query) {
$output = '<section class="blog-similar-posts">';
$output.= '<div class="similar-post-title">' . esc_html( $title ) . '</div>';
$output.= '<ul class="' . esc_attr( $column_css ) . '">';
while ($query->have_posts()) {
$query->the_post();
$output.= '<li><div class="similar-post-holder">';
$output.= '<a class="mk-similiar-thumbnail" href="' . esc_url( get_permalink() ) . '" title="' . the_title_attribute(array('echo' => false)) . '">';
if ( class_exists( 'Jupiter_Donut' ) ) {
$image_src = Mk_Image_Resize::resize_by_id_adaptive( get_post_thumbnail_id(), 'crop', $width, $height, $crop = true, $dummy = true);
$output.= '<img src="' . $image_src['dummy'] . '" ' . $image_src['data-set'] . ' alt="' . the_title_attribute(array('echo' => false)) . '" />';
}
$output.= '<div class="image-hover-overlay"></div></a>';
$output.= '' . get_the_title() . '';
$output.= '</div></li>';
}
$output.= '</ul>';
$output.= '<div class="clearboth"></div></section>';
echo $output;
}
}
if ($tags) {
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$related = new WP_Query(array(
'tag__in' => $tagIDs,
'post__not_in' => array(
$post->ID
) ,
'showposts' => $showposts,
'ignore_sticky_posts' => 1
));
$output = '';
if ($related->have_posts()) {
$related_post_found = true;
mk_similar_posts_html( esc_html__( 'Recommended Posts', 'mk_framework' ) , $width, $height, $column_css, $related);
}
$post = $backup;
}
if (!$related_post_found) {
$recent = new WP_Query(array(
'showposts' => $showposts,
'nopaging' => 0,
'post__not_in' => array(
$post->ID
),
'post_status' => 'publish',
'ignore_sticky_posts' => 1
));
$output = '';
if ($recent->have_posts()) {
$related_post_found = true;
mk_similar_posts_html( esc_html__( 'Recent Posts', 'mk_framework' ) , $width, $height, $column_css, $recent );
}
}
wp_reset_postdata();
echo $output;
endif;

Breadcrumb Incrementing Number for meta position content

I'm new to PHP, still learning, so please excuse and kindly tell me what would be the appropriate fix. I used the breadcrumb script from here answered by Pieter Goosen. I want the breadcrumbs to work with google and schema, the part that isn't working is the <meta itemprop="position" content="number" /> tag. The content="number" needs to be set for each crumb, in order (1,2,3...). On line 8 I added $i = 1;, then changed $link_after = '<meta itemprop="position" content="' . $i++ . '" /></li>';. But $i++ is not printing a new sequential number, it is just repeating itself as if there was no other $i++. My guess is to apply the rule globally and to search for $i++ used, as it's probably being applied closed within the rule, but wouldn't know how to do this.
Code:
function get_hansel_and_gretel_breadcrumbs()
{
// Set variables for later use
$here_text = __( 'You are currently here!' );
$home_link = home_url('/');
$home_text = __( 'Home' );
$link_before = '<span typeof="v:Breadcrumb">';
$i = 1;
$link_after = '<meta itemprop="position" content="' . $i++ . '" /></span>'
$link_attr = ' rel="v:url" property="v:title"';
$link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
$delimiter = ' » '; // Delimiter between crumbs
$before = '<span class="current">'; // Tag before the current crumb
$after = '</span>'; // Tag after the current crumb
$page_addon = ''; // Adds the page number if the query is paged
$breadcrumb_trail = '';
$category_links = '';
/**
* Set our own $wp_the_query variable. Do not use the global variable version due to
* reliability
*/
$wp_the_query = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();
// Handle single post requests which includes single pages, posts and attatchments
if ( is_singular() )
{
/**
* Set our own $post variable. Do not use the global variable version due to
* reliability. We will set $post_object variable to $GLOBALS['wp_the_query']
*/
$post_object = sanitize_post( $queried_object );
// Set variables
$title = apply_filters( 'the_title', $post_object->post_title );
$parent = $post_object->post_parent;
$post_type = $post_object->post_type;
$post_id = $post_object->ID;
$post_link = $before . $title . $after;
$parent_string = '';
$post_type_link = '';
if ( 'post' === $post_type )
{
// Get the post categories
$categories = get_the_category( $post_id );
if ( $categories ) {
// Lets grab the first category
$category = $categories[0];
$category_links = get_category_parents( $category, true, $delimiter );
$category_links = str_replace( '<a', $link_before . '<a' . $link_attr, $category_links );
$category_links = str_replace( '</a>', '</a>' . $link_after, $category_links );
}
}
if ( !in_array( $post_type, ['post', 'page', 'attachment'] ) )
{
$post_type_object = get_post_type_object( $post_type );
$archive_link = esc_url( get_post_type_archive_link( $post_type ) );
$post_type_link = sprintf( $link, $archive_link, $post_type_object->labels->singular_name );
}
// Get post parents if $parent !== 0
if ( 0 !== $parent )
{
$parent_links = [];
while ( $parent ) {
$post_parent = get_post( $parent );
$parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );
$parent = $post_parent->post_parent;
}
$parent_links = array_reverse( $parent_links );
$parent_string = implode( $delimiter, $parent_links );
}
// Lets build the breadcrumb trail
if ( $parent_string ) {
$breadcrumb_trail = $parent_string . $delimiter . $post_link;
} else {
$breadcrumb_trail = $post_link;
}
if ( $post_type_link )
$breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail;
if ( $category_links )
$breadcrumb_trail = $category_links . $breadcrumb_trail;
}
// Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
if( is_archive() )
{
if ( is_category()
|| is_tag()
|| is_tax()
) {
// Set the variables for this section
$term_object = get_term( $queried_object );
$taxonomy = $term_object->taxonomy;
$term_id = $term_object->term_id;
$term_name = $term_object->name;
$term_parent = $term_object->parent;
$taxonomy_object = get_taxonomy( $taxonomy );
$current_term_link = $before->labels->singular_name . '<li class="curr-cat" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . $term_name . '</span><meta itemprop="position" content="' . $i++ . '" /></li>';
$parent_term_string = '';
if ( 0 !== $term_parent )
{
// Get all the current term ancestors
$parent_term_links = [];
while ( $term_parent ) {
$term = get_term( $term_parent, $taxonomy );
$parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name );
$term_parent = $term->parent;
}
$parent_term_links = array_reverse( $parent_term_links );
$parent_term_string = implode( $delimiter, $parent_term_links );
}
if ( $parent_term_string ) {
$breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link;
} else {
$breadcrumb_trail = $current_term_link;
}
} elseif ( is_author() ) {
$breadcrumb_trail = __( 'Author archive for ') . $before . $queried_object->data->display_name . $after;
} elseif ( is_date() ) {
// Set default variables
$year = $wp_the_query->query_vars['year'];
$monthnum = $wp_the_query->query_vars['monthnum'];
$day = $wp_the_query->query_vars['day'];
// Get the month name if $monthnum has a value
if ( $monthnum ) {
$date_time = DateTime::createFromFormat( '!m', $monthnum );
$month_name = $date_time->format( 'F' );
}
if ( is_year() ) {
$breadcrumb_trail = $before . $year . $after;
} elseif( is_month() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after;
} elseif( is_day() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$month_link = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );
$breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after;
}
} elseif ( is_post_type_archive() ) {
$post_type = $wp_the_query->query_vars['post_type'];
$post_type_object = get_post_type_object( $post_type );
$breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after;
}
}
// Handle the search page
if ( is_search() ) {
$breadcrumb_trail = __( 'Search query for: ' ) . $before . get_search_query() . $after;
}
// Handle 404's
if ( is_404() ) {
$breadcrumb_trail = $before . __( 'Error 404' ) . $after;
}
// Handle paged pages
if ( is_paged() ) {
$current_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
$page_addon = $before . sprintf( __( ' ( Page %s )' ), number_format_i18n( $current_page ) ) . $after;
}
$breadcrumb_output_link = '';
$breadcrumb_output_link .= '<div class="breadcrumb">';
if ( is_home()
|| is_front_page()
) {
// Do not show breadcrumbs on page one of home and frontpage
if ( is_paged() ) {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $page_addon;
}
} else {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $delimiter;
$breadcrumb_output_link .= $breadcrumb_trail;
$breadcrumb_output_link .= $page_addon;
}
$breadcrumb_output_link .= '</div><!-- .breadcrumbs -->';
return $breadcrumb_output_link;
}

CMB2 Filelist type paginate results (uses get_post_meta)

This is getting close to what I need. It splits out 5 results from the $files (get_post_meta) and creates pages but the images are all the same images on all the pages. I am beyond the limit of my brain.
function gallery_loop() {
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$img_size = 'portfolio-catalog';
$files = get_post_meta(get_the_ID(), '_cmb_gallery_images', true);
$limit = 5;
$total = count( $files );
$pages = ceil( $total / $limit );
$curr_page = isset($_GET['page']);
$offset = ($curr_page - 1) * $limit;
$items_array = array_chunk((array) $files, $limit, true);
$files_array = array_slice($items_array, $offset, true); // this is showing the same 5 items on all the pages
foreach ($files_array as $files) {
echo '<div style="border:1px solid red;">'; //BEGIN "page" so I can see if they are splitting correctly
foreach ($files as $attachment_id => $attachment_url) {
$page=1;
echo '<div class="file-list-image">';
echo wp_get_attachment_image($attachment_id, $img_size);
echo '</div>';
$page++;
} // end $files as $attachment_id => $attachment_url
echo '</div>'; //END "page" so I can see if they are splitting correctly
} // end foreach $files_array as $files
//the correct amount of pages are showing up but the items are all the same
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'total' => $pages
) );
}
// end function
Something like this should do the trick.
PHP
<?php
$files_array=array_chunk((array) $files, 10, true); //10 is an amount per page here
foreach ($files_array as $files)
{
$page=1;
echo '<div data-imgpage="' . $page . '" class="images_page' . (($page > 1) ? 'hidden_class' : '') . '">';
foreach ($files as $attachment_id => $attachment_url)
{
echo '<div class="file-list-image">';
echo wp_get_attachment_image($attachment_id, $img_size);
echo '</div>';
}
echo '</div>';
$page++;
}
// now create some pagination if needed
echo ((count($files_array) > 1) ? '<div class="pagination_container">' . create_pagination(1, count($files_array)) . '</div>' : '');
function create_pagination($current_page, $limit)
{
$first=((($current_page - 2) > 0) ? $current_page - 2 : 1);
$last=((($current_page + 2) < $limit) ? $current_page + 2 : $limit);
$html='<ul data-imglisting-pagination-max="' . $limit . '">';
if ($first > 1)
{
$html .= '<li data-imglisting-pagination-page="1" data-imglisting-pagination-action="active"><span class="page_class">1</span></li>';
$html .= '<li data-imglisting-pagination-action="disabled" class="disabled_page_class"><span>...</span></li>';
}
for ($i=$first; $i <= $last; $i ++)
{
$html .= '<li data-imglisting-pagination-page="' . $i . '" data-imglisting-pagination-action="active" ' . (($current_page == $i) ? 'class="active_page_class"' : "" ) . '><span class="page_class">' . $i . '</span></li>';
}
if ($last < $limit)
{
$html .= '<li data-imglisting-pagination-action="disabled" class="disabled_page_class"><span>...</span></li>';
$html .= '<li data-imglisting-pagination-page="' . $limit . '" data-imglisting-pagination-action="active"><span class="page_class">' . $limit . '</span></li>';
}
$html .= '</ul>';
return $html;
}
JS
jQuery(document).ready(function ($)
{
var current_page = 1;
bind_stuff();
function bind_stuff()
{
$('[data-imglisting-pagination-action="active"]').unbind('click');
$('[data-imglisting-pagination-action="active"]').click(function ()
{
if (current_page != $(this).attr('data-imglisting-pagination-page'))
{
current_page = $(this).attr('data-imglisting-pagination-page');
$('[data-imglisting-pagination-action="active"]').removeClass('active_page_class');
$('[data-imgpage]').hide();
$('[data-imgpage="' + current_page + '"]').show();
$(this).addClass('active_page_class');
$('.pagination_container').html(build_pagination(current_page, parseInt($('data-imglisting-pagination-max').attr('data-imglisting-pagination-max'))));
bindStuff();
}
});
}
});
function build_pagination(current, limit)
{
var first = (((current - 2) > 0) ? current - 2 : 1);
var last = (((current + 2) < limit) ? current + 2 : limit);
output = '<ul data-imglisting-pagination-max="' + limit + '">';
if (first > 1)
{
output = output + '<li data-imglisting-pagination-page="1" data-imglisting-pagination-action="active"><span class="page_class">1</span></li>';
output = output + '<li data-imglisting-pagination-action="disabled" class="disabled_page_class"><span>...</span></li>';
}
for (i = first; i <= last; i++)
{
output = output + '<li data-imglisting-pagination-page="' + i + '" data-imglisting-pagination-action="active" ' + ((current == i) ? 'class="active_page_class"' : "") + '><span class="page_class">' + i + '</span></li>';
}
if (last < limit)
{
output = output + '<li data-imglisting-pagination-action="disabled" class="disabled_page_class"><span>...</span></li>';
output = output + '<li data-imglisting-pagination-page="' + limit + '" data-imglisting-pagination-action="active"><span class="page_class">' + limit + '</span></li>';
}
output = output + '</ul>';
return output;
}

Update wp_term_relationships table by using the value of wp_postmeta

Hello There I have created a meta box for date in my functions.php of admin. The code is here:-
function ep_eventposts_metaboxes() {
//add_meta_box( 'ept_event_date_start', 'Start Date and Time', 'ept_event_date', 'event', 'side', 'default', array( 'id' => '_start') );
add_meta_box( 'ept_event_date_end', 'Expiratory Date and Time', 'ept_event_date', 'post', 'side', 'default', array('id'=>'_end') );
//add_meta_box( 'ept_event_location', 'Event Location', 'ept_event_location', 'event', 'normal', 'default', array('id'=>'_end') );
}
add_action( 'admin_init', 'ep_eventposts_metaboxes' );
// Metabox HTML
function ept_event_date($post, $args) {
$metabox_id = $args['args']['id'];
global $post, $wp_locale;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
$time_adj = current_time( 'timestamp' );
$month = get_post_meta( $post->ID, $metabox_id . '_month', true );
if ( empty( $month ) ) {
$month = gmdate( 'm', $time_adj );
}
$day = get_post_meta( $post->ID, $metabox_id . '_day', true );
if ( empty( $day ) ) {
$day = gmdate( 'd', $time_adj );
}
$year = get_post_meta( $post->ID, $metabox_id . '_year', true );
if ( empty( $year ) ) {
$year = gmdate( 'Y', $time_adj );
}
$hour = get_post_meta($post->ID, $metabox_id . '_hour', true);
if ( empty($hour) ) {
$hour = gmdate( 'H', $time_adj );
}
$min = get_post_meta($post->ID, $metabox_id . '_minute', true);
if ( empty($min) ) {
//$min = '00';
$min = gmdate( 'i', $time_adj );
}
$month_s = '<select name="' . $metabox_id . '_month">';
for ( $i = 1; $i < 13; $i = $i +1 ) {
$month_s .= "\t\t\t" . '<option value="' . zeroise( $i, 2 ) . '"';
if ( $i == $month )
$month_s .= ' selected="selected"';
$month_s .= '>' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>\n";
}
$month_s .= '</select>';
echo $month_s;
echo '<input type="text" name="' . $metabox_id . '_day" value="' . $day . '" size="2" maxlength="2" />';
echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> # ';
echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';
echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';
}
/*
function ept_event_location() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
// The metabox HTML
$event_location = get_post_meta( $post->ID, '_event_location', true );
echo '<label for="_event_location">Location:</label>';
echo '<input type="text" name="_event_location" value="' . $event_location . '" />';
}
*/
// Save the Metabox Data
function ep_eventposts_save_meta( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST['ep_eventposts_nonce'] ) )
return;
if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
return;
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though
$metabox_ids = array('_end' );
foreach ($metabox_ids as $key ) {
$aa1 = $_POST[$key . '_year'];
$mm1 = $_POST[$key . '_month'];
$jj1 = $_POST[$key . '_day'];
$hh = $_POST[$key . '_hour'];
$mn = $_POST[$key . '_minute'];
$aa1 = ($aa1 <= 0 ) ? date('Y') : $aa1;
$mm1 = ($mm1 <= 0 ) ? date('n') : $mm1;
$jj1 = sprintf('%02d',$jj1);
$jj1 = ($jj1 > 31 ) ? 31 : $jj1;
$jj1 = ($jj1 <= 0 ) ? date('j') : $jj1;
$hh = sprintf('%02d',$hh);
$hh = ($hh > 23 ) ? 23 : $hh;
$hh = ($hh <= 0 ) ? date('H') : $hh;
$mn = sprintf('%02d',$mn);
$mn = ($mn > 59 ) ? 59 : $mn;
$mn = ($mn <= 0 ) ? date('i') : $mn;
$events_meta[$key . '_year'] = $aa1;
$events_meta[$key . '_month'] = $mm1;
$events_meta[$key . '_day'] = $jj1;
$events_meta[$key . '_hour'] = $hh;
$events_meta[$key . '_minute'] = $mn;
$events_meta[$key . '_eventtimestamp'] = $aa1 ."-" .$mm1 ."-". $jj1." " . $hh.":" . $mn.":".date('s');
echo $events_meta;
}
// Add values of $events_meta as custom fields
foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!
if ( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode( ',', (array)$value ); // If $value is an array, make it a CSV (unlikely)
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank
}
}
add_action( 'save_post', 'ep_eventposts_save_meta', 1, 2 );
/**
* Helpers to display the date on the front end
*/
// Get the Month Abbreviation
function eventposttype_get_the_month_abbr($month) {
global $wp_locale;
for ( $i = 1; $i < 13; $i = $i +1 ) {
if ( $i == $month )
$monthabbr = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
}
return $monthabbr;
}
// Display the date
function eventposttype_get_the_event_date() {
global $post;
$eventdate = '';
$month = get_post_meta($post->ID, '_month', true);
$eventdate = eventposttype_get_the_month_abbr($month);
$eventdate .= ' ' . get_post_meta($post->ID, '_day', true) . ',';
$eventdate .= ' ' . get_post_meta($post->ID, '_year', true);
$eventdate .= ' at ' . get_post_meta($post->ID, '_hour', true);
$eventdate .= ':' . get_post_meta($post->ID, '_minute', true);
echo $eventdate;
}
It works fine and adding the metabox on post. And Putting the value in wp_postmeta.
Now i have created a function to change the value add a TaxonomyID for the post after the condition satisfies. The code is here:-
function wp_insert_category_to_post()
{
$postid=get_the_ID();
echo $meta_values = get_post_meta( $postid, '_end_minute', true )."</br>";
echo $meta_values = get_post_meta( $postid, '_end_hour', true )."</br>";
echo $meta_values1 = get_post_meta( $postid, '_end_month', true )."</br>";
echo $meta_values2 = get_post_meta( $postid, '_end_day', true )."</br>";
echo $meta_values3 = get_post_meta( $postid, '_end_year', true )."</br>";
echo $meta_values3 = get_post_meta( $postid, '_end_eventtimestamp', true )."</br>";
global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_postmeta where meta_key='_end_eventtimestamp' And meta_value between '2015-07-01' and now() ;") as $key => $row) {
// each column in your row will be accessible like this
$my_column = $row->post_id;
//$wpdb->insert($wpdb->wp_term_relationships, array("object_id" => $row->post_id, "term_taxonomy_id" => 3, "term_order" => 0), array("%d", %d", "%d"));
$sql = $wpdb->prepare( "INSERT INTO $wpdb->wp_term_relationships (object_id, term_taxonomy_id,term_order ) VALUES ( %d, %d, %d )", $row->post_id, 3, 0 );
print_r($sql);
$res= $wpdb->query($sql);
//$wpdb->query($sql);
//echo "<br> My value = ".$my_column."<br>";
}
return $res;
}
add_action)('init','wp_insert_category_to_post');
It is written in post.php of wp-admin. But Its not working. can any one tell me the reason. And give the solution.

Adding pagination in WordPress Football Pool

I'm using a wordpress plugin to make a football pool. A really nice plugin but there is nog pagination in the ranking. So when you have over 1000 participants you will get a very long list.
The plugin author provided a class wich you can use to create pagination. My php knowledge is unfortunately to weak for this.
This is the pagination class:
<?php
class Football_Pool_Pagination {
public $show_total = true;
public $page_param = 'paged';
public $current_page = 1;
public $wrap = false;
private $total_pages = 0;
private $total_items = 0;
private $page_size = 20;
public function __construct( $num_items, $wrap = false ) {
$this->total_items = $num_items;
$this->total_pages = $this->calc_total_pages( $num_items, $this->page_size );
$this->current_page = $this->get_pagenum();
$this->wrap = $wrap;
}
public function get_page_size() {
return $this->page_size;
}
public function set_page_size( $size ) {
$this->page_size = $size;
$this->total_pages = $this->calc_total_pages( $this->total_items, $this->page_size );
$this->current_page = $this->get_pagenum();
}
public function show( $return = 'echo' ) {
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
if ( $this->total_pages ) {
$page_class = $this->total_pages < 2 ? ' one-page' : '';
} else {
$page_class = ' no-pages';
}
$output = '';
if ( $this->wrap ) $output .= sprintf( '<div class="tablenav top%s">', $page_class );
$output .= sprintf( '<div class="tablenav-pages%s">', $page_class );
if ( $this->show_total ) {
$output .= sprintf( '<span class="displaying-num">%s</span>'
, sprintf( _n( '1 item', '%s items', $this->total_items, FOOTBALLPOOL_TEXT_DOMAIN )
, $this->total_items
)
);
}
$disable_first = $disable_last = '';
if ( $this->current_page == 1 ) {
$disable_first = ' disabled';
}
if ( $this->current_page == $this->total_pages ) {
$disable_last = ' disabled';
}
$output .= '<span class="pagination-links">';
$output .= sprintf( '<a class="first-page%s" title="%s" href="%s">«</a>'
, $disable_first
, esc_attr__( 'Go to the first page' )
, esc_url( remove_query_arg( $this->page_param, $current_url ) )
);
$output .= sprintf( '<a class="prev-page%s" title="%s" href="%s">‹</a>'
, $disable_first
, esc_attr__( 'Go to the previous page' )
, esc_url( add_query_arg(
$this->page_param, max( 1, $this->current_page - 1 ),
$current_url ) )
);
$output .= sprintf( '<span class="paging-input"><input class="current-page" title="%s" type="text" name="%s" value="%d" size="%d"> of <span class="total-pages">%d</span></span>'
, esc_attr__( 'Current page' )
, $this->page_param
, $this->current_page
, strlen( $this->total_pages )
, $this->total_pages
);
$output .= sprintf( '<a class="next-page%s" title="%s" href="%s">›</a>'
, $disable_last
, esc_attr__( 'Go to the next page' )
, esc_url( add_query_arg(
$this->page_param, min( $this->total_pages, $this->current_page + 1 ),
$current_url ) )
);
$output .= sprintf( '<a class="last-page%s" title="%s" href="%s">»</a>'
, $disable_last
, esc_attr__( 'Go to the last page' )
, esc_url( add_query_arg( $this->page_param, $this->total_pages, $current_url ) )
);
$output .= '</span></div>';
if ( $this->wrap ) $output .= '</div>';
if ( $return == 'echo' ) {
echo $output;
} else {
return $output;
}
}
private function calc_total_pages( $num_items, $page_size ) {
return ceil( $num_items / $page_size );
}
private function get_pagenum() {
$page_num = Football_Pool_Utils::request_int( $this->page_param, 0 );
if( $page_num > $this->total_pages ) {
$page_num = $this->total_pages;
}
return max( 1, $page_num );
}
}
This is the class for ranking:
<?php
class Football_Pool_Ranking_Page {
public function page_content()
{
global $current_user;
get_currentuserinfo();
$output = '';
$pool = new Football_Pool_Pool;
// $userleague = get_the_author_meta( 'footballpool_league', $current_user->ID );
$userleague = $pool->get_league_for_user( $current_user->ID );
$userleague = ( isset( $userleague ) && is_integer( $userleague ) ) ? $userleague : FOOTBALLPOOL_LEAGUE_ALL;
$league = Football_Pool_Utils::post_string( 'league', $userleague );
$ranking_display = Football_Pool_Utils::get_fp_option( 'ranking_display', 0 );
if ( $ranking_display == 1 ) {
$ranking = Football_Pool_Utils::post_int( 'ranking', FOOTBALLPOOL_RANKING_DEFAULT );
} elseif ( $ranking_display == 2 ) {
$ranking = Football_Pool_Utils::get_fp_option( 'show_ranking', FOOTBALLPOOL_RANKING_DEFAULT );
} else {
$ranking = FOOTBALLPOOL_RANKING_DEFAULT;
}
$user_defined_rankings = $pool->get_rankings( 'user defined' );
if ( $pool->has_leagues || ( $ranking_display == 1 && count( $user_defined_rankings ) > 0 ) ) {
$output .= sprintf( '<form action="%s" method="post"><div style="margin-bottom: 1em;">'
, get_page_link()
);
if ( $pool->has_leagues ) {
$output .= sprintf( '%s: %s',
__( 'Choose league', FOOTBALLPOOL_TEXT_DOMAIN ),
$pool->league_filter( $league )
);
}
if ( $ranking_display == 1 && count( $user_defined_rankings ) > 0 ) {
$options = array();
$options[FOOTBALLPOOL_RANKING_DEFAULT] = '';
foreach( $user_defined_rankings as $user_defined_ranking ) {
$options[$user_defined_ranking['id']] = $user_defined_ranking['name'];
}
$output .= sprintf( '<br />%s: %s'
, __( 'Choose ranking', FOOTBALLPOOL_TEXT_DOMAIN )
, Football_Pool_Utils::select(
'ranking', $options, $ranking )
);
}
$output .= sprintf( '<input type="submit" name="_submit" value="%s" />'
, __( 'go', FOOTBALLPOOL_TEXT_DOMAIN )
);
$output .= '</div></form>';
}
$output .= $pool->print_pool_ranking( $league, $current_user->ID, $ranking );
return $output;
}
}
In another class I found a function that is printing the ranking:
public function print_pool_ranking( $league, $user, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT ) {
$output = '';
$rows = $this->get_pool_ranking( $league, $ranking_id );
$ranking = $users = array();
if ( count( $rows ) > 0 ) {
// there are results in the database, so get the ranking
foreach ( $rows as $row ) {
$ranking[] = $row;
$users[] = $row['user_id'];
}
} else {
// no results, show a list of users
$rows = $this->get_users( $league );
if ( count( $rows ) > 0 ) {
$output .= '<p>' . __( 'No results yet. Below is a list of all users.', FOOTBALLPOOL_TEXT_DOMAIN ) . '</p>';
foreach ( $rows as $row ) {
$ranking[] = $row;
$users[] = $row['user_id'];
}
} else {
$output .= '<p>'. __( 'No users have registered for this pool (yet).', FOOTBALLPOOL_TEXT_DOMAIN ) . '</p>';
}
}
if ( count( $ranking ) > 0 ) {
// get number of predictions per user if option is set
$show_num_predictions = ( Football_Pool_Utils::get_fp_option( 'show_num_predictions_in_ranking' ) == 1 );
if ( $show_num_predictions ) {
$predictions = $this->get_prediction_count_per_user( $users, $ranking_id );
}
$userpage = Football_Pool::get_page_link( 'user' );
$all_user_view = ( $league == FOOTBALLPOOL_LEAGUE_ALL && $this->has_leagues );
$i = 1;
$output .= '<table class="standen">';
if ( $show_num_predictions ) {
$output .= sprintf( '<tr>
<th>positie</th>
<th>%s</th>
<th>%s</th>
<th>%s</th>
%s</tr>'
, __( 'user', FOOTBALLPOOL_TEXT_DOMAIN )
, __( 'predictions', FOOTBALLPOOL_TEXT_DOMAIN )
, __( 'points', FOOTBALLPOOL_TEXT_DOMAIN )
, ( $all_user_view ? '<th></th>' : '' )
);
}
foreach ( $ranking as $row ) {
$class = ( $i % 2 != 0 ? 'even' : 'odd' );
if ( $all_user_view ) $class .= ' league-' . $row['league_id'];
if ( $row['user_id'] == $user ) $class .= ' currentuser';
if ( $show_num_predictions ) {
if ( array_key_exists( $row['user_id'], $predictions ) ) {
$num_predictions = $predictions[$row['user_id']];
} else {
$num_predictions = 0;
}
$num_predictions = sprintf( '<td>%d</td>', $num_predictions );
} else {
$num_predictions = '';
}
$output .= sprintf( '<tr class="%s"><td style="width:100px; text-align: center;">%d.</td>
<td>%s%s%s</td>
%s<td>%d</td>%s
</tr>',
$class,
$i++,
esc_url( add_query_arg( array( 'user' => $row['user_id'] ), $userpage ) ),
$this->get_avatar( $row['user_id'], 'medium' ),
$row['user_name'],
Football_Pool::user_name( $row['user_id'], 'label' ),
$num_predictions,
$row['points'],
( $all_user_view ? $this->league_image( $row['league_id'] ) : '' )
);
$output .= "\n";
}
$output .= '</table>';
}
return $output;
}
I know this is a lot of info and probably nobody can do something with this information but if somebody can help me that would be great.
I think you should work with this plugin:
it must save your time, and decrease complexity of your project.
Page Navigation
Thanks

Categories