WordPress child categories not displaying - wordpress

If trying to get Wordpress to pull through a list of child titles based on the parent but all its pulls through is just one or two titles. Any suggestions?
Code below
function get_categories_list() {
$parent = $child = array();
foreach (get_the_category(get_the_id()) as $cat) {
if ($cat->id === 0) {
continue;
}
if ($cat->category_parent === 0) {
array_push($parent,$cat);
} else {
array_push($child,$cat);
}
}
foreach ($parent as $key => $cat) {
if ($key !== 0) {
echo "-";
}
echo ' ' . $cat->name . '';
}
foreach ($child as $cat) {
if ($key !== 0) {
echo "-";
}
echo ' ' . $cat->name . '';
}
}

foreach ($child as $cat) {
if ($key !== 0) {
echo "-";
}
echo ' ' . $cat->name . '';
}
Should be
foreach ($child as $key => $cat) {
if ($key !== 0) {
echo "-";
}
echo ' ' . $cat->name . '';
}

Related

Gallery metabox doesn´t show new images / save changes since Wordpress 6.0

I use the code below to display images in posts. It basically works, but changes made via "Add Image" are no longer saved since WP 6.0 or 6.1.
The manual variant via the "post_banner_image" field below by entering the image ID still works.
here a front-end screenshot
<?php
// Add Meta Box to post
add_action( 'add_meta_boxes', 'multi_media_uploader_meta_box' );
function multi_media_uploader_meta_box() {
add_meta_box( 'my-post-box', 'Media Field', 'multi_media_uploader_meta_box_func', 'post', 'normal', 'high' );
}
function multi_media_uploader_meta_box_func($post) {
$banner_img = get_post_meta($post->ID,'post_banner_img', true);
?>
<style type="text/css">
.multi-upload-medias ul li .delete-img { position: absolute; right: 3px; top: 2px; background: aliceblue; border-radius: 50%; cursor: pointer; font-size: 14px; line-height: 20px; color: red; }
.multi-upload-medias ul li { width: 120px; display: inline-block; vertical-align: middle; margin: 5px; position: relative; }
.multi-upload-medias ul li img { width: 100%; }
</style>
<table cellspacing="10" cellpadding="10">
<tr>
<td>Banner Image</td>
<td>
<?php echo multi_media_uploader_field( 'post_banner_img', $banner_img ); ?>
</td>
</tr>
</table>
<script type="text/javascript">
jQuery(function($) {
$('body').on('click', '.wc_multi_upload_image_button', function(e) {
e.preventDefault();
var button = $(this),
custom_uploader = wp.media({
title: 'Insert image',
button: { text: 'Use this image' },
multiple: true
}).on('select', function() {
var attech_ids = '';
attachments
var attachments = custom_uploader.state().get('selection'),
attachment_ids = new Array(),
i = 0;
attachments.each(function(attachment) {
attachment_ids[i] = attachment['id'];
attech_ids += ',' + attachment['id'];
if (attachment.attributes.type == 'image') {
$(button).siblings('ul').append('<li data-attechment-id="' + attachment['id'] + '"><img class="true_pre_image" src="' + attachment.attributes.url + '" /><i class=" dashicons dashicons-no delete-img"></i></li>');
} else {
$(button).siblings('ul').append('<li data-attechment-id="' + attachment['id'] + '"><img class="true_pre_image" src="' + attachment.attributes.icon + '" /><i class=" dashicons dashicons-no delete-img"></i></li>');
}
i++;
});
var ids = $(button).siblings('.attechments-ids').attr('value');
if (ids) {
var ids = ids + attech_ids;
$(button).siblings('.attechments-ids').attr('value', ids);
} else {
$(button).siblings('.attechments-ids').attr('value', attachment_ids);
}
$(button).siblings('.wc_multi_remove_image_button').show();
})
.open();
});
$('body').on('click', '.wc_multi_remove_image_button', function() {
$(this).hide().prev().val('').prev().addClass('button').html('Add Media');
$(this).parent().find('ul').empty();
return false;
});
});
jQuery(document).ready(function() {
jQuery(document).on('click', '.multi-upload-medias ul li i.delete-img', function() {
var ids = [];
var this_c = jQuery(this);
jQuery(this).parent().remove();
jQuery('.multi-upload-medias ul li').each(function() {
ids.push(jQuery(this).attr('data-attechment-id'));
});
jQuery('.multi-upload-medias').find('input[type="hidden"]').attr('value', ids);
});
})
</script>
<?php
}
function multi_media_uploader_field($name, $value = '') {
$image = '">Add Media';
$image_str = '';
$image_size = 'full';
$display = 'none';
$value = explode(',', $value);
if (!empty($value)) {
foreach ($value as $values) {
if ($image_attributes = wp_get_attachment_image_src($values, $image_size)) {
$image_str .= '<li data-attechment-id=' . $values . '><img src="' . $image_attributes[0] . '" /><i class="dashicons dashicons-no delete-img"></i></li>';
}
}
}
if($image_str){
$display = 'inline-block';
}
return '<div class="multi-upload-medias"><ul>' . $image_str . '</ul>Remove media</div>';
}
// Save Meta Box values.
add_action( 'save_post', 'wc_meta_box_save' );
function wc_meta_box_save( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if( !current_user_can( 'edit_post' ) ){
return;
}
if( isset( $_POST['post_banner_img'] ) ){
update_post_meta( $post_id, 'post_banner_img', $_POST['post_banner_img'] );
}
}
?>
Can someone help me to get the other part working again?
current_user_can('edit_post') is no longer valid in WordPress 6.1. Here's the relevant core trac ticket.
You must now use one of these formats:
current_user_can( 'edit_posts' );
current_user_can( 'edit_post', $post->ID );
current_user_can( 'edit_post_meta', $post->ID, $meta_key );

Repeater Fields in Widget Form Function in Wordpress

I've been trying to add repeater fields in my wordpress widget form. I found two plugin examples but they don't work anymore. When I click on "Add row", nothing happens.
https://www.commoninja.com/discover/wordpress/plugin/social-repeater-widget
https://www.zealousweb.com/wordpress-plugins/repeater-entries-widget/
I noticed both use jquery in their widgets which I guess doesn't work anymore in the newer Wordpress versions?
/**
* Back-end widget form.
*
* #see WP_Widget::form()
*
* #param array $instance Previously saved values from database.
*/
public function form($instance)
{
$max_entries = get_option( 'max_entries' );
$max_entries = (empty($max_entries)) ? '5' : $max_entries;
$widget_add_id = $this->id . "-add";
$title = !empty($instance['title']) ? $instance['title'] : __('Social Repeater Widget', 'srw-widget');
$srw_html = '<p>';
$srw_html .= '<label for="'.$this->get_field_id('title').'"> '. __( 'Widget Title', 'srw-widget' ) .' :</label>';
$srw_html .= '<input id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" />';
$srw_html .= '<div class="'.$widget_add_id.'-input-containers"><div id="entries">';
for( $i =0; $i<$max_entries; $i++){
if(isset($instance['block-' . $i]) || isset($instance['social_platform-' . $i]))
{
$srw_tab_title = !empty($instance['social_platform-' . $i]) ? $instance['social_platform-' . $i] : __( 'Add Social Profile Details', 'srw-widget' );
$display = (!isset($instance['block-' . $i]) || ($instance['block-' . $i] == "")) ? 'style="display:none;"' : '';
$srw_html .= '<div id="entry'.($i+1).'" '.$display.' class="entrys"><span class="entry-title" onclick = "slider(this);"> '.$srw_tab_title.' </span>';
$srw_html .= '<div class="entry-desc cf">';
$srw_html .= '<input id="'.$this->get_field_id('block-' . $i ).'" name="'.$this->get_field_name('block-' . $i ).'" type="hidden" value="'.$instance['block-' . $i].'">';
$social_platform = esc_attr( $instance['social_platform-' . $i] );
$social_platform_link = esc_attr( $instance['social_platform_link-' . $i] );
$srw_html .= '<p class="last desc">';
$srw_html .= '<label for="'.$this->get_field_id('social_platform-' . $i).'"> '. __( 'Social Platform', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform-' . $i).'" name="'.$this->get_field_name('social_platform-' . $i).'" type="text" value="'.$social_platform.'" placeholder="'.__( 'Enter Social Platform name', 'srw-widget' ).'" />';
$srw_html .= '</p><p>';
$srw_html .= '<label for="'.$this->get_field_id('social_platform_link-' . $i).'"> '. __('Social platform Link', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform_link-' . $i).'" name="'.$this->get_field_name('social_platform_link-' . $i).'" type="url" value="'.$social_platform_link.'" placeholder="'.__( 'Enter Social Platform Link', 'srw-widget' ).'"/>';
$srw_html .= '</p>';
/* end wrapper with delete entry option */
$srw_html .= '<p><span class="delete-row">'. __( 'Delete Row', 'srw-widget' ) .'</span></p>';
$srw_html .= '</div></div>';
}
}
$srw_html .= '</div></div>';
$srw_html .= '<div id="message">'. __( 'Sorry, you reached to the limit of','srw-widget') .' "'.$max_entries.'" '. __( 'maximum entries', 'srw-widget' ) .'.</div>' ;
$srw_html .= '<div class="'.$widget_add_id.'" style="display:none;">' . __('Add New Platform', 'srw-widget') . '</div>';
?>
<script>
jQuery(document).ready(function(e) {
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).show();
}
});
jQuery(".<?php echo $widget_add_id; ?>" ).bind('click', function(e) {
var rows = 0;
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() == ''){
jQuery(this).find(".entry-title").addClass("active");
jQuery(this).find(".entry-desc").slideDown();
jQuery(this).find('input').first().val('0');
jQuery(this).show();
return false;
}
else{
rows++;
jQuery(this).show();
jQuery(this).find(".entry-title").removeClass("active");
jQuery(this).find(".entry-desc").slideUp();
}
});
if(rows == '<?php echo $max_entries;?>')
{
jQuery("#rew_container #message").show();
}
});
jQuery(".delete-row" ).bind('click', function(e) {
var count = 1;
var current = jQuery(this).closest('.entrys').attr('id');
jQuery.each(jQuery("#entries #"+current+" .entry-desc").children(), function(){
jQuery(this).val('');
});
jQuery.each(jQuery("#entries #"+current+" .entry-desc p").children(), function(){
jQuery(this).val('');
});
jQuery('#entries #'+current+" .entry-title").removeClass('active');
jQuery('#entries #'+current+" .entry-desc").hide();
jQuery('#entries #'+current).remove();
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).find('input').first().val(count);
}
count++;
});
});
});
</script>
<style>
.cf:before, .cf:after { content: ""; display: table; }
.cf:after { clear: both; }
.cf { zoom: 1; }
.clear { clear: both; }
.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block;}
#rew_container input,select,textarea{ float: right;width: 60%;}
#rew_container label{width:40%;}
<?php echo '.'.$widget_add_id; ?>{
background: #ccc none repeat scroll 0 0;font-weight: bold;margin: 20px 0px 9px;padding: 6px;text-align: center;display:block !important; cursor:pointer;
}
.desc{height:55px;}
#entries{ padding:10px 0 0;}
#entries .entrys{ padding:0; border:1px solid #e5e5e5; margin:10px 0 0; clear:both;}
#entries .entrys:first-child{ margin:0;}
#entries .delete-row{margin-top:20px;float:right;text-decoration: underline;color:red;}
#entries .entry-title{ display:block; font-size:14px; line-height:18px; font-weight:600; background:#f1f1f1; padding:7px 5px; position:relative;}
#entries .entry-title:after{ content: '\f140'; font: 400 20px/1 dashicons; position:absolute; right:10px; top:6px; color:#a0a5aa;}
#entries .entry-title.active:after{ content: '\f142';}
#entries .entry-desc{ display:none; padding:0 10px 10px; border-top:1px solid #e5e5e5;}
#rew_container #entries p.last label{ white-space: pre-line; float:left; width:39%;}
#message{padding:6px;display:none;color:red;font-weight:bold;}
</style>
<div id="rew_container">
<?php echo $srw_html;?>
</div>
<?php
}
I'm pretty new to php and wordpress so any help would be appreciated.
Please try the below code to create a repeater field in the widget.
dd_action( 'widgets_init', 'services_widget' );
function services_widget() {
register_widget( 'services_widget' );
}
class services_widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'services_widget',
'description' => 'Add a service description.'
);
$control_ops = array( 'width' => 400, 'height' => 350 );
parent::__construct( 'services_widget', 'Services', $widget_ops, $control_ops );
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if( have_rows('service', 'widget_' . $args['widget_id']) ):
echo '<ul>';
while ( have_rows('service', 'widget_' . $args['widget_id']) ) : the_row();
echo '<li class="service one-half">';
$title = get_sub_field( 'title', 'widget_' . $args['widget_id'] );
$body = get_sub_field( 'body', 'widget_' . $args['widget_id']);
$button = get_sub_field( 'button', 'widget_' . $args['widget_id'] );
$button_link = get_sub_field( 'button_link', 'widget_' . $args['widget_id'] );
if( $title ) {
echo '<h4>' . $title . '</h4>';
}
if( $body ) {
echo '<p>' . $body . '</p>';
}
if( $body ) {
echo '<a class="more" href="' . $button_link . '">' .$button . '</a>';
}
echo '</li><div class="clearfix"></div></ul>';
endwhile;
endif;
echo $args['after_widget'];
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
$title = sanitize_text_field( $instance['title'] );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
$instance['filter'] = ! empty( $new_instance['filter'] );
return $instance;
}
}

Force Woocommerce filter by attribute to show only attributes where search term is in title

I'm using the below code to make wordpress search only by product titles. However, the woocommerce "Filter by Attributes" widget still shows filters as if the search was done on body text too.
So for example if I search "face" the results that show up all have "face" in the title but the Filter By Attribute widget will still show Chocolate as an attribute because there is a chocolate bar that has a description "Face it, we all love chocolate"
Then if you click the "Chocolate" filter, it will show no results, because the search is only showing results with "face" in the title.
I don't even know where to begin looking to change the behaviour of the filter by attributes widget.
// Search titles only
function __search_by_title_only( $search, $wp_query )
{
global $wpdb;
if(empty($search)) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = !empty($q['exact']) ? '' : '%';
$search =
$searchand = '';
foreach ((array)$q['search_terms'] as $term) {
$term = esc_sql($wpdb->esc_like($term));
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if (!empty($search)) {
$search = " AND ({$search}) ";
if (!is_user_logged_in())
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);
I've cobbled together my own filter that plays nice with the above search (only the titles) and doesn't show any Brands that have items that don't have the keyword in the title. My only issue now is figuring out how to get the count of items.
<?php
$searchterm = htmlentities($_GET["s"]);
$filter_brand = htmlentities($_GET["filter_brand"]);
$current = $_SERVER['REQUEST_URI'];
$site_url = home_url();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
's' => $searchterm
);
// The Query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) :
echo'<div id="woocommerce_layered_nav-3" class="widget woocommerce widget_layered_nav woocommerce-widget-layered-nav">
<ul class="woocommerce-widget-layered-nav-list">';
$unique_singler = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
$brand_terms = get_the_terms( $post, 'pa_brand' );
$singler = array_values($brand_terms)[0];
$name = $singler->name;
$name_string = strtolower($name);
$name_slug = str_replace(" ","-",$name_string);
// only create option if city hasn't been added yet
if( ! in_array( $name, $unique_singler ) ) :
// add city to array so it doesn't repeat
$unique_singler[] = $name;
;?>
<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term <?php if (!($filter_brand == $name_slug) && !empty($filter_brand)) { echo 'hidden'; }?>">
<?php echo '<a class="remover ';
if ($filter_brand == $name_slug) { echo 'shown'; }
echo '" href="'.$site_url.'/?s='.$searchterm.'&tags=1&ixwps=1&post_type=product"><i class="fas fa-times"></i></a>';
?><?php
echo '<a class="dup" href="'.$current.'&filter_brand='.$name_slug.'">'.$name.'</a> <span class="count">(' .count($XXXXXXX). ')</span>';
?></li>
<?php
endif;?>
<?php endwhile;?>
</ul>
</div>
<?php else :?>
<p>Nothing to see</p>
<?php endif; ?>
<style>
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover {
display:none;
}
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover.shown {
display:inline-block;
}
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term.hidden
{
display:none;
}
.x-sidebar .widget ul li {
border-top: 1px solid rgba(0,0,0,0.085) !important;
}
ul.woocommerce-widget-layered-nav-list
{
border-top: 1px solid rgba(0,0,0,0.085) !important;
border-bottom: 1px solid rgba(0,0,0,0.085) !important;
}
.woocommerce-widget-layered-nav-list, li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term {
margin-left:0 !important;
}
.x-sidebar .widget ul li a {
padding-top: 8px;
padding-bottom: 8px;
}
div#woocommerce_layered_nav-3 {
margin: 0;
}
a.remover.shown i {
text-decoration: underline !important;
margin-right:0.5em;
}
</style>

How to create pagination in wordpress

I am using wordpress and wants to add pagination to posts . code is ok bot ot is not working in chrome.When i am clicking on the pagination it goes to the current page and if i click on the next and previous link it goes to home url But in mozila and other browsers it is working fine . my code is
<?php
if ( !function_exists( 'appcara_pagination' ) ) {
function appcara_pagination($total) {
$prev_arrow = is_rtl() ? 'next' : 'prev';
$next_arrow = is_rtl() ? 'prev' : 'next';
global $wp_query;
//$total = $wp_query->max_num_pages;
//$total="10";
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = '/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
function appcara_paginate_links( $args = '' ) {
$defaults = array(
'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
'total' => 1,
'current' => 0,
'show_all' => false,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'end_size' => 1,
'mid_size' => 2,
'type' => 'plain',
'add_args' => false, // array of query args to add
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
$args = wp_parse_args( $args, $defaults );
extract($args, EXTR_SKIP);
// Who knows what else people pass in $args
$total = (int) $total;
if ( $total < 2 )
return;
$current = (int) $current;
$end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default.
$mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
$add_args = is_array($add_args) ? $add_args : false;
$r = '';
$page_links = array();
$n = 0;
$dots = false;
$prev_link = '<p class="prev-div" ><span class="prev-click"></span></p>';
if ( $prev_next && $current && 1 < $current ) :
$link = str_replace('%_%', 2 == $current ? '' : $format, $base);
$link = str_replace('%#%', $current - 1, $link);
if ( $add_args )
$link = add_query_arg( $add_args, $link );
$link .= $add_fragment;
/**
* Filter the paginated links for the given archive pages.
*
* #since 3.0.0
*
* #param string $link The paginated link URL.
*/
$prev_link = '<a class="prev-div" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '"><span class="prev-click"></span></a>';
endif;
for ( $n = 1; $n <= $total; $n++ ) :
if ( $n == $current ) :
$page_links[] = "<h3 class='page-numbers'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</h3>";
$dots = true;
else :
if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
$link = str_replace('%_%', 1 == $n ? '' : $format, $base);
$link = str_replace('%#%', $n, $link);
if ( $add_args )
$link = add_query_arg( $add_args, $link );
$link .= $add_fragment;
/** This filter is documented in wp-includes/general-template.php */
$page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</a>";
$dots = true;
elseif ( $dots && !$show_all ) :
$page_links[] = '<span class="page-numbers dots">' . __( '…' ) . '</span>';
$dots = false;
endif;
endif;
endfor;
$next_link = '<p class="next-div" ><span class="next-click"></span></p>';
if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
$link = str_replace('%_%', $format, $base);
$link = str_replace('%#%', $current + 1, $link);
if ( $add_args )
$link = add_query_arg( $add_args, $link );
$link .= $add_fragment;
/** This filter is documented in wp-includes/general-template.php */
//$page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
$next_link = '<a class="next-div" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '"><span class="next-click"></span></a>';
endif;
switch ( $type ) :
case 'array' :
return $page_links;
break;
case 'list' :
$r.='<div class="pagination">';
$r.=$prev_link;
$r .= "<ul class='page-numbers'>\n\t<li>";
$r .= join("</li>\n\t<li>", $page_links);
$r .= "</li>\n</ul>\n";
$r.=$next_link;
$r.='<div class="clear"></div></div>';
break;
default :
$r = join("\n", $page_links);
break;
endswitch;
return $r;
}
?>
calling this function
<?php
//WordPress loop for custom post type
$url=explode('/',$_SERVER['REQUEST_URI']);
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$my_query = new WP_Query('post_type=blogs&posts_per_page=10&paged='.$url['4']);
$total=wp_count_posts('blogs');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div id="blogs">
<div class="content_blog custom_post" >
<h2><?php the_title(); ?></h2>
<i><?php echo get_the_date('', $post->ID).'-'.get_the_time('', $post->ID); ?></i>
<?php the_excerpt();
$term_list = wp_get_post_terms($post->ID, 'blog-cat');
echo "categories:-";
foreach($term_list as $key=>$cat)
{
echo "<a href='".get_term_link(intval($cat->term_id),'blog-cat')."'>".$cat->name.'</a>,';
}
?>
<?php ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
<?php appcara_pagination($total->publish/10);
?>
<style>
.pagination ul { list-style: none; border-left: 1px solid #d9d6d6; border-right: 1px solid #d9d6d6; float: left; width: 88%; }
.pagination ul li { display: inline-block; }
.pagination ul li a { display: block; font-size: 16px; font-weight: 600; color: #515050; padding: 6px 2px; }
.prev-div, .next-div { text-align: center; float: left; width: 68px; }
.prev-click, .next-click { display: inline-block; width: 10px; height: 16px; margin: 9px 0 0; }
.prev-click { background-position: -280px -311px; }
.next-click { background-position: -280px -331px; }
.prev-click:hover, .prev-div:hover .prev-click { background-position: -298px -311px; }
.next-click:hover { background-position: -301px -331px; }
.pagination ul li a:hover { color: #d75c43; }
</style>
You can use this code to functions.php file it's working fine..
/*************************************
* Wordpress pagination
**************************************/
function pagination($pages = '', $range = 3)
{
$showitems = ($range * 3)+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)
{ echo "<div class='navigation'>";?>
<?php previous_posts_link();
echo "<div class='pagination'><ul>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a rel='nofollow' href='".get_pagenum_link(1)."'>« First</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a rel='nofollow' href='".get_pagenum_link($paged - 1)."' class='inactive'>‹ Previous</a></li>";
for ($i=1; $i <= $pages; $i++)
{ if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{ echo ($paged == $i)? "<li class='current'><span class='currenttext'>".$i."</span></li>":"<li><a rel='nofollow' href='".get_pagenum_link($i)."' class='inactive'>".$i."</a></li>";
} } if ($paged < $pages && $showitems < $pages) echo "<li><a rel='nofollow' href='".get_pagenum_link($paged + 1)."' class='inactive'>Next ›</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a rel='nofollow' class='inactive' href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</ul></div>";?>
<?php next_posts_link();
echo "</div>"; }
}
Use this on index file:
<?php pagination();?>

WordPress: Custom footer links doesn't show active state

Because I'm using a couple of custom post types and two languages I decided not to use WP's own menu function, and used these conditional statements instead -
<div class="navigation">
<ul>
<li><a <?php if(is_page('home') || is_page('hem')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo(url) . '">Home'; } else { echo 'href="' . get_bloginfo(url) . '">Hem'; } ?></a></li>
<li><a <?php if(is_page('offers') || is_page('erbjudanden') || in_category('offers') || in_category('erbjudanden')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'offers">Offers'; } else { echo 'href="' . get_bloginfo('url') . '/erbjudanden">Erbjudanden'; } ?></a></li>
<li><a <?php if(is_page('properties-spain') || is_page('fastigheter-spanien')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-spain">Properties in Spain'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-spanien">Fastigheter i Spanien'; } ?></a></li>
<li><a <?php if(is_page('properties-usa') || is_page('fastigheter-usa')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-usa">Properties in USA'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-usa">Fastigheter i USA'; } ?></a></li>
<li><a <?php if(is_subpage('information') || is_subpage('radgivning')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'information/process-of-purchase">Information'; } else { echo 'href="' . get_bloginfo('url') . '/radgivning/hur-ett-kop-gar-till">Rådgivning'; } ?></a></li>
<li><a <?php if(is_page('about-us') || is_page('om-oss')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'about-us">About us'; } else { echo 'href="' . get_bloginfo('url') . '/om-oss">Om oss'; } ?></a></li>
<li class="last"><a <?php if(is_page('contact') || is_page('kontakt')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'contact">Contact'; } else { echo 'href="' . get_bloginfo('url') . '/kontakt">Kontakt'; } ?></a></li>
</ul>
</div>
Now this works excellent including it in the header file but in the footer file only some of the links are given a class of active when on the actual page. All the items in the menu exists as pages, with one version for each language (swedish and english). This error just seems really weird and I've tried checking with different conditional parameters such as page title, page id etc. with no results. Any ideas?
Sometimes queries higher up in the template source will corrupt the output of conditional tags lower down (i.e. in your footer). The trick is to put wp_reset_query() before you use them in your footer include:
<?php wp_reset_query(); ?>
<div class="navigation">
<ul>
<li><a <?php if(is_page('home') || is_page('hem')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo(url) . '">Home'; } else { echo 'href="' . get_bloginfo(url) . '">Hem'; } ?></a></li>
<li><a <?php if(is_page('offers') || is_page('erbjudanden') || in_category('offers') || in_category('erbjudanden')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'offers">Offers'; } else { echo 'href="' . get_bloginfo('url') . '/erbjudanden">Erbjudanden'; } ?></a></li>
<li><a <?php if(is_page('properties-spain') || is_page('fastigheter-spanien')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-spain">Properties in Spain'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-spanien">Fastigheter i Spanien'; } ?></a></li>
<li><a <?php if(is_page('properties-usa') || is_page('fastigheter-usa')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-usa">Properties in USA'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-usa">Fastigheter i USA'; } ?></a></li>
<li><a <?php if(is_subpage('information') || is_subpage('radgivning')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'information/process-of-purchase">Information'; } else { echo 'href="' . get_bloginfo('url') . '/radgivning/hur-ett-kop-gar-till">Rådgivning'; } ?></a></li>
<li><a <?php if(is_page('about-us') || is_page('om-oss')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'about-us">About us'; } else { echo 'href="' . get_bloginfo('url') . '/om-oss">Om oss'; } ?></a></li>
<li class="last"><a <?php if(is_page('contact') || is_page('kontakt')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'contact">Contact'; } else { echo 'href="' . get_bloginfo('url') . '/kontakt">Kontakt'; } ?></a></li>
</ul>
</div>

Resources