PHP code example of sitecrafting / sitka-insights-wordpress
1. Go to this page and download the library: Download sitecrafting/sitka-insights-wordpress library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sitecrafting / sitka-insights-wordpress example snippets
// NOTE: the client may throw an exception!
use Swagger\Client\ApiException;
// Call out to the API
try {
// search with some sensible defaults
$response = Sitka\search();
} catch (ApiException $e) {
error_log($e->getMessage());
$response = [];
}
wp_header();
// Render results
if (empty($response['results'])) :
use Swagger\Client\ApiException;
// Override your site's pagination settings.
$count = 25;
// Note that we can't use $paged here, because WordPress core won't
// necessarily report the same number of pages as Sitka, leading to 404s
// in cases where Sitka has more result pages than WP would.
$page_offset = ($_GET['page_num'] ?? 1) - 1;
// Call out to the API
try {
$response = Sitka\search([
// Pass the user's search term to the API.
'query' => get_query_var('my_search_param'),
// Tell the API how many results we want per page.
'resLength' => $count,
// Tell the API which page of results we want.
'resOffset' => $page_offset * $count,
// Tell the API to only return results of a certain type
'metaKey' => $_GET['my_content_type'],
]);
} catch (ApiException $e) {
error_log($e->getMessage());
$response = [];
}
// Render results
foreach (($response['results'] ?? []) as $result) :
$url_params = $data['url_params']; // this is just $_GET by default
$paginator = $data['paginator']; // A Sitka\Plugin\Paginator instance
$markers = $paginator->page_markers($url_params);
/* START MARKUP */
if ($paginator->page_count() > 1) :
add_filter('sitka/pagination/construct', function(array $ctor_args) {
return array_merge($ctor_args, [
'display_adjacent' => 3, // override the default of 2
]);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.