WordPress

Filter to add new css class to li elements

Most of us are familiar with adding class from the dashboard that follows :

1. In Appearance > Menus, click the screen Option Tab

2. Under Show advanced menu properties, check CSS Classes

3. Expand any menu item to see the css class text input field

4. Enter desired class name and save menu to apply the class to the menu item

If you are searching for an option to get rid of editing each menu items to add same custom class to all in the menu items, you can go for filter

Where,

  • $classes is an array of the CSS classes that are applied to the menu item’s element,
  • $item is the current menu item,
  • $args is an object of wp_nav_menu() arguments,
  • $depth is Depth of menu item

To learn more about the filter, please can refer the codex.

Hits: 6

Standard
Programming, WordPress

Fix Fatal error: Maximum execution time of 30 seconds exceeded

In WordPress, maximum execution time fatal error is seen when trying to update theme or plugins. Usually the problem occurs when a code takes a long time to run and reaches the maximum time limit set in the hosting server.

To fix the error,

1. Find your .htaccess file in the root directory, add the below line of code at the bottom of your .htaccess file and save it. The code sets the maximum execution time to 300 secs .

After saving the .htaccess file, check if the error still exists. If this doesn’t fix the issue, check the issue by increasing the maximum execution time to 600 secs

2. Create a php.ini file inside your WordPress root folder , add the following line in the php.ini file and save it.

Check your website to see if the error has gone.

3. If both of the aforementioned methods don’t fix the issue, contact your hoster for assistance.

Hits: 14

Standard
ACF, Filter and Hooks, WordPress

Issue : Custom Field Option missing from the WordPress screen options

If you have installed ACF WordPress plugin for creating custom meta boxes, then it removes the the default custom field metaboxes . The main objective of removing the default WordPress custom field is to increase the speed of load time while editing the posts. Also, you shouldn’t need the custom fields if you have ACF plugin installed in your WordPress site. However, if you need to use default WordPress custom field metabox then you can add the following code to your WordPress theme’s functions.php file.

This will solve your  custom field Option missing issue.

Hits: 19

Standard
Filter and Hooks, Programming, WordPress

Get Menu Items Based on Custom Menu

1. Register your custom navigation menu.
We have readily available funtion to register our custom menu, therefore we do not need to call add_theme_support( ‘menus’ ) :

 

Here, $locations is an associative array of menu location slugs (key) and descriptions (according value). I have registered custom menu ‘primary_menu’ as a key  and ‘Main menu’ is the description for the registered ‘primary_menu’

 

2. Create and add Menu items the dashboard

 

3. Get the array of Menu items

Use the custom menu key ‘primary_menu’ in my case to get the  all the menu items associated with it.

 

4. Function to the get the array of menu items

You can create your own function to get the menu items and its sub-items in an array

 

Hits: 13

Standard
Programming, WordPress

Display first 8 words from the Sentence

My client asked  me to display only 8 words of the long title of the post. So, to fulfill his requirement,  I just count the number of words in  the title and if the title consist of more than 8 words, I extract 8 words from the title and displayed it , otherwise displayed the title as it is.

Steps 1 :   Create function that returns the first 8 words from the title/sentence, place it anywhere in your function.php or helpers.php files

Step 2 : Count the number of words in the title and use the function in step 1, to extract 8 words form the title. If the title has less than 8 words, do nothing just display the title.

Hits: 2

Standard