• Resolved Nick Wingfield

    (@nick-wingfield)


    I’ve had to take over a WordPress site someone else has created. It’s a bit weird – the home page is largely controlled by the header.php!

    Anyway the header.php page currently has the following code that shows titles for the latest 7 posts.

    Trouble is I want to limit the list to only certain categories.

    Lets say the full range of categories are:

    Category: Apples (ID: 1)
    Category: Oranges (ID: 2)
    Category: Pears (ID: 3)

    Below is the current code that shows titles of all categories.
    How can I make it so that the homepage only shows titles for Apples and Oranges?

    <?php
    	@mysql_pconnect( $dbhost, $dbuser, $dbpassword, true );
    	$result = mysql_query("SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish'  ORDER BY post_date DESC LIMIT 0,7");
    	while($row = mysql_fetch_array($result)) {
    		echo '<li><a href="'.$root_site.''.$row['post_name'].'" title="">'.$row['post_title'].'</a></li>';
    		}
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • You should use wp_query to access your posts/pages

    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Thread Starter Nick Wingfield

    (@nick-wingfield)

    Thanks Konstantinos

    I’ve looked at the WP Query page you recommended and tried to make sense of it. Perhaps I should have said – I’m not a programmer, just a humble designer trying to muddle through.

    Are you suggesting that I should ditch the code above and replace it from something from the WP Query page?

    I’ve looked at it all but I can’t figure out where to start.

    I can see the section that shows how to exclude posts belonging to a category. the example they give is:

    $query = new WP_Query( 'cat=-12,-34,-56' );

    So if I wanted to exclude my category of Pears (ID:3), presumably it would be:

    $query = new WP_Query( 'cat=-3' );

    I’ve tried incorporating that into my existing code so it becomes:

    <?php
    	@mysql_pconnect( $dbhost, $dbuser, $dbpassword, true );
    	$result = mysql_query("SELECT * FROM wp_posts WHERE post_type='post' AND 'cat=-3' AND post_status='publish'  ORDER BY post_date DESC LIMIT 0,7");
    	while($row = mysql_fetch_array($result)) {
    		echo '<li><a href="'.$root_site.''.$row['post_name'].'" title="">'.$row['post_title'].'</a></li>';
    		}
    ?>

    But it doesn’t work. My list of 7 post titles just goes blank.

    Can anyone help?

    Try something like:

    <ul>
    <?php
    $result = new WP_Query();
    $result->query('cat=-3');
    while ($result->have_posts()) : $result->the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    Thread Starter Nick Wingfield

    (@nick-wingfield)

    Thanks for getting back to me Konstantinos. I really appreciate it.

    I’ve pasted your code and it nearly works. Category 3 is now eliminated. Only trouble is that your code doesn’t limit the titles to 7 posts.

    I went back to WP Query determined to see if I could fix it myself this time. I found the example:

    $query = new WP_Query( 'posts_per_page=7' );

    Then I tried every combination I could think of to add it to your code. I tried taking this bit:

    'posts_per_page=7'

    and placing it within each set of brackets in turn. Nothing worked. Then I took the whole line of code and added it as indicated:

    <ul>
    <?php
    $result = new WP_Query();
    $result->query('cat=-3');
    $result->query('posts_per_page=7');
    while ($result->have_posts()) : $result->the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>

    This succeeded in reducing the posts to 7 BUT Category 3 was no longer eliminated.

    Sorry for hassling you again Konstantinos – but what should I be doing?

    Try combining the two queries into one:

    <ul>
    <?php
    $result = new WP_Query();
    $result->query('cat=-3&posts_per_page=7');
    while ($result->have_posts()) : $result->the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    Thread Starter Nick Wingfield

    (@nick-wingfield)

    Brilliant! That did it!

    Many many thanks Konstantinos

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘What is the PHP code to limit post categories on home page?’ is closed to new replies.