• So I ran into something interesting regarding the the_excerpt() function.

    I have this piece of code in my blog layout:

    
    echo '<div class="box blog-item">';
    			/* Featured Image */
    			if ( has_post_thumbnail() ) : 
    
                    woffice_render_featured_image_single_post($post->ID, "", true);
    			endif; 
    			/* Content */
    			echo '<div class="intern-padding">';
    				/* Title */
    				echo'<div class="intern-box box-title">
    					<h3><a href="'. get_the_permalink() .'">'.get_the_title().'</a></h3>
    				</div>';
    				/* Excerpt */
    				echo '<p>';
    					the_excerpt();
    				echo '</p>';
    
    

    Almost every blogpost contains normal text.
    Some blogpost however contain an embedded PDF file.
    What happens then, my blog page shows the pdfviewer shortcode instead of the PDF contents (which I completely understand).

    I am now searching for a way to edit that piece of code above so that if the the_excerpt() function contains the pdfviewer shortcode in it, it should replace it with a piece of text of my choice, for ex: “Click read more to view this post.”.

    How can I achieve this?
    Hope to hear from you asap!!

    Here is a screenshot of what I actually mean..: https://i.imgur.com/NRXCIaP.png

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Hook “the_excerpt” filter and search the passed content for your shortcode, replacing it with your preferred phrase. Or instead use get_the_excerpt() and search/replace any shortcode before echoing out. Both amount to the same thing really, the main difference is where you add the code. Filter hooks go on functions.php, altering the template code is of course done on the template.

    A filter hook addresses all instances at once, altered templates only affect the templates altered.

    Thread Starter XindaoICT

    (@xindaoict)

    Hey mate,

    Thank you for your reply.
    Can you please help me a little bit with this since I’m unfortunately not a PHP expert?

    What I understand out of your explanation together with this link: https://developer.www.ads-software.com/plugins/hooks/custom-hooks/: I need to create a (custom?) hook for the “the_excerpt” function in my functions.php?

    What I actually want to do is only on this specific page to replace those shortcodes. I already copied the template files which I need to my child-theme folder.

    Thank you.

    Thread Starter XindaoICT

    (@xindaoict)

    Ok, I think I’ve found something after some research. Which is also something I definitely like!

    Adding this code:

    
    // Content Views - Do shortcode on excerpt
    add_filter( 'the_excerpt', 'cv_do_shortcode_excerpt', 100, 1 );
    function cv_do_shortcode_excerpt( $args ) {
    	$args = do_shortcode( $args );
    	return $args;
    }
    

    It shows the content of the shortcode.
    Now the only problem is that the PDF makes the div box extremely tall.
    Already tried to change that via CSS, but not working..

    The PDFviewer shortcode has a predefined height, so that’s what it’s holding on to.. I want just this piece of function/filter to have a separate height..
    How to do that?

    See screenshot for more info:

    View post on imgur.com

    Moderator bcworkz

    (@bcworkz)

    If the height is set in an element style attribute, it’s difficult to override. Ideally the attribute should be changed. It may be the shortcode handler has a filter or some mechanism for altering the height output. If not, you could alter the handler code directly, but that’s a poor choice because your code would be overwritten every time the plugin is updated.

    You could add code to cv_do_shortcode_excerpt() that uses PHP string functions to find the attribute in $args and set it to a reasonable size before the value is returned. This is probably your best option. Assuming I’m correct and the hang up is the style attribute, if you post the current shortcode HTML output (the value of $args), I could suggest proper search and replace code that would do this.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘the_excerpt()’ is closed to new replies.