Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adding HTML To Excerpts WordPress

  • SOLVED

I am attempting to add HTML (name H tags) to my excerpts via the Advanced Excerpts plugin, however I believe my theme is not allowing it do some conflicting PHP. The Plugin is not working.

I believe the 'content' line isn't gelling with the following:

<code><p class="mpc-excerpt"><?php echo preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', agera_my_excerpt(get_the_content(''), 85));?></p></code

I've spent much too long on this!

Please help.

Answers (2)

2012-11-21

Dbranes answers:


<strong>update:</strong> well it actually works to write html directly in the excerpt box ;-)

but I guess you want to use the auto generated excerpt from the content (i.e. you have all your excerpt boxes empty) ?

---

Hi, if you only need the tags h3 and h4 in your excerpts, you could fx. write

[h3]some text[/h3]
[h4]some more text[/h4]


in your excerpt box and then add this in your functions.php

<?php
add_filter("the_excerpt","convert_brackets");

function convert_brackets($excerpt){
//edit this if you need to add more tags
$from=array("[h3]","[h4]","[/h3]","[/h4]");
$to=array("<h3>","<h4>","</h3>","</h4>");

$excerpt=str_replace($from,$to,$excerpt);
return $excerpt;
}
?>


and then use

the_excerpt();

to display the excerpt with the html tags h3 and h4.



cunningfox comments:

Yes, but if it works as a temp fix where I just have to write the copy twice I'm game...is this possible?

Would the code: the_excerpt(); just preceded the excerpt copy in the HTML on that page?

The theme developer (who sorta sucks) replied that I should delete his custom exceprt tool and just reinstall Advanced Excerpt. I'm assuming his custom tool is : <blockquote>agera_my_excerpt </blockquote> from the above .php?

Where would I find that to delete?


Dbranes comments:

i.e. you could try to replace

<p class="mpc-excerpt"><?php echo preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', agera_my_excerpt(get_the_content(''), 85));?></p>

with

<p class="mpc-excerpt"><?php the_excerpt();?></p>

and write html code directly in your excerpt box.

(ps: maybe this is what Arnav meant in the above threads?)


Dbranes comments:

ps: and comment this out


//remove_filter('get_the_excerpt', 'wp_trim_excerpt');
//add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');


cunningfox comments:

Ok, I'll try the above as a resort after asking this.

The theme developer just got back to me with the following:

<blockquote>You would have to remove the filter from functions.php line 579 :) but you would have to also change every instance of the function call to the default wordpress function. If you don't know your way around wordpress this wont be easy.</blockquote>

That filter is this:

function agera_my_excerpt($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if (count($words) > $word_limit)
array_pop($words);
return strip_tags(implode(' ', $words) . '...');


What would I remove?


Dbranes comments:


<blockquote>You would have to remove the filter from functions.php line 579 :)</blockquote>
you could try to comment out line 579 (adding // in front of that line)

but you would have to also change every instance of the function call to the default wordpress function
I think he means to find all "agera_my_excerpt" function calls in your code, fx lines like

<p class="mpc-excerpt"><?php echo preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', agera_my_excerpt(get_the_content(''), 85));?></p>

and replace it with the function you want to use instead, fx

<p class="mpc-excerpt"><?php the_excerpt();?></p>


cunningfox comments:

What would it cost to have you do this?


Dbranes comments:

just for the reference:

a workable solution seems to be to modify the current theme function agera_my_excerpt to

function agera_my_excerpt($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if (count($words) > $word_limit){
array_pop($words);
}
return strip_tags(implode(' ', $words) . '...',"<h3><h4>");
}


and replace

<p class="mpc-excerpt"><?php echo preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', agera_my_excerpt(get_the_content(''), 85));?></p>


with

<div class="mpc-excerpt"><?php echo preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', agera_my_excerpt(get_the_content(''), 85));?></div>


and make some css changes.

2012-11-21

Arnav Joy answers:

are you getting errors?


cunningfox comments:

No errors, the plugin just isn't doing anything.


Arnav Joy comments:

please explain more about your problem.


cunningfox comments:

I would like to include styled test within the excerpts on the the homepage flip boxes.

.mpc-card .face.back p.mpc-excerpt

On the first box I have <blockquote>Since 2001, Billion Dollar Babes has reigned as the world’s premier designer sample sale.(??)</blockquote> wrapped in a H3, but it is stripping it from the excerpt. I need the excerpt on the homepage to allow styled HTML text.


Arnav Joy comments:

try this in functinos.php

function custom_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
//Retrieve the post content.
$text = get_the_content('');

//Delete all shortcode tags from the content.
$text = strip_shortcodes( $text );

$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);

$allowed_tags = ''; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/
$text = strip_tags($text, $allowed_tags);

$excerpt_word_count = 55; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);

$excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');



cunningfox comments:

Hmm. Nothing. I deactivated Advanced Excerpt Plugin, should I have kept active or does that matter?


Arnav Joy comments:

you can deactivate plugin and use it

<?php the_excerpt();?>


Arnav Joy comments:

and if you use it then you do not have to filter anything


<?php the_excerpt();?>


cunningfox comments:

I'm confused.

The code you gave me is not working.

What did you want me to do next? (I do not have Advanced Excerpt Installed.)


cunningfox comments:

Specifically, what does:

<blockquote>you can deactivate plugin and use it

<?php the_excerpt();?></blockquote>

mean?


Arnav Joy comments:

i mean to say that if you use

<?php the_excerpt();?>

then you do not have to filter it using any other code and you also can deactivate plugin then


Arnav Joy comments:

do you see any setting in admin panel for hover / active state of menu


cunningfox comments:

I'm an idiot. Of course. The text was white on the background box before; with that gone I need to change the header text in the theme. However now the font color on the menu to the left is no longer white...




cunningfox comments:

But the code you sent isn't working. Why do you mean by use <?php the_excerpt();?>


Here is the entire .php related to excerpts in my functions.php


/*-----------------------------------------------------------------------------------*/
/* Portfolio Setup - columns width, excerpt length ect.
/*-----------------------------------------------------------------------------------*/

function custom_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {

//Retrieve the post content.

$text = get_the_content('');

//Delete all shortcode tags from the content.

$text = strip_shortcodes( $text );

$text = apply_filters('the_content', $text);

$text = str_replace(']]>', ']]&gt;', $text);

$allowed_tags = '<h3>,<h4>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/

$text = strip_tags($text, $allowed_tags);

$excerpt_word_count = 55; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/

$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);

$excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/

$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);

if ( count($words) > $excerpt_length ) {

array_pop($words);

$text = implode(' ', $words);

$text = $text . $excerpt_more;

} else {

$text = implode(' ', $words);

}

}

return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);

}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');

add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');



/*-----------------------------------------------------------------------------------*/


function agera_portfolio_columns($type, $page_data) {
global $mp_option;
global $shortname;

if (has_post_thumbnail()) {
?>
<div class="portfolio-item-thumb">
<div class="mpc-card">
<div class="front face">
<?php the_post_thumbnail( $type ); ?>
</div>
<div class="back face" style="background: <?php echo $page_data['background']; ?> ;">
<img class="mpc-viniet" src="<?php echo MPC_THEME_ROOT; ?>/images/viniet.png"/>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<!-- Remove shortcodes from excerpts -->
<p class="mpc-excerpt"><?php echo preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', agera_my_excerpt(get_the_content(''), 160));?></p>
<?php if( function_exists('zilla_likes') ) zilla_likes(); ?>

<?php if(isset($page_data['lightbox']) && $page_data['lightbox']) {
$type = '';
$asset = $page_data['lightbox_src'];
$search = preg_match('/.(jpg|JPG|jpeg|JPEG|gif|GIF|png)/', $asset);
if($search == 1) {
$type = 'mpc-image';
$search = 0;
}

$search = preg_match('/.(vimeo)./', $asset);

if($search == 1) {
$type = 'mpc-vimeo-video';
$search = 0;
}

$search = preg_match('/.(youtube)/', $asset);

if($search == 1) {
$type = 'mpc-youtube-video';
$search = 0;
}

$search = preg_match('/.(swf|SWF)/', $asset);
if($search == 1) {
$type = 'mpc-swf';
$search = 0;
}

if($type == '') {
$type = 'mpc-iframe';
}

?>

<a class="mpc-fancybox <?php echo $type; ?>" rel="<?php echo $page_data['gallery']; ?>" href="<?php echo $page_data['lightbox_src']; ?>" title="<?php echo $page_data['caption']; ?>"></a>
<?php } ?>

<a class="view-details" href="<?php the_permalink(); ?>"></a>
</div>
</div>
</div><!-- portfolio_item_thumb -->
<?php }
}

/*-----------------------------------------------------------------------------------*/
/* Triming the excerpt
/*-----------------------------------------------------------------------------------*/

function agera_my_excerpt($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if (count($words) > $word_limit)
array_pop($words);
return strip_tags(implode(' ', $words) . '...');
}