Provide zblog template_zblog them_wordpress template download and customization

The number of articles displayed per page is different for different categories of wordpress

Tianxing Studio 2022-04-24 14:01 Wordpress Tutorial one thousand six hundred and eighty-four 0 Comments


Recently, I was preparing to Tianxing Studio App theme from zblog Convert to wordpress , encountered the need for different classification settings different number of articles displayed per page. After solving this problem, I wrote this tutorial to introduce the next process.

The number of articles displayed on each page of WordPress is set in the background - Settings - Reading - Blog page at most by default, but this is a public configuration, and all categories will use this number. When we need different classification settings and different number of articles to be displayed per page, we have to hang the hook of WordPress, and then call the user-defined value.

The hook to be hung is "pre_get_posts". The detailed code is as follows:

 //Different paging display quantity called by different classifications function tx_wp_filter_pre_get_posts($query) { If ($query ->is_main_query()) {//Judge whether the main query is         $num = ''; If (is_category()) {//Judge whether to classify             $cat_ID = get_queried_object_id(); //Get the classification ID If (get_option ('cat pagenum - '. $cat_ID)) {//Judge whether the category custom field has a value                 $num = get_option('cat-pagenum-' . $cat_ID);                 $query->set('posts_per_page', $num); //Assign a value to the number of articles per page in the main loop data             }         }     }     return $query; } add_action('pre_get_posts', 'tx_wp_filter_pre_get_posts');// Hang the hook

Note: The above codes need to be used together with the classification user-defined fields, which will be shared in the future.

If you don't know how to classify custom fields or just need to set a specific classification, you can use the following code to make it easier:

 //Different paging display quantity called by different classifications function tx_wp_filter_pre_get_posts($query) { If ($query ->is_main_query()) {//Judge whether the main query is         $num = ''; If (is_category (array (1)) {//The number 1 is the specified classification id             $num = 2; //The number 2 is the number of articles per page, which can be modified arbitrarily but must be an integer             $query->set('posts_per_page', $num); //Assign a value to the number of articles per page in the main loop data         }     }     return $query; } add_action('pre_get_posts', 'tx_wp_filter_pre_get_posts');// Hang the hook

Can't find a tutorial that can solve your problem?

You can try to search or ask questions directly online. We also provide charging technical support. If you need it, you can contact us online.

Online questions Online Service

welcome you Comment: Cancel Reply

 Please fill in the verification code
  • Latest articles
  • Hot article ranking
  • Most Comments
Label aggregation
  • Sign in
  • Registered account Forgot your password?
  • register
  • Already have an account? Direct login Forgot your password?
  • Social account login