Page 1 of 1

UDT smarty assign foreach wordpress

Posted: Thu Dec 13, 2007 2:52 am
by giggler
So I have things in parts, but honestly I don't know how to put things together using User Define Tags. I was able to use this elsewhere to get worpress post running nicely with SEF URLs. If someone know how to use this in UDT, that would be awesome:

This is to get the db stuff, now even sure if I have the smarty assign part right:

Code: Select all

$wp_post = $db->GetAll("SELECT * FROM `wp_posts` WHERE post_type = 'POST' AND post_status ='publish' ORDER BY post_date DESC LIMIT 8");
$this->smarty->assign ('wp_post'        , $wp_post);
somewhere, I have no idea where for cmsms, this should call the latest post with nice permalink:

Code: Select all

{foreach from=$wp_post item=wp_post name=wp_post}
<a href="/blog/{$wp_post.post_date|date_format:"%m/%Y/"}{$wp_post.post_name}/">{$wp_post.post_title|escape}</a><br />{$wp_post.post_excerpt}<br /> <br /> 
{/foreach}
General help, commercial help, any kind of help will be appreciated!

Re: UDT smarty assign foreach wordpress

Posted: Thu Dec 13, 2007 3:17 am
by calguy1000
wow, you're lucky, from the subject... I was thinking some kind of smart spam bot...

but anyways.... try {$wp_post|print_r} in your template after the udt is called, that should help.

Re: UDT smarty assign foreach wordpress

Posted: Thu Dec 13, 2007 4:01 am
by giggler
hahaha

K, I didn't try your method because I still wouldn't know where to put what, but this actually worked with nice SEO Friendly URL which Wordpress uses (assuming you set the same permalink):

Put this in UDT can call it {wp_post} or whatever you want and put it where ever you want and change the LIMIT#...

Code: Select all

global $gCms;
global $db;

$vars = $gCms->variables;

$query2 = "SELECT date_format(post_date, '%Y/%m/%d') as date_format, post_title, post_excerpt, post_name FROM wp_posts WHERE post_type = 'POST' AND post_status ='publish' ORDER BY post_date DESC LIMIT 8";
$result2 = mysql_query($query2) or die("Error: " . mysql_error());

while($row = mysql_fetch_array( $result2 )) {
	// Print out the contents of each row into a table
$date_format = $row["date_format"];
$post_title = $row["post_title"];
$post_name = $row["post_name"];
$post_excerpt = $row["post_excerpt"];
	echo "<a href='/blog/$date_format/$post_name/'>$post_title</a><br>";
echo "$post_excerpt<br>";}
just change the /blog/ to the directory of where your wordpress blog is from the root directory.

UPDATE: I forgot, this assumes you are using the post_excerpt field which is labelled as "Optional Excerpt" when you are posting. If you want the whole post, then use post_content in place of post_excerpt. If you want to break each category down into it's own UTD you can do that by adding in post_category.

Re: UDT smarty assign foreach wordpress

Posted: Wed Jan 16, 2008 4:53 pm
by giggler
This is an updated version without needing to enter extra excerpt. This uses the post itself and truncates it:

Code: Select all

function myTruncate($string, $limit, $break=".", $pad="...")
  {
    // return with no change if string is shorter than $limit
    if(strlen($string) <= $limit) return $string;
 
    // is $break present between $limit and the end of the string?
    if(false !== ($breakpoint = strpos($string, $break, $limit))) {
      if($breakpoint < strlen($string) - 1) {
        $string = substr($string, 0, $breakpoint) . $pad;
      }
    }
    
    return $string;
  }

global $gCms;
global $db;

$vars = $gCms->variables;

$query2 = "SELECT date_format(post_date, '%Y/%m/%d') as date_format, post_title, post_excerpt, post_content, post_name FROM wp_posts WHERE post_type = 'POST' AND post_status ='publish' ORDER BY post_date DESC LIMIT 8";
$result2 = mysql_query($query2) or die("Error: " . mysql_error());


while($row = mysql_fetch_array( $result2 )) {
	// Print out the contents of each row into a table
$date_format = $row["date_format"];
$post_title = $row["post_title"];
$post_name = $row["post_name"];
$post_excerpt = $row["post_excerpt"];
$post_content = $row["post_content"];
$postshort = myTruncate($post_content, 50);
	echo "<a href='/blog/$date_format/$post_name/'>$post_title</a><br>";
echo "$postshort<br>";}

on the page where you want the title with short summary, just add:

Code: Select all

{wp_post_date}

This is the httacess if your blog is in the blog directory:

Code: Select all

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress
This will result in url for the blog like:
blog/2008/01/05/lorem-ipsum-is-simply/

BTW-this is possible thanks to the following about php truncate!
http://www.the-art-of-web.com/php/truncate/

Re: UDT smarty assign foreach wordpress

Posted: Wed Jan 16, 2008 6:11 pm
by cyberman
Many thanks for updated version :).

Re: UDT smarty assign foreach wordpress

Posted: Tue Mar 11, 2008 2:02 pm
by avon_g
Found time and have done the change....did notice that there was "]" missing at the end of fitz htaccess file...gave me a 500 server error to but the "]" fixed it.....

Re: UDT smarty assign foreach wordpress

Posted: Sun May 11, 2008 12:54 pm
by moonie
Great tutorial, saved me lots of trouble!

Does anyone have an idea how to grab the corresponding category, tags or author to the posts in the wp_posts table? I fail to understand how it links to these, and I'd like to display these along with the post summary, too...  ???

Re: UDT smarty assign foreach wordpress

Posted: Thu May 22, 2008 7:31 am
by cyberman
moonie wrote: Does anyone have an idea how to grab the corresponding category, tags or author to the posts in the wp_posts table?
Dont know format of wp tables ... maybe you have only to modify the select request.