Archived topic
Moving Comments
23 replies · Started by Andy on May 30, 2018
The hook part isn't part of your original code:
add_shortcode( 'some_content', 'insert_before_comments' );
function insert_before_comments() {
ob_start(); ?>
<ul class="post-meta">
<li>Distance: <?php echo get_post_meta($post->ID, 'Distance', true); ?></li>
<li>Time: <?php echo get_post_meta($post->ID, 'Time', true); ?></li>
<li style="font-weight:500;"><?php echo get_post_meta($post->ID, 'Download', true); ?></li>
</ul>
<?php
return ob_get_clean();
}
Then your shortcode should be [some_content]. If this doesn't work then unfortunately we might be out of ideas.
Oh I see then I'd just need to add the shortcode [some_content] at the bottom of each post when creating it?
Yup or add them through the hook I suggested before.
Untested...
I've discovered I can just add my custom fields to the GP Hooks 'After Content' hook which works perfectly and then just hide/display sidebar as necessary with CSS.
However, going forward I will need the custom fields to only display on posts in certain categories. I've tried the following but it doesn't display and my sidebar and footer disappear:
<?php if ( in_category( 'mountainbiking' )) {
<div class="mobile-summary" style="margin-top:30px;">
<aside class="widget">
<h2 class="widget-title" style="font-size:22px;">Route Summary</h2>
<?php the_meta(); ?>
</aside>
</div>
}
?>
Try in_category( 'mountainbiking' ) && is_single()
Here's my full code now but it's still not working:
<?php if ( in_category( 'mountainbiking' ) && is_single() ) {
<div class="mobile-summary" style="margin-top:30px;">
<aside class="widget">
<h2 class="widget-title" style="font-size:22px;">Route Summary</h2>
<?php the_meta(); ?>
</aside>
</div>
}
?>
Just to clarify, if I remove the if statement and just leave the HTML and custom fields they display fine.
Try this instead:
<?php if ( in_category( 'mountainbiking' ) && is_single() ) { ?>
<div class="mobile-summary" style="margin-top:30px;">
<aside class="widget">
<h2 class="widget-title" style="font-size:22px;">Route Summary</h2>
<?php the_meta(); ?>
</aside>
</div>
<?php } ?>
Thank you Tom, works like a charm. Obviously didn't quite have my PHP syntax correct.
No problem, glad I could help :)