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'd like this function to only return on certain pages. How might i do that?

// Retreats Accordion
function retreats_accordion() { 
 ?>

<!-- accordion root --> 
<div id="accordion"> 
    <?php
        global $post;
        $c = 0;
        $class = '';
        $args = array( 'posts_per_page' => 6, 'post_type'=> 'slide', 'slideshow'=> 'retreats');
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) :  setup_postdata($post);
        $c++;
        if( $c == 6 ) $class .= ' last'; ?>
        <a href="<?php echo get_post_meta($post->ID, "slide_url_value", true); ?>">
            <?php the_post_thumbnail('quickfinder'); ?>
        </a>
        <div class="slide<?php echo $class;?>" style="width:200px;">    
            <h4><?php the_title(); ?></h4>
            <?php the_content(); ?>
        </div> 
    <?php endforeach; ?>
</div> 

<script> 
$(function() {

$("#accordion").tabs("#accordion div", {
    tabs: 'img', 
    effect: 'horizontal'
});
});
</script> 

<?php 
}

add_action('nktframework_abovecontent', 'retreats_accordion', 10);

I've tried adding this to the function but it doesn't seem to work.

if( is_page(107) )
    return;
share|improve this question
    
someone already answered your question here - wordpress.stackexchange.com/questions/6730/… (use output buffering) –  onetrickpony Jan 11 '11 at 19:58
    
@Ambitious Amoeba ehm... This seems like absolutely different question to me? That was about shortcode mechanics, this is about conditionals. –  Rarst Jan 11 '11 at 20:00
    
yes, sorry, I read this too quickly –  onetrickpony Jan 11 '11 at 20:14
    
Couldn't you just conditionalise the add_action instead, only attach it when the appropriate page is called, so rather then conditionalising the code in the function, conditionalise the action(it's possible that the is_page call is wrong in the context of that function - outside that may not be the case). –  t31os Jan 11 '11 at 23:54
add comment

2 Answers

up vote 2 down vote accepted

Your code:

if( is_page(107) )
    return;

checks for page and returns nothing on match, effectively it's exactly opposite of what you want. So if you reverse it and put this at start of function:

if( !is_page(107) )
    return;

It will do nothing everywhere, but page 107 where it will proceed to run rest of the function.

share|improve this answer
add comment

The above code should work but its kind of a hack, why not create a page template with the complete layout and accordion in it. That way you can select it easily when you create a new page and you dont have to manually edit the page id's every time you need one.

http://codex.wordpress.org/Pages look under Creating Your Own Page Templates

share|improve this answer
    
Good advice, but it seems some kind of framework is used and those can be very pro-templates and very pro-hooks. My method is hardly a hack, I am used to pro-hook Hybrid framework where working with conditionals is more common than creating templates. So what is best depends on context, there are always many ways to do something in WP. –  Rarst Jan 11 '11 at 21:42
    
Sorry, i did not mean to call your advice a hack but the original method. Your advice fixed the problem with firefusions code. Just wanted to offer a different perspective. –  Patriek Jan 11 '11 at 21:49
add comment

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.