Support » Fixing WordPress » Can this PHP be edited to display categories?

  • 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 = '';
    $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 at the following URL:  ?>
    <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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Cal


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

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

    Still unresolved issue if anyone has any ideas 🙂

    Anyone know of a way?

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

    Can you explain how I’d try that? Sorry, I don’t know php 🙁




    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.

    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/ on line 60

    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.

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




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




    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 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Can this PHP be edited to display categories?’ is closed to new replies.