0

I have some custom fields that I need to get inserted into the_content, specifically after a gallery, that's posted in the wordpress backend.

Here's my current code for pulling the data out. I can use this in a template file, but need to get it integrated into a specific place in my template:

<?php
            $project_title = get_post_meta($post->ID, "Project_Title", true);
            $project_date = get_post_meta($post->ID, "Project_Date", true);
            $project_location = get_post_meta($post->ID, "Project_Location", true);
            { ?>
                <div class="project_details">
                    <div class="project_details_title"><?php echo $project_title; ?></div>
                    <div class="project_details_date"><?php echo $project_date; ?></div>
                    <div class="project_details_location"><?php echo $project_location; ?></div>
                </div>
        <?php } ?>

Any help is appreciated.

  • 1
    Other than the spurious curly-braces, what isn't working with what you are doing? – s_ha_dum Jun 20 '13 at 20:59
  • It works just fine right now, but I want the custom fields to be called with the_content, but after the [gallery] shortcode, if there is one in a specific page entry. – ff4500 Jun 20 '13 at 21:40
  • See my answer below. That is how I would do it, rather than try to insert via the the_content filter as that approach would require some dicey regex. – s_ha_dum Jun 20 '13 at 21:43
0

I think that I would hijack the gallery shortcode.

function my_gallery_shortcode($atts) {
  global $post;
  $str = gallery_shortcode($atts);
  $project_title = get_post_meta($post->ID, "Project_Title", true);
  $project_date = get_post_meta($post->ID, "Project_Date", true);
  $project_location = get_post_meta($post->ID, "Project_Location", true);
  $str .= '<div class="project_details">';
    $str .= '<div class="project_details_title">'.$project_title.'</div>';
    $str .= '<div class="project_details_date">'.$project_date.'</div>';
    $str .= '<div class="project_details_location">'.$project_location.'</div>';
  $str .= '</div>';
  return $str;
}
add_shortcode('gallery','my_gallery_shortcode');

That should work but will print those divs whether the meta values are set or not so I'd write some conditions to keep things neat-- ie. if (!empty($project_title)) {, etc.-- so that you are only printing what you need to be.

You could also just register it with add_shortcode('my_project_gallery','my_gallery_shortcode'); and not take over the default gallery at all.

  • How would I integrate this with the existing [gallery] shortcode? When I paste it into functions.php, it lists the custom fields correctly, but stops displaying the gallery itself. – ff4500 Jun 20 '13 at 21:44
  • I noticed a minor bug/typo, which I'll edit out, but that does integrate with the default gallery and it does work when I try it from a mu-plugin file, but a plugin file should work just as well. – s_ha_dum Jun 20 '13 at 21:46
  • Aha! Yes, that works perfectly. Of course, I'm running the Photospace plugin, which likely hijacks the gallery shortcode as well, so it displays the gallery in the original WP gallery format. How would you add the if/else statements to that code? I'm unfortunately not awesome at PHP. – ff4500 Jun 20 '13 at 21:50
  • Confirmed that Photospace is already taking over the shortcode for [gallery], so injecting that code into the functions.php file makes the page revert to using the built-in gallery. Anyway to go about getting this in a different place, or am I going to have to completely replace the_content call in that template file? – ff4500 Jun 21 '13 at 13:50
  • Then register your own gallery as in the last paragraph of the answer, or try to identify what Photospace is doing and compensate. Try ` $str = photospace_shortcode($atts)` instead of $str = gallery_shortcode($atts) but now you are dealing with much more complexity and dependence on a third party. – s_ha_dum Jun 21 '13 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.