WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Function for neighbouring posts, broken by 3.1-beta1 (6 posts)

  1. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    Until now I was using this neat little code snippet in order to display neighbouring posts (i.e. posts inside the same category as the current one).

    It was made from code taken here:
    http://forum.bytesforall.com/showthread.php?t=1491

    And looked like that, and did a perfect job:

    <?php
    if (is_single( )) {
    	$post_ID = $wp_query->posts[0]->ID;
    	$all_cats_of_post = get_the_category($post_ID);
    	for($i = 0; $i < sizeof($all_cats_of_post); $i++) {
    	// dont show the cat_ID 8 and 10
    	$categories = array(8, 10);
    	if(!in_array($all_cats_of_post[$i]->cat_ID, $categories)) {
    	 ?>
        	<div id="category-posts-<?php echo $all_cats_of_post[$i]->cat_ID; ?>" class="post-categories">
    			<h3><a href="<?php bloginfo('url'); ?>/?cat=<?php echo $all_cats_of_post[$i]->cat_ID; ?>"><?php echo $all_cats_of_post[$i]->cat_name; ?></a></h3>
    				<ul class="article-list">
    					<?php global $post; $cat_posts = query_posts(array(
    			'category__in' => array($all_cats_of_post[$i]->cat_ID),
    			//'tag_slug__in' => tag1+tag2,
    			'orderby'=>date,
    			'posts_per_page'=>5,
    			'order'=>DESC,
    					));
    					foreach($cat_posts as $post) :
    					?>
    
                        <li><a href="<?php the_permalink(); ?>" class="post-<?php echo $post->ID ?>"><?php the_title(); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
    		</div>
    		<?php } ?>
    	<?php } ?>
    <?php } ?>

    I'm running now this template on a 3.1-beta1 test site, and I notice that this snipped doesn't output anything at all. It doesn't give any error message either.

    The resulting HTML code from the function above:

    <div id="category-posts-" class="post-categories">
     <h3><a href="http://example.com/?cat="></a></h3>
      <ul class="article-list">
      </ul>
    </div>

    Any idea what's going on? An unpatched 3.1-beta bug, or a deprecated function?

  2. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    As an aside, it would be great if we could find in the Codex documentation more concrete examples of "how to get things done", which would be trusted methods that won't become invalidated by updates.

    Examples of such functions that come to my mind:

    - Displaying image-attachments in a custom manner. The best sample code that I found so far is not in the codex, but in this blog: http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/

    - Creating a simple breadcrumb trail. The solution I used until now was based on this: http://dimox.net/wordpress-breadcrumbs-without-a-plugin/ -- it doesn't work in the latest 3.1-beta.

    - Displaying posts that share the same parent categories as the current post. The solution I used until now: see above. Broken by 3.1-beta.

    In other words: I certainly do appreciate the diversity of the WordPress eco-system, but sometimes I wish there would be a more centralized "cook-book" where theme developers can look for tested, documented and reliable recipes, rather than finding 10 different custom solutions that may or may not be future-proof...

    Or maybe I should urge the authors of the above solutions to integrate their examples into the Codex, and see what happens ?

  3. demetris
    Member
    Posted 3 years ago #

    Just a quick observation:

    If I wanted to get the next/previous posts, I would try with built-in WP functions first. I would start with something like:

    $prev_in_cat = get_previous_post(true);
    $next_in_cat = get_next_post(true);

    Now I have the two adjacents posts in the same category, and can go on to do what I want with them. Say:

    if ($prev_in_cat) echo $prev_in_cat->ID . '<br />';
    if ($next_in_cat) echo $next_in_cat->ID . '<br />';

    get_previous_post() and get_next_post() have been in WP for quite a long time, they are documented both inline and in the Codex, and I can use them without worrying about compatibility.

    In general, when I don’t know if WP offers built-in functionality for something I want, I go, say, to:

    http://phpdoc.wordpress.org/trunk/

    ... and search if a function or API exists already; function and class names are self-explanatory in most cases.

  4. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    Thanks Demetris for your observations. The built-in "next/prev" function is useful indeed, and your little tweak above could be very practical. But in some situations, such as a CMS-like site that strongly relies on categories, it's necessary to be able to show a complete category listing.

    I wasn't aware of the phpdoc subsite, so thanks for letting me know!

    Regarding the WP 3.1 issues I mentionned, it looks like they are both linked to modifications affecting the get_category function... see http://core.trac.wordpress.org/ticket/15442 to follow the discussion.

    So I guess we can only wait and see.

  5. demetris
    Member
    Posted 3 years ago #

    Aha! That explains it. Thanks for pointing the ticket out.

    The broken functionality is documented functionality, so, I suppose, a fix will be provided sooner than later.

    Cheers!

  6. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    Happy to see that the issues are already fixed in today's 3.1-beta1-16884 !

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.