Asked  7 Months ago    Answers:  5   Viewed   84 times

As it says in the title, I'm looking for multiple excerpt lengths in WordPress.

I understand you can do this in functions.php:

function twentyten_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );

What I want to know is how you can have multiple of these each returning different numerical values so I can get short excerpts for sidebar loops, longer excerpts for featured loops, and the longest excerpt for the main article.

Something like using these in the templates:

<?php the_excerpt('length-short') ?>
<?php the_excerpt('length-medium') ?>
<?php the_excerpt('length-long') ?>

Cheers, Dave

 Answers

62

How about...

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);

      if (count($excerpt) >= $limit) {
          array_pop($excerpt);
          $excerpt = implode(" ", $excerpt) . '...';
      } else {
          $excerpt = implode(" ", $excerpt);
      }

      $excerpt = preg_replace('`[[^]]*]`', '', $excerpt);

      return $excerpt;
}

function content($limit) {
    $content = explode(' ', get_the_content(), $limit);

    if (count($content) >= $limit) {
        array_pop($content);
        $content = implode(" ", $content) . '...';
    } else {
        $content = implode(" ", $content);
    }

    $content = preg_replace('/[.+]/','', $content);
    $content = apply_filters('the_content', $content); 
    $content = str_replace(']]>', ']]&gt;', $content);

    return $content;
}

then in your template code you just use..

<?php echo excerpt(25); ?>

from: http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/

Wednesday, March 31, 2021
 
leebriggs
answered 7 Months ago
56

Try this in your functions.php:

add_action('init', function()
{
    add_rewrite_rule('^property-([^/]+)/([^/]+)/?$', 'index.php?post_type=property&name=$matches[1]', 'top');
}, 0, 0);

See add_rewrite_rule() for more info.

Friday, May 28, 2021
 
Jubair
answered 5 Months ago
48

This can be easily achieved using the Multiple Registration Forms module from Profile Builder plugin.

It's shortcode based and allows you to set up multiple registration forms with different profile fields for different user roles.

Here's a step by step tutorial on how to set up multiple registration forms.

For creating different user roles, you can use a free membership plugin like Members. These two recommended plugins have been tested and play really well together.

Saturday, May 29, 2021
 
John_BSDthos
answered 5 Months ago
16

When you use wordpress / (ACF) functions, always check if they display or return the value.

Function, which displays the value:

function displayX(){
    echo "data";
}

If you want to call this function, you won't need a echo to display the data, just call it, e.g.

displayX();  //output: data

Note: The function, won't return the data. But even if it doesn't have an explicit return statement and also won't return the data, it still will return something (NULL).

Function, which returns the value:

function returnX(){
    return "data";
}

If you want to call this function, you will need a echo to display the data, just call it, e.g.

echo returnX();  //output: data

Note: This function will return the data and doesn't display it by its own.

Different behaviour

You will notice some differences, when you use functions which display or return a value.

  1. Assignment

    1.1 Function, which displays the value:

    $variable = displayX();
    

    Note: $variable, will be assigned with NULL and the line above will output data.

    1.2 Function, which returns the value:

    $variable = returnX();
    

    Note: $variable, will be assigned with data and the line above won't output anything.

  2. Concatenation

    2.1 Function, which displays the value:

    echo "string start " . displayX() . " string end";
    

    Note: You will concatenate NULL here, since this function will return this value. The function will display data first, before you see the concatenated string.

    2.2 Function, which returns the value:

    $variable = returnX();
    

    Note: You will concatenate data here, since this function will return this value. The function won't display anything first, before you see the concatenated string.

  3. Printing

    3.1 Function, which displays the value:

    displayX();
    

    Note: This code will output data.

    3.2 Function, which returns the value:

    returnX();
    

    Note: This code won't display anything.


So in your current example you use the_field(), which displays the data. But if you want to concatenate it, you will need the data returned, means use get_filed(), which simply will return the data.

There is also a easy way to check what a function returns. Just do: var_dump(functionCall()); and you will see what the function returns.

Saturday, May 29, 2021
 
nighter
answered 5 Months ago
18

Your code is actually very close.

If you take a look at this list of all of your action hooks, you'll see that the send_headers action occurs before the Wordpress object is completely set up.

What this means is that the usual objects and functions that reference the Wordpress globals won't work at this point in the lifecycle. You actually have to hook into the core a little bit later so that you can retrieve Post-related data.

I'm not sure why you have the is_admin() || check prefixing your action hook. add_action returns a call to add_filter (Source), which in turn returns a boolean value of true (Source).

Short-circuiting wouldn't be of any use to you here, so I've modified your code to the following:

add_action('wp', function(){
    global $post;
    $title = get_the_title($post->ID);
    header('X-IC-Title:' . $title);
}, 1);

I've tested this in a clean Bedrock installation on a Homestead server and I'm seeing the new header in my network output(screenshot attached).

Bedrock Test

Hopefully this helps!

Saturday, May 29, 2021
 
Eddas
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :