UDT smarty assign foreach wordpress

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
giggler
Forum Members
Forum Members
Posts: 197
Joined: Tue Oct 09, 2007 7:08 am

UDT smarty assign foreach wordpress

Post 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!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: UDT smarty assign foreach wordpress

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
giggler
Forum Members
Forum Members
Posts: 197
Joined: Tue Oct 09, 2007 7:08 am

Re: UDT smarty assign foreach wordpress

Post 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.
Last edited by giggler on Thu Dec 13, 2007 4:19 am, edited 1 time in total.
giggler
Forum Members
Forum Members
Posts: 197
Joined: Tue Oct 09, 2007 7:08 am

Re: UDT smarty assign foreach wordpress

Post 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/
Last edited by giggler on Wed Jan 16, 2008 8:23 pm, edited 1 time in total.
cyberman

Re: UDT smarty assign foreach wordpress

Post by cyberman »

Many thanks for updated version :).
avon_g

Re: UDT smarty assign foreach wordpress

Post 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.....
User avatar
moonie
Forum Members
Forum Members
Posts: 81
Joined: Tue Feb 13, 2007 3:08 pm

Re: UDT smarty assign foreach wordpress

Post 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...  ???
cyberman

Re: UDT smarty assign foreach wordpress

Post 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.
Post Reply

Return to “CMSMS Core”