Category Archives: Wordpress

Debbuging WordPress

Add following rows to wp-config.php file:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
It creates and updates debug.log file in the wp-content folder.

Page template and custom header

Few quick heurekas about these topics. Page template
  • Name your file like this: my-template.php
  • At the beginning of my-template.php add:
    
    
    
    Actually it showed up in both cases Template name: My-template and Template name: my-template. It is traditionally used with capital letter, but I have to check, maybe it is a standard
Different header
  • Name your file like this: header-yourheadername.php
  • To call it out on your about page, add into template-file.php or about-template.php:
    if(is_page('about')){
        
        get_header('yourheadername');
    
    } else {
        get_header();
    }
    

Show x latest blogposts

Needed a quick solution for displaying five latest blogposts on home-page. It will show only titles and makes them active links. This is the code:
    '5' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ echo '
  • ' . $recent["post_title"].'
  • '; } wp_reset_query(); ?>
If different number of posts is needed, change the number 5 in this row:
$args = array( 'numberposts' => '5' );
Source

Get the title in default language

Sometimes we need variables to stay in a default language, e.g in English. For example, we have a multi-language site but Google Maps accepts country's name only in English. In our php-part:
if (ICL_LANGUAGE_CODE!="en"){
   $original_ID = icl_object_id( $post->ID, 'post', false, 'en' );
   $country = get_the_title( $original_ID );
   
  } else {
       $country = $country_full_name;
  }
And in our HTML-part:

Change categories’ order

Needed the categories to be in certain order - not in alphabetical and not by id. It can be done by some plugin, but here is the way also through code:
 // change order of terms
   	function gto_forced_order($orderby) {

             switch (ICL_LANGUAGE_CODE){
   	      case("de"):
    	         $orderby = "FIELD(t.term_id, 465, 468,524,525,574,575, 600, 601,1240, 775, 777, 778, 841,843, 844)";
    	      break;
   	      case("fr"):
    	         $orderby = "FIELD(t.term_id,183, 184,203,204, 218, 219, 230, 231, 1239, 294, 296, 297, 306, 308,309)";
    	      break;
   	      case("ru"):
    	        $orderby "FIELD(t.term_id,954,955,974,975,989,990,1041,1042,1241,1145,1147,1375,1184,1186,1187)";
    	      break;
              case("es"):
    	         $orderby = "FIELD(t.term_id,834, 935,994,995,1009,1010,1021,1022,1242, 1165,1167,1168,1177,1179,1180 )";
    	       break;
               case("zh-hant"):
    	           $orderby = "FIELD(t.term_id,464, 467,526,527, 569, 570,609, 610,1238,795, 797, 798, 834, 836, 837)";
    	       break;
               case("ar"):
    	         $orderby = "FIELD(t.term_id,466, 469,528,529, 579, 580,591, 592,1243,815, 817, 818, 827, 829, 830)";
    	       break;
               default: 
     	         $orderby = "FIELD(t.term_id,125,95,97,121,33,32,124,99,37,36,96,122,123,94,34)";
     	        break;
              }

 
         return $orderby;
       }
add_filter('get_terms_orderby','gto_forced_order');

//original query
$tax_terms = get_terms( $tax, $tax_args );
gto_forced_order($tax_terms);
Just change the terms' IDs. Source as usual almighty Internet.

Is it a subpage of the page id 129?

So in your functions.php file:
**
 * Check whether we are on a subpage
 *
 * @return mixed ID of the parent post or false if this is not a subpage.
 */
function wpdocs_is_subpage() {
    // Load details about this page.
    $post = get_post();
 
    // test to see if the page has a parent
    if ( is_page() && $post->post_parent ) {
        // Return the ID of the parent post.
       return $post->post_parent;
    // There is no parent so ...
    } else {
        // ... The answer to the question is false
          return false;
    }
   
}

 
And in your page.php or where ever:

 

 
Source