Can this PHP be edited to display categories? (12 posts)

  1. k0k0pelli
    Posted 8 years ago #

    Hi Peeps,

    Am setting up a site and have managed to use some PHP to display a snippet of the blog posts on a static page within the site.

    What I really need to do is to somehow specify it to only show posts from certain categories.
    Could anyone help? The code I used is below..

    /*Turn off error reporting.*/
    /*Configure database connection*/
    $dbhost = 'internal-db.s12969.gridserver.com';
    $dbuser = 'xxxxxxx';
    $dbpass = '[stop looking!]';
    /*Connect to the database.*/
    $conn = mysql_connect ($dbhost, $dbuser, $dbpass)
    		or die
    		('There was an error connecting to the database.');
    /*Select WordPress database.*/
    $dbname = 'db12969_moments';
    mysql_select_db ($dbname);
    /*Get data from the database*/
    //Use $howmany variable to define how many entries to display
    $howmany = 2;
    $result = mysql_query("SELECT * FROM wp_posts WHERE <code>post_type<code>=\&quot;post\&quot; and</code>post_status
    = \"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'];
    	// Example of results
    	// $dateArray[0]= 2007
    	// $dateArray[1] = 02
    	// $dateArray[2] = 05
    	?> <p class="small_grey_text"> <?php
    	echo date('F j, Y', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]));
    	echo "</p>";
    	// result: Feb 2, 2007
    	// Explode (Time) Code found on PHP.net at the following URL: http://www.php.net/manual/en/function..php#72953  ?>
    <div class="hp_blog_title">
    <a>" title="<?php echo "{$row['post_title']}";?>">
    <?php echo "{$row['post_title']}";?></a>
    <p><?php echo "{$row['post_excerpt']}";?></p>
    <?php }
    //Free the memory and then
    //Close the database connection.
    mysql_close ($conn);  ?>

    Any help would be much appreciated.. many thanks in advance.

  2. Cal
    Posted 8 years ago #

    you may want to delete that dbuser and dbpass from your post

  3. k0k0pelli
    Posted 8 years ago #

    Phew, what an error! Thank you, have edited and changed all my database details .. thanks again.

    Still unresolved issue if anyone has any ideas :)

  4. k0k0pelli
    Posted 8 years ago #

    Anyone know of a way?

  5. triffid
    Posted 8 years ago #

    I don't know much about MySQL, but can you not just add a 'post_category' condition to your select?

  6. k0k0pelli
    Posted 8 years ago #

    Can you explain how I'd try that? Sorry, I don't know php :(

  7. whooami
    Posted 8 years ago #

    you are really going about doing that the hard way..

    if all you are looking to do is display a snippit ..

    this is all you need is something like this..

    <?php require('path/to/wp-blog-header.php');
    while (have_posts()) : the_post(); ?>
      <?php the_title(); ?>
      was just posted in
      <?php the_category(', ') ?>
      <a href="<?php the_permalink() ?>" rel="bookmark">Read More</a>
    <?php endwhile;

    Thats just a rough example - You can use the _excerpt to show snippits.

  8. k0k0pelli
    Posted 8 years ago #

    Thank you for the suggestion .. I tried it, but got this error message in the browser after upload:

    Fatal error: Call to undefined function: query_posts() in /home/12969/domains/domain.co.uk/html/site/index.php on line 60

  9. triffid
    Posted 8 years ago #

    I'd guess you (haven't and) need to change the 'path/to/wp-blog-header.php' in Whooami's example to the actual path to that file in your WordPress installation.

  10. k0k0pelli
    Posted 8 years ago #

    lol - yeah I did change it but got the above error message.

  11. whooami
    Posted 8 years ago #

    query_posts is available in 2.2. so I dont know why you would see that error.

  12. whooami
    Posted 8 years ago #

    heres an even simpler example:

    <?php require('./wp-blog-header.php');
    if (have_posts()) : while (have_posts()) : the_post();
    endwhile; else:
    endif; ?>

    (edit the path to wp-blog-header.php)

    My point in these is to show that in your first example you're sort of rebuilding the house. The foundation, atleast :)

Topic Closed

This topic has been closed to new replies.

About this Topic