Return function only on certain pages
-
16-10-2019 - |
Question
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;
Solution
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.
OTHER TIPS
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