I have been building a website recently and I faced a problem with the pagination. I am getting same posts (portfolio post in this case) on each page. I've been googling around how to solve this but it seems that this problem is individual on each theme.
This is the code from the page template I'm using...
<?php if ( have_posts() ) while ( have_posts() ) : the_post();
$pag = get_post_meta( $post->ID, 'folio_pag', true );
$per = get_post_meta( $post->ID, 'folio_per', true );
$style = get_post_meta( $post->ID, 'folio_style', true );
$animation = get_post_meta( $post->ID, 'folio_anim', true );
$color = get_post_meta( $post->ID, 'folio_color', true );
$info = get_post_meta( $post->ID, 'folio_info', true );
$ratio = get_post_meta( $post->ID, 'folio_ratio', true );
$r = explode( ':', $ratio );
$cols = get_post_meta( $post->ID, 'folio_cols', true );
$c = $cols == 'two' ? 648 : ( $cols == 'three' ? 432 : 324 );
$img_factor = '0';
$cats = get_post_meta( $post->ID, 'folio_cats', true );
$query_filter = '';
$filter_output = '<li>' . __( 'All', 'krown' ) . '</li>';
$show_cats = get_post_meta( $post->ID, 'folio_filter', true ) == 'enable-filters' ? true : false;
if ( ! empty ( $cats ) ) {
if ( sizeof( $cats ) == 1 ) {
$show_cats = false;
}
foreach ( $cats as $cat ) {
$filter = get_term_by( 'id', $cat, 'portfolio_category' );
$query_filter .= $filter->slug . ', ';
$filter_output .= '<li>' . $filter->name . '</li>';
}
} else {
$cats = get_categories( array( 'taxonomy'=>'portfolio_category' ) );
foreach ( $cats as $cat ) {
$filter_output .= '<li>' . $cat->name . '</li>';
}
}
?>
<?php if ( get_post_meta( $post->ID, 'folio_content', true ) == 'content-above' ) {
the_content();
} ?>
<?php if ( $show_cats ) {
echo '<div id="filter"><ul class="clearfix">' . $filter_output . '</ul></div>';
} ?>
<div id="portfolio-holder" class="clearfix">
<ul id="portfolio" class="folio-grid cols-<?php echo $cols; ?> style-<?php echo $animation . ' ' . $color; ?> show-<?php echo $info; ?> layout-<?php echo $style; ?> clearfix">
<?php
$paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : ( get_query_var( 'page' ) ? get_query_var( 'page' ) : 1 );
$args = array(
'post_type' => 'portfolio',
'portfolio_category' => $query_filter,
'offset' => 0,
'posts_per_page' => ( $pag == 'no-pagination' ? -1 : $per ),
'paged' => $paged
);
$all_posts = new WP_Query( $args );
$page_id = $post->ID;
while ( $all_posts->have_posts() ) : $all_posts->the_post();
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_image_src( $thumb, 'full' );
if ( $thumb == '' ) {
$img_url = Array( get_template_directory_uri() . '/images/blank_2.gif' );
}
$retina = krown_retina();
$retina_thumb = get_post_meta( $post->ID, 'portfolio_retina-thumbnail_thumbnail_id', true );
if ( $style == 'fixed' ) {
switch ( $cols ) {
case 'two':
$img_width = 648;
$img_height = ceil( $img_width / $r[0] * $r[1] );
break;
case 'three':
$img_width = 432;
$img_height = ceil( $img_width / $r[0] * $r[1] );
break;
default:
$img_width = 324;
$img_height = ceil( $img_width / $r[0] * $r[1] );
break;
}
if ( $retina === 'true' && $retina_thumb != '' ) {
$retina_url = wp_get_attachment_image_src( $retina_thumb, 'full' );
$image = aq_resize( $retina_url[0], $img_width*2, $img_height*2, true, false );
} else {
$image = aq_resize( $img_url[0], $img_width, $img_height, true, false );
}
} else if ( $style == 'masonry' ) {
$img_factor = 1;
$img_width = $c;
if ( $retina === 'true' && $retina_thumb != '' ) {
$retina_url = wp_get_attachment_image_src( $retina_thumb, 'full' );
$image = aq_resize( $retina_url[0], $img_width*2, null, false, false );
} else {
$image = aq_resize( $img_url[0], $img_width, null, false, false );
}
} else if ( $style == 'masonry-advanced' ) {
$img_factor = floor( $img_url[1] / $c );
$img_width = $img_factor * $c;
if ( $retina === 'true' && $retina_thumb != '' ) {
$retina_url = wp_get_attachment_image_src( $retina_thumb, 'full' );
$image = aq_resize( $retina_url[0], $img_width*2, null, false, false );
} else {
$image = aq_resize( $img_url[0], $img_width, null, false, false );
}
}
?>
<li class="item <?php krown_categories( $post->ID, 'portfolio_category', ' ', 'slug' ); ?>" data-factor="<?php echo $img_factor; ?>">
<a href="<?php echo get_new_permalink( $page_id, $post->ID, $cats ); ?>">
<img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php the_title(); ?>" />
<div class="caption">
<div>
<div>
<h3><?php the_title(); ?></h3>
<span class="category"><?php krown_categories( $post->ID, 'portfolio_category' ); ?></span>
<span class="excerpt"><?php echo krown_excerpt( 'krown_excerptlength_post' ); ?></span>
</div>
</div>
</div>
</a>
</li>
<?php endwhile;
?>
</ul>
</div>
<?php if ( $pag == 'classic' ) {
krown_pagination( $all_posts, true );
} else if ( $pag == 'infinte-loading' ) {
echo '<div class="infinite-barrier"><span class="preloader"></span><p class="end">' . __( 'No More Projects', 'krown' ) . '</p><a id="infinite-link" href="' . next_posts( 0, false ) . '">' . __( 'Load More Projects', 'krown' ) . '</a></div>';
} ?>
<?php wp_reset_query(); ?>
<?php if ( get_post_meta( $post->ID, 'folio_content', true ) == 'content-below' ) {
the_content();
} ?>
I think the reason why the same content is showing in every other pages because you set offset to 0 in the portfolio query. You should update your offset $offset = $per * ($paged-1) so that every page change would tell WP_Query to offset right amount of posts that match current page context.
Related
so I wanted to know how, if at all, you could wrap every <li> tag of a wordpress submenu, under a <div> tag, such that the output looks something like this:
<ul class="menu">
<ul class="sub-menu ">
<div class="something"><li class="menu-item"></li></div>
<div class="something"><li class="menu-item"></li></div>
</ul>
</ul>
I'm new to wordpress so I don't know much about it, I've tried using a Walker but that just wraps around the <ul> tag, whereas I want to wrap every single <li> tag, I don't even know if it's possible, so feel free to let me know.
I hope this will help, my working walker with bootstrap 5:
class My_Walker_Menu extends Walker_Nav_Menu {
private $curr_item = null;
private $count = 0;
public function start_lvl( &$output, $depth = 0, $args = NULL ) {
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );
$display_depth = ( $depth + 1);
$is_parent = get_post_meta( $this->curr_item->db_id, '_featured_menu_meta', true );
$is_parent = rest_sanitize_boolean( $is_parent );
$menu_parents = get_option('featured_menu_parents', []);
$is_featured = in_array( $this->curr_item->menu_item_parent, $menu_parents);
$class_names = ( $display_depth >= 1 ? 'dropdown-menu' : '' );
$class_names = ($is_parent || $is_featured) && $display_depth == 1 ? 'list-unstyled mb-0' : $class_names;
$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
}
public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
global $wp_query;
$this->curr_item = $data_object;
$item = $data_object;
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );
$has_children = $args->walker->has_children;
$is_parent = get_post_meta( $item->db_id, '_featured_menu_meta', true );
$is_parent = rest_sanitize_boolean( $is_parent );
$menu_parents = get_option('featured_menu_parents', []);
$is_featured = in_array($item->menu_item_parent, $menu_parents);
// menu_item_parent
if ( $depth == 0 ) {
$item_class = $has_children ? 'nav-item dropdown' : 'nav-item';
}
if ( $depth >= 1 ) {
$item_class = $has_children ? 'dropdown' : '';
}
$item_attr = $item_class ? ' class="'. $item_class . '"' : '';
$output .= $indent . '<li'. $item_attr . '>';
// link attributes
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$attributes .= $has_children && $depth == 0 ? ' data-bs-toggle="dropdown"' : '';
if( $item->current ) {
$attr_classes = ' class="nav-link active' . ( $has_children ? ' dropdown-toggle' : '' ) . '"';
} else {
$attr_classes = ' class="nav-link'. ( $has_children ? ' dropdown-toggle' : '' ) . '"';
}
if ( $is_parent && $depth == 0 ) {
$args->after = '<div class="dropdown-menu overflow-hidden p-0" data-bs-popper="static">';
$args->after .= '<div class="d-lg-flex">';
$args->after .= '<div class="mega-dropdown-column pt-1 pt-lg-3 pb-lg-4">';
}
if ( $is_featured && $depth == 1 ) {
$thumb_url = get_the_post_thumbnail_url( $item->object_id, 'post-thumbnail' );
$thumb_url = trim($thumb_url);
$visibile = $this->count == 1 ? ' rounded-3 rounded-start-0' : ' zindex-2 opacity-0';
if( $thumb_url ) {
$args->after = '<span class="mega-dropdown-column position-absolute top-0 end-0 h-100 bg-size-cover bg-repeat-0'.$visibile.'" style="background-image: url('.$thumb_url .');">';
}
$attr_classes = ' class="dropdown-item"';
}
$attributes .= $attr_classes;
$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters( 'the_title', $item->title, $item->ID ),
$args->link_after,
$args->after
);
$args->after = '';
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
$this->count++;
}
public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
$item = $data_object;
$is_parent = get_post_meta( $item->db_id, '_featured_menu_meta', true );
$is_parent = rest_sanitize_boolean( $is_parent );
$menu_parents = get_option('featured_menu_parents', []);
$is_featured = in_array($item->menu_item_parent, $menu_parents);
if ( $is_parent && 0 == $depth ) {
$output .= '</div>';
$output .= ' <div class="mega-dropdown-column position-relative border-start zindex-3"></div>';
$output .= '</div>';
$output .= '</div>';
}
if ( $is_featured && $depth == 1 ) {
$thumb_url = get_the_post_thumbnail_url( $item->object_id, 'post-thumbnail' );
$thumb_url = trim($thumb_url);
if( $thumb_url ) {
$output .= '</span>';
}
}
$output .= "</li>\n";
}
}
wp_nav_menu([
'theme_location' => 'header_menu',
'container' => 'nav',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'navbarNav',
'menu_class' => 'navbar-nav navbar-nav-scroll me-auto',
'items_wrap' => '<ul class="%2$s">%3$s</ul>',
'depth' => 3,
'walker' => new My_Walker_Menu()
]);
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;
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;
}
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.
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