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

Include HTML before first P inside ‘the_content’ WordPress

  • SOLVED

Hi,

Due to aesthetics <strong>(single.php only)</strong> I need to include some content before the <p> in both my content and excerpt… (displaying both same page) was wondering if there was an easy of doing so, possibly removing the first and last P tag in the query.

<strong>First:</strong>

<p><em><span class="red">Discription:</span><?php the_content('');?>... </em></p>

<strong>Second:</strong>
<p><span class="red">From the Essay:</span> "<?php the_excerpt(); ?>" </p>

<strong>On a side note I need a good character limiter for both</strong>, value must be included with each string considering they differ between files, verse globally.

Answers (5)

2010-11-15

Juanfra Aldasoro answers:

You can create a function over functions.php with a filter function for the_content

you can do something like this:

add_filter( "the_content",'addstyling', 100 );

function addstyling($content){

global $post;

return '<p><span class="red">Discription:</span>'.$content.'... </p>';

}


And once you use the_content() it will show results with the filter function applied.

You can add another filter function for the excerpt.

I hope this helps you.

Regards,
Juan


West Coast Design Co. comments:

Juan,

Thanks … is there a possibility to include it selectively, considering I have both pages, categories, tags and custom queries … all using the same ‘the_content’ this works … except everywhere.

:)


Juanfra Aldasoro comments:

Jermaine is right, you can put some conditionals and use the filter wherever you need it. In your case if you only need that for single.php you should do:


add_filter( "the_content",'addstyling', 100 );

function addstyling($content){

global $post;

if(is_single()) { // works on single (posts)

return '<p><span class="red">Discription:</span>'.$content.'... </p>';

}

}


Hope this help you.
Cheers,
Juan


West Coast Design Co. comments:

Hi Juan,

This didn’t ‘Remove the first and last P tag in the query.’ That’s my entire issue.

Thanks for trying!

2010-11-16

Jermaine Oppong answers:

You should be able to add this selectively with




add_filter( "the_content",'addstyling', 100 );



function addstyling($content){

global $post;

if(is_page() || is_tag() || is_category()) { // works on pages, tag archives and category archives
return '<p><span class="red">Discription:</span>'.$content.'... </p>';
}

}

check out the conditional tags here:

http://codex.wordpress.org/Conditional_Tags


Jermaine Oppong comments:

Looks like the question has been changed. I tested Denzel's code but got no results. Now you've stated that its <strong>single.php</strong> then the code needed would be:



function content($num) {

$limit = $num+1;

$content = str_split(get_the_content());

$length = count($content);


if ($length>=$num) {

$content = array_slice( $content, 0, $num);
$content = implode("",$content)."...";

if(is_single()){

echo '<p><span class="red">Discription:</span>'.$content.'</p>';

}else{

echo '<p>' . $content . '</p>';

}//end check for single post

} else {

if(is_single()){

echo '<p><span class="red">Discription:</span>'. get_the_content() . '</p>';

}else{

the_content();

}//end check for single post

}

}



What the code above does is split the content characters and extracts the amount of characters. This code not only works with your <strong>single.php</strong> but other structures like categories, tags, archives etc...

Execute the results with:



<?php content(20); ?>


West Coast Design Co. comments:

Ahhh!

Just waking up, so many ways of doing the same thing … I do like the idea of keeping this entire string inside a general file (functions.php) helps clean up my ever so organized template includes.

Coffee’s on, thanks!

2010-11-16

Baki Goxhaj answers:

Solved it on site :)

2010-11-16

Denzel Chia answers:

Hi,

If you want character limiting, you can use the functions here, just add in your wordpress conditional tags and html before return.

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

Example character limit with 40 character.

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);

if(is_single()){
return '<p><span class="red">Discription:</span>'.$content.'</p>';
}else{
return $content;
}

}


Usage;


<?php echo content(40); ?>


Hope it helps.

Thanks.


West Coast Design Co. comments:

Hi Danzel,

This didn’t ‘Remove the first and last P tag in the query.’ That’s my entire issue.

Thanks for trying!

2010-11-16

rilwis answers:

For character limiter, because of post content has been already formated, it's very hard to limit its length by characters. I think Denzel Chia's solution for limitting by words is good.

About removing first and last P tag in the query, I think we better wrap your additional content into a new P tag, then insert it into the beginning of post content. This way, all the P tags are set in right order.

So, we get this code:

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);
if(is_page()){
return '<p><span class="red">Discription:</span></p>'.$content;
}else{
return $content;
}
}


Use: <?php echo content(100); ?> in your loop.