WordPress.org

Ready to get started?Download WordPress

Forums

Semiologic terms2posts oustide of The Loop (9 posts)

  1. jalenack
    Member
    Posted 8 years ago #

    Hey,

    I'm having an odd problem with Semiologic's Terms2Posts plugin. I can only use the function inside the loop, even on single pages. What I'm trying to do is:

    <?php if (is_single()) { ?>
    <h3>Related Posts</h3>
    <?php sem_terms2posts(); ?>
    <?php } ?>

    Except that's not returning anything. So I dove into the code and made the function return instead of echo.

    Then, in my single.php template WITHIN the loop, I added <?php $related = sem_terms2posts(); ?>

    Then I went back to sidebar.php and added global $related; echo $related;

    And that works! Which seems strange to me. I tried declaring the global $post variable, but that turned up nothing from sem_terms2posts(). I know that's still available because print_r($post) prints the whole object with all its information intact.

    I narrowed the problem down to:

    if ( $terms && sizeof( $terms ) && function_exists('sem_search_posts') ) ... $terms is unavailable. So I dove into sem_extract_terms for a bit before giving up and posting here.

    So, any ideas?

  2. Kafkaesqui

    Posted 8 years ago #

    Would something like this work?

    <?php if (is_single()) { ?>
    <h3>Related Posts</h3>
    <?php
    global $post, $wp_query;
    $post = $wp_query->post;
    sem_terms2posts();
    ?>
    <?php } ?>

    I'm assuming sem_terms2posts() works off the post object.

  3. jalenack
    Member
    Posted 8 years ago #

    No beans. Yeah, it works off of the $post object. I know $post is available from where I need it, yet sem_terms2posts() isn't picking up on it.

  4. Denis de Bernardy
    Member
    Posted 8 years ago #

    tried using sem_terms2posts($wp_query->post); (arbitrary post)?

  5. jalenack
    Member
    Posted 8 years ago #

    <?php global $wp_query;
    if (is_single()) { ?>
    <h3>Related Posts</h3>
    <?php sem_terms2posts($wp_query->post); ?>
    <?php } ?>

    Still turns up nothing.

  6. Denis de Bernardy
    Member
    Posted 8 years ago #

    what's in var_dump($wp_query->post);?

  7. jalenack
    Member
    Posted 8 years ago #

    object(stdClass)(24) { ["ID"]=> string(3) "169" ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2005-08-18 01:27:50" ["post_date_gmt"]=> string(19) "2005-08-18 08:27:50" ["post_content"]=> string(990) "Greetings, I'm announcing Jalecode, a new blog dedicated to coding. In short, that means no more code will be appearing here. I'll be leaving Democracy,
    .....
    ["post_title"]=> string(19) "Announcing Jalecode" ["post_category"]=> string(1) "0" ["post_excerpt"]=> string(0) "" ["post_lat"]=> NULL ["post_lon"]=> NULL ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(19) "announcing-jalecode" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2005-08-18 01:27:50" ["post_modified_gmt"]=> string(19) "2005-08-18 08:27:50" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> string(1) "0" ["guid"]=> string(54) "http://blog.jalenack.com/archives/announcing-jalecode/" ["menu_order"]=> string(1) "0" ["post_view_count"]=> string(1) "0" }

    Looks good to me

  8. Denis de Bernardy
    Member
    Posted 8 years ago #

    sem_get_terms2posts has the following sig:

    sem_get_terms2posts($num_posts = 5, $my_post = null)

    looks like I forgot to give the same sig to sem_term2posts.

    let me know if the following works for you:

    echo sem_get_terms2posts(5, $wp_query->post);

    i'll take a look when i've time if it doesn't.

  9. jalenack
    Member
    Posted 8 years ago #

    I just upgraded and it works beautifully now. Great! Although I think there was a bug where there would only be max one result, because $html was being reset every time through the loop instead of being appended.

    Now, I've made some changes to suit me. I've addded some code to use/make excerpts of post contents. The code: http://phpfi.com/75595

    Basically it uses the post excerpt if there is one, and makes an excerpt of the first 10 words if not. Perhaps you could trick out the parameters to show excerpt or not. Thanks for the fantastic plugin!

Topic Closed

This topic has been closed to new replies.

About this Topic