Display latest post by category

Hi, i’m trying to display the most recent post from a particular category in a carousel on my homepage. I have three categories with an id of 1, 2 & 3, which are also connected to post_id.

By the way I am a noob. I followed a guide to build my own php blog. I have learned a bit about php but not enough to figure this out.

The function I am using to get a post by category is

<?Php
function getPublishedPostsByCategory($category_id) {This text will be hidden
	global $conn;
	$sql = "SELECT * FROM posts ps 
			WHERE ps.id IN 
			(SELECT pt.post_id FROM post_category pt 
				WHERE pt.category_id=$category_id GROUP BY pt.post_id 
				HAVING COUNT(1) = 1)";
	$result = mysqli_query($conn, $sql);
	// fetch all posts as an associative array called $posts
	$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);

	$final_posts = array();
	foreach ($posts as $post) {
		$post['category'] = getPostCategory($post['id']); 
		array_push($final_posts, $post);
	}
	return $final_posts;
}

?>

My homepage html for the carousel is

<?php 
$posts="category_id";
$categorypost_id="post_id";
$posts=array();


	// Get posts under a particular topic
	if (isset($_GET['category'])) {
		$category_id = $_GET['category_id'];
		$posts = getPublishedPostsByCategory($category_id);
	}
?>
<html>
<div class="container marketing" id="services">
            <h1>Services</h1>
            <p>Take a look at our services.</p>
            <div class="responsive-4-items">
              <div style="padding: 10px; box-sizing: border-box">
                <?php foreach ($posts as $post): ?>  
        <div class="card"> 
        <img class="card-img-top" src="<?php echo BASE_URL . '/img/' . $post['image']; ?>" class="post_image" alt="post image" style="width:100%;margin-bottom:0">
       <div class="card-body"> 
       <h4 class="card-title" style="margin-top:0"><?php echo $post['title'] ?></h4>
      <p class="card-text">Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo.</p>
       <a href="post.php?post-slug=<?php echo $post['slug']; ?>" class="btn btn-primary">View Post</a>
       <div  class="card-footer  text-muted"href="#"></div>
              </div>
              </div>
              </div><?php endforeach ?>
              </div>
</html>

It doesn’t seem to be working, can anyone help? Thanks in advance.

How is it not working? What is (not) happening? Do you get any errors?

No errors, the post just isn’t displaying. There is a white space where the post should be though, so i’m guessing there is something wrong in the code.

I have done the same thing to display the most recent post using

The function

<?php 
/* * * * * * * * * * * * * * *
* Returns all published posts
* * * * * * * * * * * * * * */
function getPublishedPosts() {
	// use global $conn object in function
	global $conn;
	$sql = "SELECT * FROM posts WHERE published=true ORDER BY id DESC LIMIT 1";
	$result = mysqli_query($conn, $sql);
	// fetch all posts as an associative array called $posts
	$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);

	$final_posts = array();
	foreach ($posts as $post) {
		$post['category'] = getPostCategory($post['id']); 
		array_push($final_posts, $post);
	}
	return $final_posts;
}
?>

The HTML

<?php $posts = getPublishedPosts(); ?>

<html>
<div class="container marketing" id="services">
            <h1>Services</h1>
            <p>Take a look at our services.</p>
            <div class="responsive-4-items">
              <div style="padding: 10px; box-sizing: border-box">
                <?php foreach ($posts as $post): ?>  
        <div class="card"> 
        <img class="card-img-top" src="<?php echo BASE_URL . '/img/' . $post['image']; ?>" class="post_image" alt="post image" style="width:100%;margin-bottom:0">
       <div class="card-body"> 
       <h4 class="card-title" style="margin-top:0"><?php echo $post['title'] ?></h4>
      <p class="card-text">Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo.</p>
       <a href="post.php?post-slug=<?php echo $post['slug']; ?>" class="btn btn-primary">View Post</a>
<?php if (isset($post['category']['name'])): ?>
			<a 
				href="<?php echo BASE_URL . '/filtered_posts.php?category=' . $post['category']['id'] ?>">
				<type="button" class="btn btn-outline-info">
				<?php echo $post['category']['name'] ?>
			</a>
		<?php endif ?>
       <div  class="card-footer  text-muted"href="#"></div>
              </div>
              </div>
              </div><?php endforeach ?>
              </div>
</html>
Sponsor our Newsletter | Privacy Policy | Terms of Service