Jump to content

Limiting characters


papillonstudios

Recommended Posts

Im not sure f this is the right spot for this or not but...

 

I have a website that has a content box that has a fixed height and width.

 

How would i make it so after the box is full it puts a link to read more at the bottom of it? Also images may be used as well.

 

Its also for a wordpress blog

Link to comment
Share on other sites

Use this to get you started:

 

<?php$text="PHP is proud to announce TestFest 2010. TestFest is PHP's annual campaign to increase the overall code coverage of PHP through PHPT tests. During TestFest, PHP User Groups and individuals around the world organize local events where new tests are written and new contributors are introduced to PHP's testing suite.Last year was very successful with 887 tests submitted and a code coverage increase of 2.5%. This year we hope to do better.TestFest's own SVN repository and reporting tools are back online for this year's event. New to TestFest this year are automated test environment build tools as well as screencasts showing those build tools in action.Please visit the TestFest 2010 wiki page for all the details on events being organized in your area, or find out how you can organize your own event.";$limit=200;echo substr($text,0,$limit);echo '<hr>';echo substr($text,$limit);?>

 

 

of course you'll need to solve the problem of it cutting words in half but that shouldn't be too hard :P

Link to comment
Share on other sites

ok i have came up with this code here:

<?php 

		 $entry = the_content(); 
		 $limit = 200;

		 if (strlen($entry) > || strlen($entry) == $limit)
		 {
			 substr($entry,0,$limit);
			  echo '<a href="#"><img src="<?php bloginfo("template_url"); ?>/images/readmore.png" class="readmore" /></a>'
		 }	

		 ?>

 

but i forget what the proper way to say if $entry is greater than 200 or equal to 200 then do this.

 

if that right i do get this error when i load the page

Parse error: syntax error, unexpected T_BOOLEAN_OR in /Users/GamingFusion/Sites/rbrwp/wp-content/themes/RedBarRadio/index.php on line 139

 

 

Link to comment
Share on other sites

ok its not working right it says the post has zero characters.

<?php 

		 $entry = the_content(); 
		 $limit = 200;

		 if (strlen($entry) >= $limit)
		 {
			 substr($entry,0,$limit);
			  echo '<a href="#"><img src="<?php bloginfo("template_url"); ?>/images/readmore.png" class="readmore" /></a>';
		 }	

		 echo 'This post has: ' . strlen($entry) . ' Characters';
		 ?>

 

OUTPUT :

Kelt yaders piat keropas jergyadera kertyuades fertyseras

omniriea kerod miaset tirase neruasera jerta kertyua eatdertasen

accusa ntium reue kertsdea

Haseistique orci ferodeaser vasaecer dimagna nasertad ace

ultricies pharetras malesuada orcgertyae nec accumanera semis

ertsertas mierty uadesaso nec sit amet erosertaera kertya.

Lorem ipsum dolor sit amnsetuer adipiscing elit.

Mauris fermentum dictum magna. Sed laoreet aliquam leo. Ut tellus dolor, dapibus eget, elementum vel, cursus eleifend, elit. Aenean auctor wisi et urna.

Haseistique orci ferodeaser vasaecer dimagna nasertad aceultricies pharetras malesuada orcgertyae nec accumanera semis ertsertas mierty uadesaso nec sit amet erosertaera kertya. Lorem ipsum dolor sit amnsetuer adipiscing elit. Mauris fermentum dictum magna. Sed laoreet aliquam leo. Ut tellus dolor, dapibus eget, elementum vel, cursus eleifend, elit. Aenean auctor wisi et urna.

Haseistique orci ferodeaser vasaecer dimagna nasertad ace ultricies pharetras malesuada orcgertyae nec accumanera semis ertsertas mierty uadesaso nec sit amet erosertaera kertya.

Lorem ipsum dolor sit amnsetuer adipiscing elit. Mauris fermentum dictum magna. Sed laoreet aliquam

This post has: 0 Characters

 

 

Link to comment
Share on other sites

Ok putting all the text in the variable like this works

<?php 

		 $entry = 'Kelt yaders piat keropas jergyadera kertyuades fertyseras
omniriea kerod miaset tirase neruasera jerta kertyua eatdertasen
accusa ntium reue kertsdea
Haseistique orci ferodeaser vasaecer dimagna nasertad ace
ultricies pharetras malesuada orcgertyae nec accumanera semis
ertsertas mierty uadesaso nec sit amet erosertaera kertya.
Lorem ipsum dolor sit amnsetuer adipiscing elit.
Mauris fermentum dictum magna. Sed laoreet aliquam leo. Ut tellus dolor, dapibus eget, elementum vel, cursus eleifend, elit. Aenean auctor wisi et urna.
Haseistique orci ferodeaser vasaecer dimagna nasertad aceultricies pharetras malesuada orcgertyae nec accumanera semis ertsertas mierty uadesaso nec sit amet erosertaera kertya. Lorem ipsum dolor sit amnsetuer adipiscing elit. Mauris fermentum dictum magna. Sed laoreet aliquam leo. Ut tellus dolor, dapibus eget, elementum vel, cursus eleifend, elit. Aenean auctor wisi et urna.
Haseistique orci ferodeaser vasaecer dimagna nasertad ace ultricies pharetras malesuada orcgertyae nec accumanera semis ertsertas mierty uadesaso nec sit amet erosertaera kertya.
Lorem ipsum dolor sit amnsetuer adipiscing elit. Mauris fermentum dictum magna. Sed laoreet aliquam'; 
		 $limit = 200;

		 if (count($entry) >= $limit)
		 {
			 substr($entry,0,$limit);
			  echo '<a href="#"><img src="<?php bloginfo("template_url"); ?>/images/readmore.png" class="readmore" /></a>';
		 }	

		 echo 'This post has: ' . count($entry) . ' Characters';
		 ?>

 

but now it doesnt want to show the content.

Link to comment
Share on other sites

EDIT: Missed your last post. I see you've already figured out the first part . . .

If I replace the function call to the_content() with

$entry = "[that whole entry in your last post]";

 

It returns this (HTML source). You need to remove the <?php ?> tags on this line, since you're inside php already.

<a href="#"><img src="<?php bloginfo("template_url"); ?>/images/readmore.png" class="readmore" /></a>This post has: 1189 Characters

Link to comment
Share on other sites

ok i took out the if statement kuzz i should have a link to the post always anyways so heres my code now just have to get it to work with the the_content();


<?php 

			$entry = the_content(); 
			$limit = 200;
		 	substr($entry,0,$limit);
		 ?>
			  <a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a>

 

Link to comment
Share on other sites

its a wordpress blog but heres the function for it.

 

function the_content($more_link_text = null, $stripteaser = 0) {

        $content = get_the_content($more_link_text, $stripteaser);

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

        $content = str_replace(']]>', ']]>', $content);

        echo $content;

}

Link to comment
Share on other sites

The problem you're experiencing is due to the fact that the function uses echo rather than return. As soon as you invoke the function, it echos everything to the screen instead of allowing you to store the output in a variable, therefore $entry has no value.

 

I'm going to stop short of recommending you change the function to return the value, instead of echoing it because I'm nearly 100% certain doing that will cause a virtual shitstorm of problems. You can try it if you want, but I doubt it will give you good results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.