• Resolved landscribe

    (@landscribe)


    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 ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • hey, you might want to try this:

    i saw your post about “nothing shows up”, you forgot to put the the_content(); . ppl show you the loop but forgot to tell you have to put the results hehe. my code show 1 post but you can change it.

    you dont have to use plugin to have a static/cms page. i can do it with the loops.

    so this is a working code from my site:

    <?php
    $posts = get_posts('numberposts=1');
    foreach ($posts as $post) :
    ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h3>
    <div id="Boxcontent"><small><?php the_time('F jS, Y') ?></small></div>
    <div id="Boxcontent">
    <?php the_excerpt(); ?></div>
    <div id="Boxcontent" style="text-align:right; font-size:10px;">Posted in <?php the_category(', ') ?><?php edit_post_link(' | Edit','',''); ?> <a href="<?php comments_link('No Comments »', '1 Comment »', '% Comments »'); ?>">| Add comment </a></div>
    
    <?php endforeach;?>

    edit: get from a specific cat:

    <?php
    $posts = get_posts('numberposts=6&category=2');
    foreach($posts as $post) :
    ?>
    <div class="Box"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><small><?php the_time('F jS, Y') ?></small></div>
    <div> <?php the_content(); ?></div>
    <?php endforeach; ?>

    cheers ??

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    ??

    I’m confused as to why anybody would do something like manually selecting posts from the database like that. It’s crazy. If you’re using a static frontpage with the built in WordPress static page functionality, then all you need is to create a custom page template.

    1. You make a page template with a Loop in it. If you want it to have custom cats, you include a specific query_posts() call to get those.
    2. You make a new Page that uses that template.
    3. You select that to be your “Front Page” on the Options settings.

    That’s it. You don’t need a whole separate PHP file to actually connect to the database and such. The only reason you’d need that is to get the WordPress data outside of WordPress entirely. Which is major overkill in almost all cases.

    Thread Starter landscribe

    (@landscribe)

    Well, here’s the reason Big Square Blog gives for writing a custom query:

    Last week I brought to you a post on how to add a WordPress Mini-Loop to your static front page. I didn’t realize it at the time, but after some thought, I must say that there were some serious problems with that solution. For starters, the static front page of Big Square Dot was running noticeably slower after implementing the Mini-Loop. Secondly, and most importantly, I could not use the WP-Cache plugin with the above code.

    I’ve done a quick test of cam_oai‘s loop and I think it will work great with a bit of tweaking the CSS. Cheers.

    how come some of the code like the one above the read more doesn’t appear to work

    i tried the < ?php the_content(__(‘Read More »’)); ? > but instead of separating the content with a read more link it just displays the entire post

    is there another way to do it ?

    [ RESOLVED: I was adding $more=0 in the wrong place, needs to be after while statement. ]

    I’m using WordPress 2.3 and have set a custom page as the static front page. In the page template I have the following code to grab the last 5 posts:

    <?php
    $home_query = new WP_Query('showposts=5');
    while ( $home_query->have_posts() ) : $home_query->the_post()
    ?>
    
    stuff here including the_content(...)
    
    <?php endwhile ?>

    Now this works but the problem, like gspark, is that the read more link doesn’t work. I saw in another thread that adding $more=0 can fix this but it still doesn’t work.

    Anyone know how I can get the read more link working or is this a bug?

    Take a look at wp-includes/query.php starting at line 1494 so see when the $more value is set…

    Note: using Version 2.3.1

    wpitn2shape

    (@wpitn2shape)

    Mine brings up the PAGE text. Any help?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Latest Posts (of a specific category) on a Static Front Page?’ is closed to new replies.