WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] String replacements (6 posts)

  1. Troy Chaplin
    Member
    Posted 2 years ago #

    I'm wondering if it's possible to do a string replacement on the_title when it is being outputted from a reciprocal connection? Did not see any specific information about this in the github doc area, unless I somehow overlooked it?

    Any help would be greatly appreciated.
    Thanks

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

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Do you mean in the metabox or in The Loop or where? More context please. kthnxbye.

  3. Troy Chaplin
    Member
    Posted 2 years ago #

    It's within the loop on a custom post type archive page. Using your docs I've set the reciprocal connection like:

    <?php $connected = p2p_type( 'progs_to_progs' )->get_connected( $post->ID );
    	p2p_list_posts( $connected, array(
    	'before_list' => '',
    	'after_list'  => '',
    	'separator'   => ', Master of ',
    ) ); ?>

    We've tried a couple ways to do a string replacement on the title that gets output, but had no luck.

    In the query I'm setting a variable:

    $datitle = get_the_title();

    Then directly before the connection I have:

    <?php echo str_replace(' (Master’s)', '',$datitle) ?>

    I'm looking to do the same replacement on the_title output from the connected posts so the output will read as:

    Aerospace Engineering, Master of Engineering, Master of Applied Science

    Currently, it reads as:

    Aerospace Engineering, Master of Engineering (Master’s), Master of Applied Science (Master’s)

    *with Master of Engineering (Master's) and Master of Applied Science (Master's) being the items connected to Aerospace Engineering in the output

  4. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    If you check the docs again, you will see that p2p_list_posts() is no longer used, precisely because it's hard to customize:

    https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage

  5. Troy Chaplin
    Member
    Posted 2 years ago #

    Ok thanks, I'll go back and read things more thoroughly. I was going by this page here, and everything worked fine, until I started looking at the need for the string replacement.

    I'm guessing some of the info in Looping the Loop is where I should be focusing my efforts?

  6. Troy Chaplin
    Member
    Posted 2 years ago #

    Just came back to follow up, and close this ticket. Thanks for doing that already.

    So, minus some smaller stuff like a separator, the Looping the Loop section worked perfectly! So now, within the loop I have:

    <?php
    	$connected = p2p_type( 'progs_to_progs' )->get_connected( $post );
    
    	while ( $connected->have_posts() ) : $connected->the_post();
    	$connectedtitle = get_the_title();
    
    	echo str_replace(' (Master’s)', '',$connectedtitle);
    
    	endwhile;
    
    	wp_reset_postdata();
    ?>

    Thanks scibu! Appreciate the help, as well as this awesome plugin!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic