• Hello,

    I am using the Relations custom field to display selected posts (from a blog) that are relative to the custom post type.

    How do I take this code:
    Select Relative Posts: <?php
    $my_array = get_custom_field(‘select_relative_posts’);
    foreach ($my_array as $item) {
    print $item;
    }
    ?>

    to spit out a loop of the three posts I selected to be displayed in my sidebar with a title, date, excerpt and featured image>

    Thank you so much for your assistance!

    Jason

    http://wordpress.org/extend/plugins/custom-content-type-manager/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    See http://code.google.com/p/wordpress-custom-content-type-manager/wiki/FAQ

    Specifically #2. You’ll want to use the http://code.google.com/p/wordpress-custom-content-type-manager/wiki/get_post_OutputFilter filter. Also note that WP stores the featured image as a hidden custom field named _thumbnail_id.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Actually, the GetPostsQuery part in there skips over hidden custom fields (i.e. those beginning with an underscore). You may have to use http://codex.wordpress.org/Function_Reference/the_post_thumbnail or http://codex.wordpress.org/Function_Reference/get_post_meta

    e.g.

    <?php
    
    $thumbnail_id =  get_post_meta($post_id, '_thumbnail_id', true);
    $thumb = CCMT::filter($thumbnail_id, 'get_post');
    
    print_r($thumb);  // this will show you all attributes available to you
    
    ?>

    Thread Starter kuhio61

    (@kuhio61)

    @fireproofsocks,

    Thanks for getting back to me so quickly. I looked at all the supplied links and I am not really seeing how these apply to me. Right now, the above code prints the post’s id’s. How do I use these ids from the custom fields to query three posts? I am lost here.

    Thanks.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    #2 from the FAQ applies exactly to your situation. Copied from the FAQ:

    “I used a Multi-Relation field to select 3 other posts. How do print links to them with title, thumbnail, etc?”

    The trick here is that the relation fields store only the post IDs of those related posts, and you don’t want the post’s ID, you want the post that the ID represents. In such a case, you’ll find the get_post Output Filter extremely helpful. Instead of just retrieving the link to the other post (like the to_link_href or to_link filters do), the get_post gets everything from the referenced post, leveraging the power of the GetPostsQuery::get_post function from the integrated Summarize Posts plugin.

    What does that mean? It means you can easily get the post’s title, date, and all of its custom fields.

    If it’s a Repeatable Field…

    If your custom field is a “repeatable” field that allows you to store multiple values, then you must first use the to_array filter to ensure that you retrieve an array of values. You can pass the name of a 2nd Output Filter and each member of the array will get filtered by this secondary filter.

    $my_posts = get_custom_field('select_relative_posts:to_array', 'get_post');
    foreach ($my_posts as $p) {
        print $p['post_title'];
        print $p['guid'];  // this is the permalink
        print $p['my_custom_field']; // any defined custom field is available
        print $p['_thumbnail_id'];  // This is where WP stores the "Featured Image"
        print_r($p); // this will show you EVERYTHING that's available in that post
    }

    Once we’ve got an array of post IDs, then all you have to do is come up with some way to look up the data represented by that ID. You can use WordPress’ own functions for this (e.g. get_permalink() or get_post(), but the CCTM’s get_post() function has the advantage of automatically retrieving all the custom fields, whereas with WordPress’ built-in functions, you’d have to use 2 separate functions: one to get the post, and another to get the post’s custom fields.

    Note that if you are trying to get the “Featured Image”, !Wordpress stores that in a hidden custom field named _thumbnail_id. See that? WordPress also stores only the ID of the image.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Actually, you should be able to print out “thumbnail_src” to get the thumbnail’s src.

    $my_posts = get_custom_field(‘select_relative_posts:to_array’, ‘get_post’);
    foreach ($my_posts as $p) {
    print $p[‘post_title’];
    print $p[‘guid’]; // this is the permalink
    print $p[‘thumbnail_src’]; // this is the src of the thumbnail (if a featured image is set)
    }

    Thread Starter kuhio61

    (@kuhio61)

    I used the repeating code and it was able to print this:

    Mayhem Blog 2http://localhost/lost/?post_type=mayhems_blog&p=2428Array ( [ID] => 24 [post_author] => 1 [post_date] => 2012-01-03 01:52:47 [post_date_gmt] => 2012-01-03 01:52:47 [post_content] => [post_title] => Mayhem Blog 2 [post_excerpt] => Excerpts are optional hand-crafted summaries of your c [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => mayhem-blog-2 [to_ping] => [pinged] => [post_modified] => 2012-01-03 02:21:33 [post_modified_gmt] => 2012-01-03 02:21:33 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/lost/?post_type=mayhems_blog&p=24 [menu_order] => 0 [post_type] => mayhems_blog [post_mime_type] => [comment_count] => 0 [ancestors] => Array ( ) [filter] => raw [_edit_last] => 1 [_edit_lock] => 1325641454:1 [_thumbnail_id] => 28 ) Post 4http://localhost/lost/?p=3728Array ( [ID] => 37 [post_author] => 1 [post_date] => 2012-01-03 05:36:12 [post_date_gmt] => 2012-01-03 05:36:12 [post_content] => test [post_title] => Post 4 [post_excerpt] => Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => post-4 [to_ping] => [pinged] => [post_modified] => 2012-01-03 05:36:12 [post_modified_gmt] => 2012-01-03 05:36:12 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/lost/?p=37 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 1 [ancestors] => Array ( ) [filter] => raw [_edit_last] => 1 [_edit_lock] => 1325568862:1 [_thumbnail_id] => 28 )

    How do I apply HTML elements to it so I can format it?

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    I can only advise you in the use of the CCTM — there are plenty of tutorials, books, sites out there to help you write HTML or PHP. PHP’s print_r() is a useful debugging tool that shows you the contents of an array.

    So if you do this:

    print_r($p);

    And you see this:

    Array ( [ID] => 24 [post_author] => 1 ...etc...

    Then you know you can do this:

    print $p['ID'];

    And that will print 24.

    print $p['post_author'];

    Will print out 1, etc. All I was helping you see was how the CCTM could be used to retrieve that data. How you choose to print and format it is entirely up to you.

    Perhaps you’ve fallen victim to WP’s way of coddling people into thinking that there has to be a function for everything. You don’t need a function for this: just print out the data where you need it. E.g.

    <a href="<?php print $p['guid']; ?>"><?php print $p['post_title']; ?></a>

    I much prefer the laissez faire approach where you add the HTML you want and keep the PHP in your templates as clean and simple as possible. It’s a case where less is more.

    Thread Starter kuhio61

    (@kuhio61)

    Yeah, maybe it’s a very one sided way of looking at it… and maybe why I would have so much trouble here. Anyway, I used this code:

    <?php
    $my_posts = get_custom_field(‘select_relative_posts:to_array’, ‘get_post’);
    foreach ($my_posts as $p) {
    $p[‘post_title’];
    $p[‘guid’]; // this is the permalink
    $p[‘my_custom_field’]; // any defined custom field is available
    $p[‘thumbnail_src’]; // This is where WP stores the “Featured Image”
    }

    ?>

    <div class=”module”>
    <div class=”thumbnail”>
    ” rel=”bookmark” title=”<?php the_title_attribute(); ?>”>
    <img src=”<?php print $p[‘thumbnail_src’]; ?>” />

    </div>
    <h3 class=”blog_header”> “><?php print $p[‘post_title’]; ?> </h3>
    <p><?php print $p[post_date]; ?></p>
    <div class=”entry”>
    <p><?php print $p[post_excerpt]; ?></p>
    </div>
    </div>
    <!– blog_module –>

    And it gave me this:

    Post 4

    2012-01-03 05:36:12

    Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text. Excerpt Text.

    Which is in the right direction. I’m just no familiar how to make the loop display all the posts I attached to it? Also, I wasn’t able to get it to print out the featured image src. Any idea?

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Do you see the foreach statement there? It’s bounded by the curly braces, so anything you want to loop over will have to go inside the { braces }.

    You didn’t DO anything with the data that was inside the loop: you just stated the variable, you didn’t actually PRINT it. It also looks like you botched your anchor tag, AND you have errors in your PHP:

    print $p[post_excerpt]; <– this needs quotes around the array key

    Whereas this works:
    print $p['post_excerpt'];

    Maybe that’s just the WP forum clipping your code. Better to use pastebin.com for code.

    Very importantly, you gotta turn on debugging, otherwise you’re flying in the dark. Put this into your wp-config.php file somewhere near the top:

    define('WP_DEBUG', true);

    It’s soooo critical to get your environment set up. I talk about this in some detail in my book: http://www.amazon.com/WordPress-3-Plugin-Development-Essentials/dp/184951352X

    Anyhow… you can start to see why I hate the way WP handles templates and why I always recommend MODX for a sturdy and scalable CMS with clean architecture. In my mind, there should be as little logic and looping in your templates as possible… WordPress isn’t as bad as Joomla and Drupal, but its approach here is entirely wrong in my opinion.

    Here’s the code that I think you want:
    http://pastebin.com/LgwYh0f5

    Can you see how the loop there iterates over your HTML? That stuff will be printed for each post in your array.

    Thread Starter kuhio61

    (@kuhio61)

    Wow. You’ve been more than helpful and I appreciate your help.

    Firstly, I’m still having issues with this line:

    <img src=”<?php print $p[‘thumbnail_src’]; ?>” />

    The thumbnail isn’t being printed. It’s just blank.

    Secondly, is there another way to format the date?

    From this:
    2012-01-03 05:35:52

    to this:

    01-03-2012

    Thank you.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    If the thumbnail_src isn’t coming through but the _thumbnail_id is, then use one of the previously mentioned WP functions to fetch that post’s guid. The thumbnail is just another post, it just happens to be an image post, and you want that post’s guid.

    Date formats: you’ll have to read up on PHP’s multiple ways of converting date formats.

    Thread Starter kuhio61

    (@kuhio61)

    @fireproofsocks,

    I tried researching this more, looking for the thumbnail information. Could you point me in the right direction? Haven’t a hard time for it to print the thumbnail source.

    Thanks!

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Thread Starter kuhio61

    (@kuhio61)

    @fireproofsocks,

    I got it to display the image, but I am getting the same thumbnail for each post in the loop. How can I differentiate?

    Here’s the code I am using to display the thumb:

    <?php
    $my_posts = get_custom_field('select_relative_posts:to_array', 'get_post');
    foreach ($my_posts as $p) {
    ?>
    <?php echo get_the_post_thumbnail($p, 'home-page-thumb'); ?>
    <?php }
    ?>

    [Please post code snippets between backticks or use the code button.]

    Thanks!

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Man, I think you’re missing what’s going on here. When you use the “get_post” output filter, the $p variable contains the ENTIRE POST ARRAY. $p is an ARRAY, not an integer. get_the_post_thumbnail() only retrieves the dedicated “featured image”, but when you pass it an array when it expects an integer, it instead coughs out the thumbnail for the CURRENT POST because you fed it bad input. So of course, it prints that out over and over.

    Use this function:
    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

    And you have to pass it an INTEGER, not the whole array. You could omit the “get_posts” output filter and deal just with the raw integers (e.g. with a multi-image field):

    <?php
    $my_posts = get_custom_field('select_relative_images:to_array');
    foreach ($my_posts as $post_id) {
       print wp_get_attachment_image($post_id');
    }
    ?>

    Or for use in with a multi-relation field, you MUST indicate which field you are passing to the wp_get_attachment_image() function. (see the WP docs for its other arguments).

    <?php
    $my_posts = get_custom_field('select_relative_images:to_array', 'get_post');
    foreach ($my_posts as $p) {
       print wp_get_attachment_image($p['some_integer_field']);
       // or
       //    print wp_get_attachment_image($p['ID']);
       // or
        //    print wp_get_attachment_image($p['_thumbnail_id');
    }
    ?>

    See how you have to specify a KEY In the $p array there? You can’t just throw $p around, you have to read a value out of it. You have to tell wp_get_attachment_image() which integer to operate on. If you know post ID 123 is an image, then leave the CCTM and everything else out of it and verify that you comprehend this function in its simplest usage:

    <?php
    print wp_get_attachment_image(123);
    ?>

    If this stuff is really over your head, it would probably save us both a lot of time to hire me to fix this up for you, then you could see how it’s done:
    http://fireproofsocks.com/contact/

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Plugin: Custom Content Type Manager] Relations — Display loop with Title, date, excerpt and featur’ is closed to new replies.