Need a hand with shortening a text string
Posted: Thu Jan 01, 2009 2:50 pm
Hello everyone, the name's Christian..i'm 27 years old and live in the UK.
I am creating a 'portal' for one of my Wordpress powered blogs. So far i've managed to make the portal display the number of articles and the number of comments on the blog. I've also managed to code it up so I can display the title of the most recent blog.
However, my problem is that the space the recent post title has to fit in isn't very large...about 50 characters i'd say. I've looked into shortening text strings but haven't had an awful lot of luck.
Here's the code i'm using to call the recent post title:
The variable to actually post the recent post title is $post_title.
As I said I've looked into this a few days ago and came up with this code I placed below the code above:
The problem is, for one reason or another it just isn't cutting long blog titles down to 50 char and adding "..." at the end. Can anybody help me fix this code, or suggest a different one?
Many Thanks
I am creating a 'portal' for one of my Wordpress powered blogs. So far i've managed to make the portal display the number of articles and the number of comments on the blog. I've also managed to code it up so I can display the title of the most recent blog.
However, my problem is that the space the recent post title has to fit in isn't very large...about 50 characters i'd say. I've looked into shortening text strings but haven't had an awful lot of luck.
Here's the code i'm using to call the recent post title:
Code: Select all
<?php
// Call the database connection settings
require("wp-config.php");
// Connect to WP database
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if ( !$dbc ) {
die( 'Not Connected: ' . mysql_error());
}
// Select the database
$db = mysql_select_db(DB_NAME);
if (!$db) {
echo "There is no database: " . $db;
}
// Formulate the query
$query = "
SELECT post_title,post_content,UNIX_TIMESTAMP(post_date) AS post_date_unix
FROM `wp_posts`
WHERE `post_status` = 'publish'
AND `post_password` = ' '
AND `post_type` = 'post'
ORDER BY `wp_posts`.`post_date` DESC
";
// Perform the query
$result = mysql_query( $query );
// Check results of the query and terminate the script if invalid results
if ( !$result ) {
$message = 'Invalid query.' . PHP_EOL;
$message .= 'Whole query: ' . $query . PHP_EOL;
die ( $message );
}
/*
* QUERY RESULT VARIABLES
*/
// Count the number of rows returned by the query
$num_rows = mysql_num_rows( $result );
// Use the results of query
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
// Init var for DATE of the post
$post_date = date( "l, F jS, Y ", $row['post_date_unix'] );
// Init var for TITLE of the post
$post_title = $row['post_title'];
// Init var for CONTENT of the post
$post_content = $row['post_content'];
// Free the resources associated with the result set
if ( $result ) {
mysql_free_result( $result );
mysql_close();
}
?>As I said I've looked into this a few days ago and came up with this code I placed below the code above:
Code: Select all
<?php echo strlen($post_title > 50) ? substr($post_title, 0, 50) . '...' : $post_title; ?>Many Thanks