• Resolved Rics1983

    (@rics1983)


    Hello! πŸ™‚

    I’ve succesfully applied the “less efficient” code listed on page Looping-The-Loop.

    But i can’t get to work the second one, the efficient code!

    I have defined in functions this code:

    function my_connection_types() {
    	// Make sure the Posts 2 Posts plugin is active.
    	if ( !function_exists( 'p2p_register_connection_type' ) )
    		return;
    
    	// Keep a reference to the connection type; we'll need it later
    	global $my_connection_type;
    
    	$my_connection_type = p2p_register_connection_type( array(
    		'id' => 'posts2pages',
    		'from' => 'custom_post_type_1',
    		'to' => 'custom_post_type_2'
    	) );
    }
    add_action( 'init', 'my_connection_types', 100 );

    Then I have a page where I already have a loop and where i’ve placed the first “less efficient” code:

    <?php
    /*
    Template Name: Name
    */
    ?>
    
    <?php get_header(); ?>
    
    <div>
        <h5>Heading name</h5>
        <ul>
        <?php $args = array('post_type'=> 'custom_post_type_1', 'posts_per_page'=> -1); ?>
        <?php query_posts($args); ?>
        <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('label-thumb'); ?>
                </a>
    	    <div>
    	        <h2>
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
    		<?php
    		    // Find connected pages
    		    $connected = $my_connection_type->get_connected( $post->ID );
    		    // Display connected pages
    		    p2p_list_posts( $connected );
    	        ?>
    	    </div>
    	</li>
    	<?php endwhile; endif; wp_reset_query(); ?>
         </ul>
    </div>
    
    <?php get_footer(); ?>

    This page template show the connected pages, but out of be less efficient, it wraps the connected pages into an extra ‘ul’ and each page link into a ‘li’ with his relative anchor ‘a’.

    Probably i haven’t yet a great php knowledge but i would to display the connected pages only as anchor ‘a’ without wrapping them into a ‘li’ list.

    Then i’ve tried to put into this template page the second “more efficient” code but i have an error showing into the page:

    Fatal error: Call to a member function each_connected() on a non-object in address-bla-bla.php on line 25

    I would be glad if someone can help me!
    Thank you!

    http://wordpress.org/extend/plugins/posts-to-posts/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author scribu

    (@scribu)

    Please fix your formatting. I can’t understand anything.

    Also, use pastebin.com if it’s a big chunk of code.

    Thread Starter Rics1983

    (@rics1983)

    I’m doing it of course, can’t understand myself why the text renders in this way. I’m trying to be the more accurate as possible describing the problem!

    Ok done! πŸ™‚

    Plugin Author scribu

    (@scribu)

    Please paste the code that containes the each_connected() call. It should be right after query_posts().

    Thread Starter Rics1983

    (@rics1983)

    This is what give me error, but i admit that i’m not sure that i’ve placed the code in the right way, sounds strange to me the nested whiles.

    <?php get_header(); ?>
    
    <div>
        <h5>Heading Name</h5>
        <ul>
    	<?php $args = array('post_type'=> 'custom_post_type_1', 'posts_per_page'=> -1); ?>
    	<?php query_posts($args); ?>
    	<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    	<li>
    	    <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('label-thumb'); ?>
                </a>
    	    <div>
    		<h2>
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
    		<?php
    		    // Find connected pages (for all posts)
    		    p2p_type( 'posts_pages' )->each_connected( $wp_query );
    		?>
    		<?php while ( have_posts() ) : the_post(); ?>
    		<?php the_title(); ?>
                    <?php
    		    // Display connected pages
                        p2p_list_posts( $post->connected );
    		?>
    		<?php endwhile; ?>
    	    </div>
    	</li>
    	<?php endwhile; endif; wp_reset_query(); ?>
        </ul>
    </div>
    
    <?php get_footer(); ?>

    Thank you for the replies!

    Plugin Author scribu

    (@scribu)

    It gives you an error because when you define the connection type you use ‘posts2pages’ and in the template file you use ‘posts_pages’.

    Thread Starter Rics1983

    (@rics1983)

    Yes, thank you, you’re right but you missed another error that is in the second example that iv’e posted.
    I’ve discovered it myself when refreshing the page after i modified the connection_type name (posts2pages).

    Other than using a different defined name (posts2pages vs posts_pages), by copying your code from your example page i’ve placed into the template also a nested while loop that bring a never endind loop.
    So i had to delete a portion of the code that i posted in the 2nd example.

    This is the working correct code (to be inline i will call this the 3rd example! πŸ˜‰ ):

    <?php
    /*
    Template Name: Your Name
    */
    ?>
    
    <?php get_header(); ?>
    
    <div>
        <h5>Heading name</h5>
        <ul>
    	<?php $args = array('post_type'=> 'custom_post_type_1', 'posts_per_page'=> -1); ?>
            <?php query_posts($args); ?>
            <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    	<li>
    	    <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('label-thumb'); ?>
                </a>
    	    <div>
    	        <h2>
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h2>
    		<?php
    		    // Find connected pages (for all posts)
    		    p2p_type( 'posts2pages' )->each_connected( $wp_query );
    		?>
    		<?php
    		    // Display connected pages
    		    p2p_list_posts( $post->connected );
    		?>
    	    </div>
    	</li>
    	<?php endwhile; endif; wp_reset_query(); ?>
        </ul>
    </div>
    
    <?php get_footer(); ?>

    I hope that can be useful also to other people that encounter the same difficulties that affected me!

    Last thing scribu, what about the connected page result wrapped into an ‘ul’->’li’ markup?
    How i can leave them into ‘a’ tags only?

    Thank you!

    Plugin Author scribu

    (@scribu)

    p2p_list_posts( $post->connected, 'before_list=&after_list=&before_item= &after_item=' );
    Thread Starter Rics1983

    (@rics1983)

    Nope, using this code i have the template markup broken.
    ‘ul’ tag will not be closed and apart the first two generated ‘li’ i’ll have all the other ‘li’ elements placed at the first div level.

    Plugin Author scribu

    (@scribu)

    Works for me. Try again (I added some &).

    Thread Starter Rics1983

    (@rics1983)

    Purrfect!
    Yes, i confirm that now works flawlessy.
    The first time that i’ve tried i suspected that something was missing, i’ve did some tests placing values after the ‘=’ like null (=”) but, my shame, i totally forgot the ampersands! πŸ™‚

    So, don’t know where you live but, if we ever met, you’ll have a beer from me! (or whatever else drink you like!)

    If you agree i’ll set the post as solved!

    Please add some snippets from this thread also in your docomentation/examples, could be useful for someone else!

    Thank you for all!

    Plugin Author scribu

    (@scribu)

    Thread Starter Rics1983

    (@rics1983)

    Great Cristi!
    Top notch!
    πŸ˜‰

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Plugin: Posts 2 Posts] Looping the loop function help’ is closed to new replies.