Latest Posts (of a specific category) on a Static Front Page?
-
I’m using the built in support for a Static Frontpage in WordPress 2.2 – in this previous post we discussed how to include a list of latest posts on the static front page — and that’s fine.
Now I want to amend this code so that on the static front page, the latest posts that it displays are ones that have a specific category attached – e.g. a “latest news” category.
I guess I have to add another statement to the SQL query (using JOIN??) that pulls out the relevant category id from wp_post2cat, but I’m new to SQL queries, and I’m not sure how to combine that with the wp_posts query I’m currently using!
Can anyone hack the following code to something as I’ve described above??
<?php /*Turn off error reporting.*/ error_reporting(0); /*Configure your database connection*/ $dbhost = 'localhost'; $dbuser = '******'; $dbpass = '******'; /*Connect to the database.*/ $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('There was an error connecting to the database.'); /*Select your WordPress database.*/ $dbname = 'infobridge'; mysql_select_db ($dbname); /*Get data from the database*/ //Use the $howmany variable to define how many entries you want to be displayed $howmany = 5; $result = mysql_query("SELECT * FROM wp_posts WHERE <code>post_type</code>=\"post\" and <code>post_status</code>= \"publish\" ORDER BY post_date desc LIMIT ". $howmany); /*Display the data*/ while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { //Date function $datevalue = $row['post_date']; $dateArray=explode('-',$datevalue); // Example of results // $dateArray[0]= 2007 // $dateArray[1] = 02 // $dateArray[2] = 05 ?> <p class="date"> <?php echo date('F j, Y', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0])); echo " "; // result: Feb 2, 2007 // Explode (Time) Code found on PHP.net at the following URL: https://www.php.net/manual/en/function.date.php#72953 ?> <h2 class="title"> <a href="<?php echo "{$row['guid']}";?>" title="Permalink to: <?php echo "{$row['post_title']}";?>"> <?php echo "{$row['post_title']}";?></a> </h2> <div class="excerpt"> <?php echo "{$row['post_excerpt']}";?> </div> <?php } //Free the memory and then mysql_free_result($result); //Close the database connection. mysql_close ($conn); ?> </div>
Many thanks for any help or pointers ??
- The topic ‘Latest Posts (of a specific category) on a Static Front Page?’ is closed to new replies.