985Customising Pagination in WordPress

Getting and Customising Pagination:

function getPagination() {
  global $wp_query;
  $a = paginate_links(array(
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages,
    'type' => 'array',
    'aria_current' => false,
    'show_all' => true,
    'end_size' => 2,
    'mid_size' => 2,
    'prev_next' => true,
    'prev_text' => '<div class="pg_arrow left"></div>',
    'next_text' => '<div class="pg_arrow right"></div>',
  ));
  if (empty($a)) return false;
  foreach($a as &$l) {
    $l = str_replace('prev page-numbers', 'pg_item', $l);
    $l = str_replace('next page-numbers', 'pg_item', $l);
    $l = str_replace('page-numbers', 'pg_item pg_num', $l);
    $l = str_replace(' aria-current=""', '', $l);
    $l = str_replace('span', 'a', $l);
    $l = str_replace('current', 'active', $l);
  }
  return $a;
}

Calling:

if spaces are needed in front

foreach(getPagination() as $p) {
  echo "   " . $p . "\n";
}

no spaces

echo implode("\n", getPagination());

984WordPress – Getting $post from blog home

Problem:

ACFs in sidebar-top.php in home.php are not displaying. $post is set to the first Post, rather than the home.php page.

Solution:

Check is is_home(), assign $post to get_option('page_for_posts').

if (is_home()) {
  $post = get_option('page_for_posts');
}

953Changing Languages in WordPress

Install additional language: Change user language:

921Forcing a Database Upgrade in ACF/WordPress

Background:

A site that was developed some years ago with WordPress 4.x and ACF 4.x got moved to a new server. During the migration process, WordPress and the Plug-ins were updated to their latest version – I assume, before the MySQL DB got migrated, which let to the following complaint:

“ACF are not showing in ACF 5.x”

After some detective work, I realise that the data was still present. ACF v4.x was storing the ACF data in wp_options, ACF v5.x is storing it in wp_termmeta.

Which is great, because all that is needed, is to update the DB! But how to do it?

First attempt: Re-install ACF 4.4.12, then update to ACF 5.x

Didn’t work as expected.

Second attempt: change the acf_version in wp_options

In the wp_options, the acf_version is set to the current WP version, 5.7.6 in this case. Setting it to 4.4.12, triggered the ACF DB update script, the ACF data got migrated to wp_termmeta, the site is working again as expected.

Shout out to the ACF Support Pages for solving the last piece of the puzzle.

914Show WordPress Theme Menu

$l = get_nav_menu_locations(); $m = wp_get_nav_menu_items($l['footer-menu']); foreach($m as $i) { // $i->url; // $i->title; }

913WordPress functions.php

// update_option('siteurl','http://127.0.0.1/my-site'); // update_option('home','http://127.0.0.1/my-site'); // add excerpts to pages add_post_type_support('page', 'excerpt'); // remove auto-p in blog content remove_filter('the_content', 'wpautop'); // replace \n with <br /> // substitute
for line breaks in the content function addLineBreaks($content) { return str_replace("\n", "\n<br />", $content); } add_filter('the_content', 'addLineBreaks'); // register menu function register_theme_menu() { register_nav_menu('footer-menu', 'Footer Menu'); } add_action('init', 'register_theme_menu');
Show WordPress Theme Menu

912WP Menus – Theme Setup & Menu as Array

functions.php function register_theme_menu() { register_nav_menu('my-menu', 'My Menu'); } add_action( 'init', 'register_theme_menu' ); Menu Location $l = get_nav_menu_locations(); $m = wp_get_nav_menu_items($l['my-menu']); foreach($m as $i) { // custom HTML echo $i->url; echo $i->title; }

894How to get Parent ID from a Post Page

if ($wp_query->is_posts_page) { $id = $wp_query->queried_object->ID; }

869Pagination in Custom Queries need $paged

...$nr // number of posts $args = array( 'posts_per_page' => $nr ? $nr : get_option('posts_per_page'), 'paged' => get_query_var('paged', 1), 'post_type' => 'post', ... ); $wp_query = new WP_Query($args); while ($wp_query->have_posts()) : $wp_query->the_post(); // display endwhile; // getPagination()

868Removing Categories on save_post

function onSavePosts($post_id) { if (wp_is_post_revision($post_id)) return; // Remove Post Categories, set post categories to empty array wp_set_post_terms($post_id, array(), 'category'); } add_action('save_post', 'onSavePosts');