O
O
Orkhan Hasanli2018-02-13 09:31:12
WordPress
Orkhan Hasanli, 2018-02-13 09:31:12

How to create a multilingual alphabetical pagination (glossary) for WP?

I'm trying to create a multilingual alphabetic pagination for WP. For multilingual use WPML plugin. I found a small solution on the net and decided to try it myself.
The result I got is link
But there are problems. When debugging (if you click on any letter) in the console, I get an error:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

What's my mistake? Thanks in advance)
Below is all the code that I used for this:
I connect the script through functions for specific pages
function get_ajax_url() {
    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/alphabet.js', array('jquery') );

    wp_localize_script("alphabetJS", "ajaxurl", admin_url("admin-ajax.php"));  
}
add_action( 'wp_enqueue_scripts', 'get_ajax_url' );

The contents of the alphabet.js script
jQuery(document).ready(function($) 
  {
    jQuery('.alphabet').click(function(){
      var data = {
        action: 'get_by_char',
        char: this.id
      };

      jQuery.post(ajaxurl, data, function(response) {

        $("#abc-drugs").html('');

        $.each($.parseJSON(response), function() {
          var they = this;

            $("#abc-drugs").append
            (
              '<li class="drug">' + 
                '<a href="' + they.url + '">' +
                  '<h2 class="md7_drug_title">' + they.title + '</h2>' + 
                '</a>' +
               '</li>'
            );
        });

          if(response.length < 3){
          $("#abc-drugs").html('<div class="no-drug-array">Препаратов не найдено</div>');
          };

      });
    })
  });

Next added to functions.php
function get_by_char_callback() 
{    
    global $wpdb;
    $b=array();
    $char=$_POST['char'];

    $a=$wpdb->get_results('SELECT post_title,ID FROM wpwv_posts where post_type="drug" and post_status="publish"  and (post_title like "[:ru]'.$char.'%" or post_title like "%[:en]'.$char.'%" or post_title like "'.$char.'%") ',ARRAY_A  );       

    foreach ($a as $key => $value) 
    {       

        $b[$key]['title']=translate($value['post_title']);
    }
    echo json_encode($b);
    wp_die(); 
}

add_action('wp_ajax_get_by_char', 'get_by_char_callback');
add_action('wp_ajax_nopriv_get_by_char', 'get_by_char_callback');

The code that I display in the page template:
<div class="alphabet-wrap">
                  <?php 
                    $alphabet=array(
                            'ru_RU'=>array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','э','ю','я'),
                            'en_US'=>array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'),
                            'de_DE'=>array('a','ä','b','c','d','e','f','g','h','i','j','k','l','m','n','o','ö','p','q','r','s','ß','t','u','ü','v','w','x','y','z'),

                          );
                    foreach ($alphabet[get_locale()] as $key => $value):
                  ?>
                  <span id="<?=$value?>" class="alphabet"><?=$value?></span>
                  <?php endforeach;?>
                </div>


                  <ul class="drugs" id="abc-drugs">
                    <?php $args = array(
                        'post_type' => 'drug',
                        'posts_per_page' => -1
                        );
                      $loop = new WP_Query( $args );
                      if ( $loop->have_posts() ) {
                        while ( $loop->have_posts() ) : $loop->the_post(); ?>
                          <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
                        <?php endwhile;
                      } else {
                        echo __( 'Препаратов не найдено' );
                      }
                      wp_reset_postdata();
                    ?>
                  </ul>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2018-02-15
@azerphoenix

azerphoenix , in this place
in my_ajax_object.ajaxurl
there should be a url of the form
perhaps, on another site in the code, the object my_ajax_object is declared with the ajaxurl property in which is just the path to the file/wp-admin/admin-ajax.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question