[Plugin: Posts 2 Posts] How to make the call multiple times within the loop? (3 posts)

  1. Anointed
    Posted 5 years ago #

    when in the single.php view for a custom post type I can use the following function that you gave me.

    function my_p2p_callback($query) {
    	while ( $query->have_posts() ) : $query->the_post(); ?>
    Preacher: <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    <?php	endwhile;
    p2p_list_connected('actors', 'from', get_the_ID(), 'my_p2p_callback');

    However, when I try to use that function within the loop on an archive page, I get the following errors in my log.

    PHP Fatal error: Cannot redeclare my_p2p_callback()

    I understand why I am getting the error, but can't figure out how to properly write a function that will return a list of actors for my archive pages.

    Basically the above function will run one time, and it does it perfectly, however when it tries to load the second post in the archive page, it errors out.

    I know it's a 'newbie' question, but man I am stuck. Can you help me on this one?

    It would also help me on the single.php page, because I have to use that function to make multiple calls to different post_types on the single page.

    show actors, directors, genres etc...

    Right now I am simply doing a hack and using
    etc.. very tacky, but it's all I understand to do right now.

    thanks so much


  2. scribu
    Posted 5 years ago #

    Just move the my_p2p_callback() definition to functions.php.

  3. Anointed
    Posted 5 years ago #

    Thank you, had not thought about that. All of this is quite a learning process.

    anyhow, it's 3:30am now and I've got to get some rest. Appreciate the help. The site is coming along pretty nice though. Lots of headway.

    If you get a chance, I may have found a bug in 3.0, or my code is bad, not sure, http://wordpress.org/support/topic/403210?replies=2

    Have a great day.

Topic Closed

This topic has been closed to new replies.

About this Topic