Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following shortcode in my content

[src]<a href="#">Some Link</a>, <a href="#">Some Other Link</a>[/src]

My post structure looks like this:

<?php echo $content; ?>
<?php wp_link_pages();?>

If that shortcode is being used in the content, I'd like for wp_link_pages to appear before it, if not, I would like it to display after the content. What's the best way to achieve this?

share|improve this question
    
What have you tried so far? –  s_ha_dum Dec 28 '12 at 22:04
    
If the shortcode is present, you want wp_list_pages to display before all of the post content or just before the shortcode? –  s_ha_dum Dec 28 '12 at 22:09
    
If the shortcode is present, I want wp_list_pages to display before that shortcode. Haven't tried anything yet, I've been looking for a solution. I figure I need to a function that will check if the shortcode is being used in the post, and place the wp_list_pages before it, else, place wp_list_pages after the_content. And I understand that I'll need to use a filter to check for the shortcode before it is processed. –  Joshua Richards Dec 28 '12 at 22:11

2 Answers 2

up vote 0 down vote accepted

Do something like this.

// your shortcode callback
function your_sc_callback($atts,$content) {
  $content = wp_list_pages(array('echo'=>false)).$content;
  define('YOUR_SC_RAN',true);
  return $content;
}

Now, in your theme template after the content prints

if (!defined('YOUR_SC_RAN')) {
  wp_list_pages();
}

Or, you could do ...

function append_list_pages($content) {
  return $content.wp_list_pages(array('echo'=>false));
}
add_filter('the_content','append_list_pages',100);

And your shortcode callback would be ...

function your_sc_callback($atts,$content) {
  $content = wp_list_pages(array('echo'=>false)).$content;
  remove_filter( 'the_content','append_list_pages',100 );
  return $content;
}

Both or these are untested, so no guarantees, but I think either should work.

share|improve this answer
    
Will test later tonight. Thank you! –  Joshua Richards Dec 29 '12 at 0:56
    
No luck. wp list pages doesn't work from inside a shortcode function. Also, instead of echo, we have to use return I'm pretty sure. Alternatively, is there a way to pull out the content that is returned from the shortcode, and append it after the wp_list_pages. Maybe if I create a global variable to hold the output, then echo that after the wp_list_pages... though I'm not entirely sure how to even do that. –  Joshua Richards Dec 29 '12 at 4:59
    
Yes. Probably should return. I am somewhat concerned about other the_content filters breaking the markup. I see no reason wp_list_pages shouldn't work in a shortcode : core.trac.wordpress.org/browser/tags/3.5/wp-includes/… –  s_ha_dum Dec 29 '12 at 5:06
    
Actually it is being output! However, it's not in the correct position, it is appearing above the actual post. –  Joshua Richards Dec 29 '12 at 19:12
    
Nevermind, I reviewed your code again, and I had implemented it incorrectly. It works now! Thank you. –  Joshua Richards Dec 29 '12 at 19:19

You can create an empty where you want to put the wp_list_pages and and after all of that, use jQuery (html function) to move the content frome #from to #destionation.

share|improve this answer
    
Please supply some example code as well as the description of what is does –  bungeshea_ Dec 29 '12 at 8:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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