X
X
xonar2020-05-11 23:19:36
WordPress
xonar, 2020-05-11 23:19:36

How to call two different the_excerpt() in wordpress?

Hello.

There are 2 categories on the page where posts are displayed. For some posts, you need to display up to 100 characters (a short description), and in the second category, 50 characters.

For the first category without problems:

add_filter( 'excerpt_length', function(){
  return 100;
} );


But how to duplicate the_excerpt() function? All wp-kama, codex wp rummaged. Is it really impossible to display different amounts of a short description in different places?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-05-12
@xonar

Use conditional tags inside the filter and check any properties. In this case, whether the current post belongs to the specified categories:

add_filter('excerpt_length', function(){
  
  // Для постов с рубрикой 1
  if (in_category(1)) {
    return 100;
  } 

  // Для постов с рубрикой 2
  if (in_category(2)) {
    return 50;
  }
  
  // Значение по умолчанию
  return 25;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question