Can anyone supply me with some code that will grab the last 3 blog posts from Wordpress within Magento using the Fishpig extension. Can't find a template to use anywhere. Need to grab blog title, content and featured image.
I believe i'm on the wrong track with what I have so far.
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID ORDER BY p.post_date";
$results = $readConnection->fetchAll($query);
?>
<?php
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='/news/wp-content/uploads/'.($results1[0]['meta_value']);
?>
<div class="blog-post-image">
<img src="<?php echo $url; ?>">
</div>
<div class="blog-post-content">
<?php ?>
<h3> <?php echo $row['post_title'];?></h3>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
More Info
</div>
<?php endif; ?>
<?php
if($counter == 4)
{
break;
}
$counter++;
}
?>
Thanks.
Related
I have created two tables in MySql one where blog content saved and another where blog ID and different pics path saved. I have saved more than one pics in one postID. Now I am trying to fetch the pics and blog post at same time but its not working. code below...
blogposts.php
<?php
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * , upload_data.FILE_NAME from blog_posts LEFT JOIN upload_data ON blog_posts.postID=upload_data.postID ; ");
while($row = mysqli_fetch_array($result))
{?>
<div class="b-slider j-smallslider" data-height="382">
<ul><?php
if (!empty($FILE_NAME)) {
$result = mysqli_query($conn,"SELECT * from upload_data where postID=postID ; ");
while($row = mysqli_fetch_array($result))
{?>
<li data-transition="3dcurtain-vertical" data-slotamount="12">
<img data-retina src="articles/user_data/<?php echo( htmlspecialchars( $row['FILE_NAME'] ) ); ?>">
</li> <?php }} ?>
</ul>
</div>
<div class="b-blog-one-column__info">
Title : <?php echo( htmlspecialchars( $row['postTitle'] ) ); ?>, <?php echo( htmlspecialchars( $row['postCat'] ) ); ?>
<span class="b-blog-one-column__info_delimiter"></span>
Tag : Nllam
<span class="b-blog-one-column__info_delimiter"></span>
<i class="fa fa-comment"></i>12 Comments
</div>
<?php } ?>
The Problem is I am unable to fetch the pics. :(
use this query
$result = mysqli_query($conn,"SELECT blog_posts.* , upload_data.FILE_NAME from blog_posts,upload_data WHERE blog_posts.postID=upload_data.postID");
and then print your results with print_r() function and check its get results or not.
SELECT blog_posts.*, group_concat(upload_data.FILE_NAME) FROM `blog_posts`
LEFT JOIN upload_data on blog_posts.postID = upload_data.postID
GROUP BY blog_posts.postID
Try like this you will get number of post rows and filename concadinated with commas if you have images. then Apply logic to display results. Any help you need. Don't hesitate to ask me.
hope this work;
$post_id = 1337;
$conn = new PDO(......); //get connection
$conn->query('SET group_concat_max_len = 4096;');
$query = "SELECT bp.* ,
(SELECT GROUP_CONCAT(ud.FILE_NAME SEPARATOR ',')
from upload_data ud where ud.postID=bp.postID) as files
FROM blog_posts bp
WHERE bp.postID=:post_id ;";
$result = $conn->prepare($query);
$result->bindParam(":post_id", $post_id);
$result->execute();
//don't really need while for one row but oh well
while($row = $result->fetch()){
$files=$row['files'];
$files = empty($files) ? [] : explode(',', $files);
?>
<div class="b-slider j-smallslider" data-height="382">
<ul>
<?php foreach ($files as $file){?>
<li data-transition="3dcurtain-vertical" data-slotamount="12">
<img data-retina src="articles/user_data/<?php echo( htmlspecialchars( $file ) ); ?>">
</li>
<?php } ?>
</ul>
</div>
<div class="b-blog-one-column__info">
Title : <?php echo( htmlspecialchars( $row['postTitle'] ) ); ?>, <?php echo( htmlspecialchars( $row['postCat'] ) ); ?>
<span class="b-blog-one-column__info_delimiter"></span>
Tag : Nllam
<span class="b-blog-one-column__info_delimiter"></span>
<i class="fa fa-comment"></i>12 Comments
</div>
<?php } ?>
I am trying to get my code to return the last 3 posts from wordpress in Magento using the Fishpig extension. This is the code I have so far, but it appears to returning posts more than once. I also need it to just return posts, as it also returns pages at the moment.
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID ORDER BY p.post_date";
$results = $readConnection->fetchAll($query);
?>
<?php
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='/news/wp-content/uploads/'.($results1[0]['meta_value']);
?>
<div class="blog-post-image">
<img src="<?php echo $url; ?>">
</div>
<div class="blog-post-content">
<?php ?>
<h3> <?php echo $row['post_title'];?></h3>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
More Info
</div>
<?php endif; ?>
<?php
if($counter == 4)
{
break;
}
$counter++;
}
?>
To display 3 posts and display the post title, URL, image and post content (or excerpt), you would need the following code:
<?php $posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addPostTypeFilter('post')
->setPageSize(3)
->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
<?php echo $post->getPostContent() ?>
<?php
/**
* You could also use:
* echo $post->getPostExcerpt(20)
* This would display the first 20 words of the post content
* or the manually entered excerpt
**/
?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This has been writte quickly by hand and hasn't been tested but should work fine.
Im trying to get two of the latest posts from Wordpress that include featured images. Need to get the post title, content (character limited) and the featured image. I have this so far, all that is missing is the featured image.
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT `id`, `post_title`,`post_name` ,`post_content`, `comment_count` FROM `wp_posts` WHERE `post_type`='post' ORDER BY `comment_count` DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'): ?>
<li class="item">
<?php echo $row['post_title'];?>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
Try this:
I have updated your query and added another query to get post image url :
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID WHERE pm.meta_key = '_thumbnail_id' AND p.post_type='post' ORDER BY p.post_date DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='www.yoursite.com/wp-content/uploads/'.($results1[0]['meta_value']);
echo $url; //YOUR URL just set your site name
//So final url YOUR--> SITENAME/wp-content/uploads/pathfromquery
?>
<li class="item">
<?php echo $row['post_title'];?>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
You shouldn't really be using raw SQL here. As a work around, couldn't you do either of the following:
1) Select more than 2 posts (eg. 5) and loop through them and display the 2 that have featured images
2) Ensure all posts have featured images and then select the first 2 posts
3) Use a collection of posts and then add your custom SQL to that.
This code will get 2 posts and add the _thumbnail_id to the collection. You can add some code to this to check this field exists (use $posts->load(true) to debug the SQL query and $posts->getSelect()->where() to add the custom filter)
<?php $posts = Mage::getResourceModel('wordpress/post_collection') ?>
<?php $posts->addPostTypeFilter('post') ?>
<?php $posts->addIsViewableFilter() ?>
<?php // Limit the collection to 2 posts. Change this number or remove this line completely to include all posts
<?php $posts->setPageSize(2) ?>
<?php // Adds the _thumbnail_id meta field to the collection ?>
<?php // This can be used for checking in the SQL whether a post has a featured image ?>
<?php $posts->addMetaFieldToSelect('_thumbnail_id') ?>
<?php $posts->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2><?php echo $post->getPostTitle() ?></h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
<div class="post-content"><?php echo $post->getPostContent() ?></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I am creating a forum and the code I am putting in this thread is the page that shows all of the forum categories. I have this setup where I have an id, category_section, category_title, and category_description. The way it is structured now is that it creates a new category section border every time I add a new category title in my database. This is what I mean by this:
As an example:
My category section is called Shoes, my category titles are Nike, Adidas, Reebok
Currently, it does:
Shoes
Nike
Shoes
Adidas
Shoes
Reebok
But it should do this:
Shoes
Nike
Adidas
Reebok
How can I get my current code to do this?
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_title DESC");
$numrows = mysqli_num_rows($query);
$categories = "";
if($numrows > 0){
while($forum_row = mysqli_fetch_assoc($query)){
$categoryid = $forum_row['id'];
$category_section = $forum_row['category_section'];
$categoryTitle = $forum_row['category_title'];
$categoryDescription = $forum_row['category_description'];
$categories = "<a href='forum_view_category.php?cid=".$categoryid."'>"
. $categoryTitle . "</a>";
//$categories .= "<a href='forum_view_category.php?cid=".$categoryid
//."' class='cat_links'>" . $categoryTitle . "</a>" " - " . $categoryDescription .;
?>
<div class="category_section">
<?php echo $category_section; ?>
</div>
<div class="category_border">
<div class="discussions_left">
<div class="discussions_category_title">
<?php echo $categories; ?>
</div>
<div class="discussions_category_description">
<?php echo $categoryDescription; ?>
</div>
</div>
<div class="discussions_right">
</div>
</div>
<?php
}
//echo $categories;
} else {
echo "<p>There are no posted categories available yet. </p>";
}
I would change your SQL to order by section first, then the title.
SELECT * FROM forum_categories ORDER BY category_section DESC, category_title DESC
After you've done that, define an if statement to check if the current section is the same as the previous statement.
If so, move one.
If not, echo the new section title.
I've cleaned up your code a bit and switched to the Alternative syntax for control structures for readability and maintainability.
PHPFiddle Demo (faked MySQL)
<?php
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_section DESC, category_title DESC");
$category_section = "";
?>
<?php if(mysqli_num_rows($query) > 0): ?>
<?php while($cat = mysqli_fetch_assoc($query)): ?>
<?php if($cat['category_section'] !== $category_section): ?>
<?php $category_section = $cat['category_section'] ?>
<div class="category_section">
<?= $category_section ?>
</div>
<?php endif; ?>
<div class="category_border">
<div class="discussions_left">
<div class="discussions_category_title">
<a href="forum_view_category.php?cid=<?= $cat['id'] ?>">
<?= $cat['category_title'] ?>
</a>
</div>
<div class="discussions_category_description">
<?= $cat['category_description'] ?>
</div>
</div>
<div class="discussions_right">
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<p>There are no posted categories available yet.</p>
<?php endif; ?>
I am trying to make a youtube like main page. With the code below I want to make videos that are recommended for my users.
The following code shows only a user's video.
<?php $query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
?>
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php } ?>
I want to show this section only one time.
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
Anyone can help me in this regard ?
The easiest way would be with a counter, like this:
<?php
$query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
$counter = 1;
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
if($counter == 1){
$counter++;
echo '<div class="onerilent"><img src="'.$user_avatar.'">'.$user_name.' Recommended for you</div>';
}
?>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php
}
?>
note the $counter variable is set to 1 before the loop, and inside the loop there is a condition to check if it is set to the value 1, and if it is, then it echo's your html and increments the $counter, so that it is no longer set to 1