[Plugin: Posts 2 Posts] Display connect TO posts
-
Second question (sorry for that),
it’s hard for my little head to understant clearly how your plugin works (I’m a little lost with from and to). OK, I managed to show related posts FROM (default behaviour).
I want now tho show posts TO. I look at Wiki the code… and I cannot understand. It’s exactly the same code for the two so I’ve got the same output. I did use Winmerge to get sure I was not dreaming. The SAME code π
-
I need to know:
* what the call to p2p_register_connection_type() looks like
* what the code you’re having trouble with looks like
* the template file in which said code is addedEven if it’s the same code, the value of get_queried_object_id() is different.
Thank you for quick answer.
I put this:<?php function my_connection_types() { // Make sure the Posts 2 Posts plugin is active. if ( !function_exists( 'p2p_register_connection_type' ) ) return; p2p_register_connection_type( array( 'id' => 'page_un', 'from' => 'webcomic', 'to' => 'webcomic', 'sortable' => 'any', ) ); } add_action( 'init', 'my_connection_types', 100 ); ?>I added the code in a single-webcomic.php (custom post).
Maybe I have not understand how the plugin works. I try to explain what I try to do (sorry for my bad English).
I want to create a webcomics navigation. Each comics page is a custom post (named webcomic). As there will be a lot of short stories, I wanted to use your plugin to link every posts of each story. To manage the stories, I thought I had to make the first post of the story special.
An example: a story with page 1, page 2 and page 3. To list all the pages related to one page, I used the FROM feature and add the two other pages. No problem, the code shows the related pages.
Now, I need a link to the first page of the story (“go to first page”). I tried to link TO page 2 and 3 to page 1 but I don’t know to get the link to page 1. Maybe I’m completely wrong.Interesting approach. I would have created an additional “serie” post type that would be connected to all the webcomics.
Anyway, this should work:
$query = new WP_Query( array( 'p2p_type' => 'page_un', 'post_type' => 'webcomic', 'connected_to' => get_queried_object_id(), ) ); $first_comic = $query->posts[0];The
'connected_to'part is important.Interesting approach. I would have created an additional “serie” post type that would be connected to all the webcomics.
Could you explain more ? For the moment, I decided to create a custom taxonomy with one tag: “first page”. Every first page will get this tag so I can list all stories, showing only the first page π (and maybe a custom field with the title of the story but I did not find how to show this title in the other pages. I tink I will have to give the same custom field to all posts).
Thank you for your code but I did not manage to use the $first_comic to get the link to first post. Could you explain ? (and sorry for such dumb questions).
A ‘serie’ post would just be a container for ‘webcomic’ posts.
The connection type would look like this:
p2p_register_connection_type( array( 'id' => 'page_un', 'from' => 'serie', 'to' => 'webcomic', 'sortable' => true ) );You can also set ‘cardinality’ => ‘one-to-many’ if you want a webcomic to only belong to a single serie.
You should just be able to follow the explanations on the wiki:
https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage
I will study this. It’s very nice for you to make such suggestions as I am searching for good advice.
And for the $first_comic = $query->posts[0]; code and how to retrieve the first page link ?
echo
get_permalink( $first_comic );But if you’re going with the ‘serie’ post type, it will be different.
Thank you. It works. Now I will take a look at the series concept and I’m afraid I will need your help π
OK, here I am again. I have created my “stories” post type and register the connection to webcomics page with ‘cardinality’ => ‘one-to-many’.
Now, I have to create in each webcomics page a link to “previous story” and “next story”. And I have no idea how to get this π
And a link to the story.
Sorry for this questions but I did not find any information on the wiki or maybe I did not look at the good place.In your single-webcomic.php file:
Thank you again π I plan to make a complete tutorial about this webcomics concept when I will achieve it.
OK…
I’ve got thisp2p_register_connection_type( array( 'id' => 'histoire', 'from' => 'stories', 'to' => 'webcomic', 'sortable' => 'true', 'cardinality' => 'one-to-many', 'title' => array( 'from' => 'ReliΓ©s', 'to' => 'Histoire' ), ) );and
<?php // Collect the relevant ids $comic_id = get_queried_object_id(); $story_id = p2p_type( 'histoire' )->get_connected( $comic_id )->posts[0]->ID; $prev = p2p_type( 'histoire' )->get_prev( $comic_id, $story_id ); $next = p2p_type( 'histoire' )->get_next( $comic_id, $story_id ); // Display what we found echo get_permalink( $prev ); echo get_permalink( $story_id ); echo get_permalink( $next ); ?>and an error about get_prev and get_next
Warning: call_user_func_array() expects parameter 1 to be a valid callback, class ‘P2P_Directed_Connection_Type’ does not have a method ‘get_prev’ in E:\xampp\htdocs\wordpress\wp-content\plugins\posts-to-posts\core\type.php on line 86
The echo $prev and $next give me the URL of the actual post. $story_id is OK.
And I forget to say I need the link to the first post of the webcomics in previous and next story π
Right, it’s
get_previous, notget_prev.The get_previous and get_next functions do not work for me… I tried get_adjacent too but with no result. echo get_permalink ( $prev ) show the URL of the actual post (same for $next)
$story_id seems correct as echo get_permalink( $story_id ) gives the URL to the story custom post.
The topic ‘[Plugin: Posts 2 Posts] Display connect TO posts’ is closed to new replies.