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

    <?php
    /*Turn off error reporting.*/
    error_reporting(0);
    
    /*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'];
    	$dateArray=explode('-',$datevalue);
    	// 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>
    </div>
    
    <p><?php echo "{$row['post_excerpt']}";?></p>
    
    <?php }
    //Free the memory and then
    mysql_free_result($result);
    //Close the database connection.
    mysql_close ($conn);  ?>
    </code>

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

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

    (@themesbycal)

    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 🙁

    whooami

    (@whooami)

    Member

    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');
    query_posts('showposts=1');
    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>
    </p>
    <?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/domain.co.uk/html/site/index.php 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.

    whooami

    (@whooami)

    Member

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

    whooami

    (@whooami)

    Member

    heres an even simpler example:

    <?php require('./wp-blog-header.php');
    if (have_posts()) : while (have_posts()) : the_post();
    the_title();
    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.