• I’m making an WP theme, but I have a little problem. I want the length of excerpt (in carachters) to be adaptive to the screen width.

    if screen width is bigger then 1000px, 20 characters
    between 1000 and 700 it should be 5
    between 700 and 640px, 20 chars
    between 640 and 480, 5 chars
    below 480px, 20 chars.
    Hope that this makes sense. ??

    I tried creating something in my functions.php. I used methods as:

    function new_excerpt_length($length) {

    if ((screen.width > 1024))
    {
    return 5;
    }
    else
    {
    return 15;
    }
    }
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

    But that didn’t work, it outputs the same number. CSS isn’t a solution, has to be done in JS, in the functions.php file (no jQuery).

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m sorry no one responded to your question. I hope you’ve figured this out by now, and if so, it would very helpful if you posted a solution here or linked to a resolved thread. TIA.

    Otherwise, you cannot place JS in functions.php, it’s for PHP only. The closest you can come is enqueueing your JS with PHP. This isn’t a good solution for PHP because it cannot easily determine the display width. That is the realm of JS.

    I’d suggest enqueueing a JS function that grabs the inner HTML from excerpt containers and reduces the content as required. jQuery would make this much easier, there’s little reason to avoid it, it can be quickly loaded from the local WP installation.

Viewing 1 replies (of 1 total)
  • The topic ‘Adapt lenght of wordpress excerpt to screen width’ is closed to new replies.