Forum Replies Created

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter vossavant

    (@vossavant)

    I found the solution. The code below will do the trick:

    <?php
    	// grab 4 random categories
    	$cats 		= '';
    	$categories	= get_categories();
    	$rand_keys 	= array_rand($categories, 4);
    	foreach ($rand_keys as $key) {
    		$cat = $categories[$key]->term_id;
    		$nam = $categories[$key]->cat_name;
    
    	// grab the latest post from each of the 4 random categories
    	$latest = new WP_Query("cat=$cat&amp;showposts=1"); 
    
    	while($latest->have_posts()) : $latest->the_post();
    ?>
    
    <small>Latest in <a href="#"><?php echo $nam; ?></a></small>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    
    <?php endwhile; ?>
    <?php } ?>

    Seeing as how this was last posted to 9 months ago, maybe something new has happened.

    I too would like to be able to modify a live post or page and then preview the changes before I make those changes live.

    I’ve done a lot of searching but haven’t found anything on this. Can anyone at WordPress or in the community speak to this?

    John,

    I posted a thank you on your blog for your insight on this. I was looking for more control over the output of the number of posts for each category. Instead of the default output:

    Uncategorized (2)

    I wanted something like this:

    Uncategorized // 2 posts

    After looking at your code I came up with this:

    foreach (get_categories(array('hide_empty'=>false)) as $category)
    {
    	echo '<li><a href="' . get_bloginfo('wpurl') .
    	'/category/' . $category->category_nicename . '/">' .
    	$category->cat_name . '</a> // ' . $category->count .
    	'posts</li>';
    }

    Simply drop this code in between some <ul> tags where you want your category list to appear and you’re set. It’s also possible to wrap the “number of posts” piece in a <span> or similar tag to style it differently.

    If you are content with showing it like this:

    Category (x)
    Category (y)

    Where x and y are the number of posts in the category, then just be sure to include the “show_count” parameter in the wp_list_categories function:

    <?php wp_list_categories('show_count=1'); ?>

    As for displaying the posts in a fancier way, paste the following snippet of code where you want to show your categories:

    <ul>
    foreach (get_categories(array('hide_empty'=>false)) as $category)
    {
    echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' . $category->cat_name . '</a> (' . $category->count . ' posts)</li>';
    }
    </ul>

    This will output:

    Category Name (X posts)

    You can wrap the (X posts) piece in a <span> tag or other HTML tag if you want to style it separately. In my case, I was looking to position the category name to the left and the number of posts to the right.

    Special thanks to John Herren for this one.

    vossavant

    If this topic still needs an answer, try this. No plug-in required, and deceptively simple. Just add this line of code before your while loop:

    $hit_count = $wp_query->found_posts;

    For example, my search results page looks like this:

    <?php if (have_posts()) : ?>
    
    <?php $hit_count = $wp_query->found_posts; // count # of search results ?>
    <p>Your search for <b>'<?php the_search_query(); ?>'</b> returned <?php echo $hit_count . ' results'; ?></p>

    .
    .
    .
    (while loop, etc.)

    I know this topic got dropped a while ago, but I found an answer:

    To count the total number of search results for a search that has paginated results, simply add this above your loop:

    $hit_count = $wp_query->found_posts;

    For example, my search results page looks like this:

    <?php if (have_posts()) : ?>
    
    <?php $hit_count = $wp_query->found_posts; // count # of search results ?>
    <p>Your search for <b>'<?php the_search_query(); ?>'</b> returned <?php echo $hit_count . ' results'; ?></p>

    .
    .
    .
    (while loop, etc.)

    munzli,

    Your solution worked for me. However, instead of selecting a “Posts page” from the WordPress Options->Reading menu, I use a custom page template that lists posts from a single category. The page ID for this is 58, so I modified your code to read:

    if ($page->ID == $current_page || ($current_page == 0 && $page->ID == '58'))

    This change (on line 512 of classes.php) saved me a headache. Thanks so much.

    btw – running version 2.2.3

    Thread Starter vossavant

    (@vossavant)

    The error went away, and God knows why. I suspect it might have been a conflict with Lutz Schroeer’s Pagebar plug-in, which I swapped out in favor of the superior Pagebar Navigation plug-in by Andy Staines.

    In that last bit of code, the only change is from

    $post->ID

    to

    $post->post_parent

    Going off of sojweb’s last bit of code, I came up with:

    echo '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';

    This does the trick of displaying the parent page’s title if you are on a subpage of the parent or on the parent itself.

    Alternatively, you can make it a heading by swapping in <h2> tags for the <a> tags.

Viewing 10 replies - 1 through 10 (of 10 total)