Provide zblog template_ Zblog topic_ Download and customization of wordpress templates

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 two hundred and sixty-nine 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

Cancel Reply welcome you Comment:

 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