PHP Question
- Started
- Last post
- 11 Responses
- hubb7designs
So I have figured out how to embed Wordpress into my website and it works great.
I'm using the following code to place the Blog post on my home page:
<?php while (have_posts()): the_post(); ?>
<h5><?php the_title(); ?></h5>
<?php the_excerpt(); ?>
<p><a href="<?php the_permalink(); ?>">Read more...</a></p>
<br />
<?php endwhile; ?>What I'm trying to figure out is how can I limit the size of the post that shows up on the home page?
Right now the post overflow in the box where they are displayed and it doesn't look clean.
- ismith0
I had just typed a reply when I realized you said SIZE and not length. Truncating text to a length or by certain rules isn't difficult (the_excerpt is automatic and self explanatory) but if you want to control it's actual size... I don't know how to help as far as PHP. Did you try to adjust the overflows in CSS?
- hubb7designs0
@ismith: Sorry for the confusion. I'm not trying to adjust the size of the text, but I'm trying to limit the amount of text that is displayed.
I probably only want to show 2-3 lines in the post.
- hubb7designs0
So I tried overflow:hidden, and that didn't do the trick.
Anyway to limit the length of the post through the php coding?
- BattleAxe0
try playing with substr it lets you set a starting point and a character length
so instead of
< ? php the_excerpt(); ? >something like
< ? php $text = the_excerpt();
echo $text = substr ( $text,0,150) ; ? >
0 = start at first position of the post , 150 is the character limit
- acescence0
a couple changes from the post above- the_excerpt will always echo the text, you can't assign it to a var like that. you either need to buffer output or use get_the_excerpt.
if you do it by character using substr, you'll end up with parts of words instead of only whole words..$string = get_the_excerpt();
$words = explode(' ', $string);
$string = implode(' ', array_slice($words, 0, 10)); // 10 = number of words
echo $string;- yes , I was uneasy about the_excerpt(); not sure what it's made offBattleAxe
- hubb7designs0
@BattleAxe and @acescence
I tried both of your settings and neither seemed to have any effect?
- just pasted it in a theme and it's working here, are you using it within the loop? can we see it?acescence
- vaxorcist0
nice one acescence
- hubb7designs0
@acescence....
I do believe it's within a loop... still pretty new to PHP
Here is the link to the home page...
- I'm editing the PHP code outside of the Wordpress Admin also.. if that changes things.hubb7designs
- vaxorcist0
PHP source code would be helpful.... can you post a duplicate copy of the code with .phps as the filename extension? NOTE: remove any private stuff first...
- hubb7designs0
On line 1 of the index.php I have:
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
query_posts('showposts=3');
?>Then for the section that contains the summary of the last blog post I have:
<div id="fromtheblog">
<h1 class="blog">from the blog</h1>
<?php while (have_posts()): the_post(); ?>
<h5><?php the_title(); ?></h5>
<?php the_excerpt(); ?>
<p<a href="<?php the_permalink(); ?>">Read more...</a></p>
<br />
<?php endwhile; ?>
</div>