• Resolved loudliger

    (@loudliger)


    I created a custom post type, “Testimonials” so that my client can easily add new testimonials as she receives them from her clients. By using the “post snippets” plugin, I have created a shortcode to display each testimonial by specifying the post ID like this: [testimonial id=”100″] By simply filling in the client’s name in the title, position in the custom fields, and testimonial in the content area, the shortcode then creates a properly styled testimonial box for her. However I am having trouble getting the content to appear the way I would like. I am by no means a PHP expert, but I am having trouble figuring out what I’ve missed here.

    The following code does work:

    echo "<div class=\"boxbox\">";
    echo "<div class=\"box\">";
    $title = get_the_title('{id}');
    echo "<span style=\"color:#060c59\"><strong>$title</strong></span>";
    echo "<br/>";
    $position = get_post_meta('{id}', 'position', true);
    echo "<span style=\"text-decoration: underline\"><em>$position</em></span>";
    echo "<br/><br/>";
    
    // This section is a compromise but it works:
    $post_id = {id};
    $queried_post = get_post($post_id);
    $content = $queried_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;
    // End of the section
    
    echo "</div>";
    echo "</div>";

    The above displays the testimonial, but if I understand correctly, by using the_content() I should be able to display a “Read More” link on especially long ones. I would like to replace my “compromise” section with something the following:

    query_posts( 'p={id}' );
    global $more;
    $more = 0;
    while (have_posts()) : the_post();
    the_content( 'Read more »' );
    endwhile;
    wp_reset_query();

    But it doesn’t work. Instead, the content doesn’t display at all. I also tried the same method with WP_Query. Is my PHP wrong, or is it something about the plugin? Is there any way I can get this to work? Any help would be greatly appreciated. Thanks.

    http://ww.wp.xz.cn/extend/plugins/post-snippets/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter loudliger

    (@loudliger)

    The page in question: http://ccrumb.org/testimonials/

    Hi,

    If I remember correctly, for “Read More” to work in WP you need to instruct it where you want the read more link to appear. ie, where to make the break in the post. You can do that by inserting a more tag in your post.
    <!--more-->

    Have you done that? If not, that would be a place to start and see if it shows up.

    Thread Starter loudliger

    (@loudliger)

    Thank for the quick reply. Yes, I have done that and it made no difference. The problem, however, isn’t just that the read more link won’t appear. When I use the loop, the entire content area doesn’t appear. There isn’t even any html there when I inspect with chrome or firebug.

    For purposes of demonstration, I’ve set up a new page: http://ccrumb.org/demo/

    The column on the left contains the shortcodes I’m currently using, with the “compromise” code. The column on the right contains another shortcode using the code that I’d like to use, with the loop.

    Thread Starter loudliger

    (@loudliger)

    Polite bump on this one. I really want to figure out a solution and could use some help on it. Thanks in advance.

    Hi,

    I’d like to point you in the right direction, and as I see it you have two options. If you go with your first snippet of code, you can use post_extract instead of post_content, and use the extract field in the posts for the short version of your testimonial when it’s a long one. And have a condition that checks if a post extract exist, and if not, use post_content as before.

    But going that route, means more work as you have to add content both to the post content and to the post extract when inserting testimonials. In the long run it’s of course quicker and more elegant to just have to insert a read more tag.

    So that’s where your second snippet of code comes into place. What you want is nested loops, as you are already within a loop. WordPress do support multiple loops -> to have a loop within a loop, and you can find some examples here to get you going.

    http://codex.ww.wp.xz.cn/The_Loop#Multiple_Loops

    Anyway, as your question is more of a general “how to develop something for WordPress”-question than a specific support question for this plugin, you should probably try to ask about nested loops in the more general forums here if you need more code examples / a more elaborate answer how to deal with that.

    I hope this help you though.

    Cheers,
    Johan

    Thread Starter loudliger

    (@loudliger)

    Thanks for your help. I recognize that it’s a bit of a broader question than applies strictly to this plugin, but I wasn’t sure if was my code or the plugin that was causing the problem.

    You did point me in the right direction, although I couldn’t get nested loops to work in the shortcode no matter what I did. It’s annoying because I’m pretty sure I’ve got the code right. It’s not that complicated. Still, whenever I use a nested loop of any sort in the shortcode, it doesn’t display anything. I’m sure there’s a reason for this, and I’m still a newbie at PHP. I just wish I understood what was going wrong.

    Anyway, what did end up working was setting up a condition with the post_excerpt method you suggested. It might even work better for the client this way, because now instead of inserting the more tag, she can just copy the section she wants to appear to the excerpt field. Thanks for taking the time to look at my question, and for developing a great plugin that I will use a lot.

    Sweet! I am happy it worked out for you!

    Cheers,
    Johan

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

The topic ‘Displaying custom posts with "read more" in a shortcode’ is closed to new replies.