WordPress.org

Forums

Posts 2 Posts
[resolved] Looping the loop function help (13 posts)

  1. Rics1983
    Member
    Posted 3 years ago #

    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/

  2. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

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

  3. Rics1983
    Member
    Posted 3 years ago #

    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! :)

  4. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

  5. Rics1983
    Member
    Posted 3 years ago #

    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!

  6. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

  7. Rics1983
    Member
    Posted 3 years ago #

    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!

  8. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    p2p_list_posts( $post->connected, 'before_list=&after_list=&before_item= &after_item=' );
  9. Rics1983
    Member
    Posted 3 years ago #

    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.

  10. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

  11. Rics1983
    Member
    Posted 3 years ago #

    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!

  12. scribu
    Member
    Plugin Author

    Posted 3 years ago #

  13. Rics1983
    Member
    Posted 3 years ago #

    Great Cristi!
    Top notch!
    ;)

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic