WordPress.org

Forums

Article count on external index.php? (15 posts)

  1. mhw
    Member
    Posted 6 years ago #

    Hey everyone,

    So here goes...I have a website (www/index.php) and a blog (www/blog/index.php). Say my blog has 55 articles on it, would it be possible to show "55" on www/index.php? If I connected to the database in www/index.php, is there any kind of code that can be used to display the blog's articles?

    Also if it's possible to do this with the number of overall comments on the blog too that would be great.

    Not in any rush, thanks everyone =)

  2. darrinb
    Member
    Posted 6 years ago #

    Assuming your index.php file is not generated by WP, here's a very quick script to pull the number of posts in the WP database (excludes password-protected posts).

    <?php
    // ...Call the database connection settings
    require( path to /wp-config.php );
    
    // ...Connect to WP database
    $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if ( !$dbc ) {
        die( 'Not Connected: ' . mysql_error());
    }
    // Select the database
    $db = mysql_select_db(DB_NAME);
    if (!$db) {
        echo "There is no database: " . $db;
    }
    
    // ...Formulate the query
    $query = "
        SELECT *
        FROM `wp_posts`
        WHERE `post_status` = 'publish'
        AND `post_password` = ''
        AND `post_type` = 'post'
        ";
    
    // ...Perform the query
    $result = mysql_query( $query );
    
    // ...Check results of the query and terminate the script if invalid results
    if ( !$result ) {
        $message = '<p>Invalid query.</p>' . "\n";
        $message .= '<p>Whole query: ' . $query ."</p> \n";
    	die ( $message );
    }
    
    // Init a variable for the number of rows of results
    $num_rows = mysql_num_rows( $result );
    
    // Print the number of posts
    echo "$num_rows Posts";
    
    // Free the resources associated with the result set
    if ( $result ) {
        mysql_free_result( $result );
        mysql_close();
    }
    ?>

    I use something similar to pull the most recent post from my blog and display a snapshot of it on my home page.

  3. mhw
    Member
    Posted 6 years ago #

    Thanks for that, it should really come in useful :)

    Would you be able to supply me with the code for the most recent too? - If it's not too much trouble? Just the title of the article would suffice, I have no need for the rest =)

  4. darrinb
    Member
    Posted 6 years ago #

    You could use something along these lines:

    <?php
    // Call the database connection settings
    require( path-to- /wp-config.php );
    
    // Connect to WP database
    $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if ( !$dbc ) {
        die( 'Not Connected: ' . mysql_error());
    }
    // Select the database
    $db = mysql_select_db(DB_NAME);
    if (!$db) {
        echo "There is no database: " . $db;
    }
    
    // Formulate the query
    	$query = "
    		SELECT post_title,post_content,UNIX_TIMESTAMP(post_date) AS post_date_unix
    		FROM `wp_posts`
    		WHERE `post_status` = 'publish'
    		AND `post_password` = ' '
    		AND `post_type` = 'post'
    		ORDER BY `wp_posts`.`post_date` DESC
    		";
    
    // Perform the query
    $result = mysql_query( $query );
    
    // Check results of the query and terminate the script if invalid results
    if ( !$result ) {
        $message = 'Invalid query.' . PHP_EOL;
        $message .= 'Whole query: ' . $query . PHP_EOL;
    	die ( $message );
    }
    
    /*
     * QUERY RESULT VARIABLES
     */
    
    // Count the number of rows returned by the query
    $num_rows = mysql_num_rows( $result );
    
    // Use the results of query
    $row = mysql_fetch_array( $result, MYSQL_ASSOC );
    
    // Init var for DATE of the post
    $post_date = date( "l, F jS, Y ", $row['post_date_unix'] );  
    
    // Init var for TITLE of the post
    $post_title = $row['post_title'];
    
    // Init var for CONTENT of the post
    $post_content = $row['post_content'];
    
    // Free the resources associated with the result set
    if ( $result ) {
        mysql_free_result( $result );
        mysql_close();
    ?>

    You now have variables for the most recent post:
    Post Title: $post_title
    Post Date: $post_date
    Post Content: $post_content

    as well as the total number of posts:
    Number of posts: $num_rows

    Example use:

    The total number of posts is <?php echo $num_rows; ?>.
    
    The most recent post:
    
    <h2><?php echo $post_title; ?></h2>
    <p class="post-meta">Posted: <?php echo $post_date; ?>
    
    <?php echo $post_content; ?>

    Again, this is just a quick look over. You can use this script instead of the one I supplied earlier as this one provides you with all the variable needed.

  5. darrinb
    Member
    Posted 6 years ago #

    err, forgot the closing </p> tag.

    It should read:
    <p class="post-meta">Posted: <?php echo $post_date; ?></p>

  6. mrmist
    Forum Janitor
    Posted 6 years ago #

    For info, you can also do

    require_once('./blog/wp-blog-header.php');

    And then you can access any of the WordPress functions within your page.

  7. darrinb
    Member
    Posted 6 years ago #

    @mrmist: Thanks for that tidbit of info! Now I have to re-think a whole bunch of things! :)

    @mhw: The script I supplied should work (I tested it on my own site), but now that I have this info from mrmist, there is a much neater script that can be written. I'll post it later!

  8. darrinb
    Member
    Posted 6 years ago #

    Ok, so this ridiculously short script will get you a post count (excludes any private posts, includes password-protected posts):

    <?php
    require_once('./directory-where-WP-is-installed/wp-blog-header.php');
    $count_posts = wp_count_posts();
    $published_posts = $count_posts->publish;
    ?>

    Example:

    There are currently <?php echo $published_posts; ?> posts.

    And this script will return the post title (and permalink) of the most recent post:

    <?php  query_posts('showposts=1'); ?>
        <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
            <?php endwhile; ?>
            <?php else : ?>
    <?php endif; ?>

    If you're using both of these on the same page, then you only have to call require_once('./directory-where-WP-is-installed/wp-blog-header.php'); one time.

    @mrmist Thanks so much for the heads-up it really neatened up a lot of external page scripts!

  9. mhw
    Member
    Posted 6 years ago #

    Thanks a million for the code dbmartin! :D

    It's helped me a great deal =]

  10. mhw
    Member
    Posted 6 years ago #

    Okay guys, I am using this code to display the most recent title on my external page:

    <?php
    require_once('wp-blog-header.php');
    $count_posts = wp_count_posts();
    $published_posts = $count_posts->publish;
    ?>
    
    <?php  query_posts('showposts=1'); ?>
        <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a>
            <?php endwhile; ?>
            <?php else : ?>
    <?php endif; ?>

    It works a treat! My new problem is that I need to cut down the title so it will fit into the small area I need it to. I did a bit of research and somebody told me to add this code:

    echo strlen($text > 125) ? substr($text, 0, 125) . '...' : $text;

    I haven't a clue where to add it. Can anyone help?

    Thanks guys.

  11. mhw
    Member
    Posted 6 years ago #

    Nobody? :'(

  12. darrinb
    Member
    Posted 6 years ago #

    Basically what that script is saying is "If the length of $text is longer than 125 characters, cut it to 125 characters and add '...' to the end. If it isn't (longer than 125 characters) then show all of it."

    The "problem" with arbitrarily cutting your title down to 125 characters is that it's going to chop off words mid-word.

    Another possibility is styling the link (that's what this is:
    <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a>) to fit in allowed space using your CSS style sheet.

    Can you provide a link to the site you're talking about? It would help if I could see it.

  13. mhw
    Member
    Posted 6 years ago #

  14. leighring
    Member
    Posted 6 years ago #

    Hey! Great code dbmartin, one question...how can I add the excerpt of the post to be pulled in just below the post title?

    Thanks :)

  15. freese
    Member
    Posted 5 years ago #

    Thanik you very much dbmartin!!
    Your post has been really useful to me. I have but another question tough. For a project at the company I work, I need a Script that shows up the search result count of a wordpress blog, outside of it. What I mean is in a separate website, that is used as a metasearch engine, I have to show the search result number from a wordpress blog. How could I modify your script for pulling the number of posts in the WP database so that it pulls the search count?

    Of course the query:

    // ...Formulate the query
    $query = "
        SELECT *
        FROM <code>wp_posts</code>
        WHERE <code>post_status</code> = 'publish'
        AND <code>post_password</code> = ''
        AND <code>post_type</code> = 'post'
        ";

    hast to be different, but I´m really not a php master. Thank you very much for your support!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.