How to Make Different Templates for Categories and Subcategories in WordPress

In the base template hierarchy, you can use the following category templates: category-{slug}.php category-{id}.php categories.php But, if you need to apply different php templates for categories and subcategories, you can use the category_template hook and check if the current category has a parent element, in which case load e.g. subcategory.php: If your task is to…

How to add pagination for pages or cpt using WP_Query()

WordPress is designed so that you can paginate any query for posts from the database using WP_Query(), because the necessary ‘paged’ and ‘posts_per_page’ arguments are already present in the query. By pagination we mean page navigation. For example, if we specify the output of 10 posts per page and the WP_Query() response returns information about…

How to exclude pages, categories, or author from search results in WordPress

By default, the search functionality in WordPress shows all record types and pages in the results. You may want to remove pages, categories, taxonomies, or posts by a specific author from your search. To do this, we need to fix the main is_search() query on the pre_get_posts hook. Let’s see some examples. How to hide…

How to change the number of entries on the search page in WordPress

By default, the number of entries on the search results page in WordPress is taken from the read settings in the admin. You can change this global request by using the pre_get_posts hook. In the example below, we’ll set it to display 50 entries on the search.php search results page:

How to close the Magnific Popup after submitting a form

When writing your own form of communication and using the Magnific Popup library, a situation can arise when the communication form is successfully sent, but the popup remains unclosed and disturbs the site visitor. It is much more natural if the form displays a message about successful sending and closes itself after 2-3 seconds. You…

How to disable the ability to change files in the WordPress admin

By default, the WordPress admin has the ability to modify theme files or plugins in Appearance → Editor. But there may be situations where you need to show the admin to a customer or editor. If you don’t want any of them to be able to change your files, you should add this line to…

How to add noindex to headings without posts in WordPress

You may have a situation where you have created several headings, but have not yet added posts to them. You probably don’t want empty categories without posts to be indexed by search engines. You can use wp_robots filter to add noindex to the robots meta tag:

How to change robots.txt in a WordPress multisite

WordPress dynamically creates robots.txt. To overwrite it during a normal, non-multisite installation, you can simply upload a static robots.txt file to the server. In a multi-site installation, this will overwrite the robots.txt file for all sites, which is not always the desired result. In this post, we will discuss how you can change the robots.txt…

How to reset permanent links on all sites in the WordPress network (Multisite)

Quite often we encounter a situation when something breaks the structure of permanent links on all sites in the WordPress network (Multisite installation), for example, when a plugin activation error occurs throughout the network. On one site from the network, this is easy enough to fix by manually going into Settings → Permalinks, which will…

How to include styles and scripts in a child WordPress theme?

Dozens of articles have already been written on this topic, but the topic continues to be discussed, and in chat rooms and forums, people ask questions and face problems over and over again. Let’s try to figure out how to correctly connect styles and scripts in WordPress using examples. Let’s start in order. There is…