related posts not shown by function in single custom post type
-
i used this code in my single-hindi-songs.php
<?php // check if the function exists if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) { echo '<p>func exists - args</p>'; $args = array( 'post_types' => 'hindi-songs', 'posts_per_page' => 1, ); $taxonomies = array('version'); $post_id = $post->ID; // post id available inside the loop. $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id, $taxonomies, $args ); if ( $related_posts ) { echo '<ul>'; foreach ( (array) $related_posts as $related ) { echo '<li><a style="color:green;" href="' . get_permalink( $related->ID ) . '">' . $related->post_title . '</a></li>'; } echo '</ul>'; } } ?>but it doesn’t shows anything but when i use shortcode for the same
echo do_shortcode('[related_posts_by_tax taxonomies="version" post_types="hindi-songs" title="" posts_per_page="10"]');
it works.
I m also using another shortcode that is working fine in the same template to show related posts from another different taxonomy after the content.
<?php echo do_shortcode('[related_posts_by_tax taxonomies="hindi-chords" post_types="hindi-songs" posts_per_page="5" title="More Songs On Similar Chords"]');?>
i m using correct slug for post type and taxonomy. please check if i m using the correct code.
-
Hi east2611
Check if the post ID is available
$post_id = $post->ID; // post id available inside the loop. echo 'Post ID = ' . $post_id;If it’s not try:
$post_id = get_the_ID(); echo 'Post ID = ' . $post_id;tried both of them still doesn’t show anything. :/ i m using hueman theme and latest version of this plugin. i m not able to understant why the same works with shortcode but not with function.
this is how i have registered the post type and taxonomy
add_action( 'init', 'cptui_register_my_cpts' ); function cptui_register_my_cpts() { $labels = array( "name" => "Hindi Songs", "singular_name" => "Hindi Song", "menu_name" => "Hindi Songs", "all_items" => "All Hindi Songs", "add_new" => "Add New", "add_new_item" => "Add New HS", "edit" => "Edit", "edit_item" => "Edit HS", "new_item" => "New HS", "view" => "view", "view_item" => "View HS", "search_items" => "Search Hindi Songs", "not_found" => "No Songs Found", "not_found_in_trash" => "No Songs Found in Trash", "parent" => "Parent Song", ); $args = array( "labels" => $labels, "description" => "", "public" => true, "show_ui" => true, "has_archive" => true, "show_in_menu" => true, "exclude_from_search" => false, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => true, "rewrite" => array( "slug" => "hindi-songs", "with_front" => true ), "query_var" => true, "menu_position" => 7, "supports" => array( "title", "editor", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "thumbnail", "author", "page-attributes", "post-formats" ), "taxonomies" => array( "artists", "movies", "hs-category", "lyricists", "moods", "music-directors", "types", "hindi-chords", "hs-types", "hs-difficulty", "scales", "version", "contributor", "instructor", "capo" ) ); register_post_type( "hindi-songs", $args ); // End of cptui_register_my_cpts() } add_action( 'init', 'cptui_register_my_taxes' ); function cptui_register_my_taxes() { $labels = array( "name" => "Versions", "label" => "Versions", "menu_name" => "Versions", "all_items" => "All Versions", "edit_item" => "Edit Version", "view_item" => "View Version", "update_item" => "Update Version name", "add_new_item" => "Add New Version", "new_item_name" => "New Version name", "parent_item" => "Parent Version", "parent_item_colon" => "Parent Version:", "search_items" => "Search Versions", "popular_items" => "Popular Versions", "separate_items_with_commas" => "Separate Versions with commas", "add_or_remove_items" => "Add or remove Versions", "choose_from_most_used" => "Choose from most used", "not_found" => "No version found", ); $args = array( "labels" => $labels, "hierarchical" => false, "label" => "Versions", "show_ui" => true, "query_var" => true, "rewrite" => array( 'slug' => 'version', 'with_front' => true ), "show_admin_column" => false, ); register_taxonomy( "version", array( "hindi-songs", "english-songs" ), $args ); // End cptui_register_my_taxes }I’ve tested it with your post type and taxonomy set up and it works for me.
Did the testing code show the right post ID?
Do any other posts have similar version terms as the current posthi thanks for your continous support.
How will i come to know if it’s showing the right post id. Sorry i m beginner. I tested the both of the below code one by one u mentioned in previous reply, it didn’t display anything.. The result was same blank.
$ p o s t _ i d = $ p o s t - > I D ; / / p o s t i d a v a i l a b l e i n s i d e t h e l o o p . e c h o ' P o s t I D = ' . $ p o s t _ i d ; $ p o s t _ i d = g e t _ t h e _ I D ( ) ; e c h o ' P o s t I D = ' . $ p o s t _ i d ;And i have a single same term conecting two posts.. I m using post_per_page => 10 and in the wp reading setting default posts are set to 11.
I don’t think term and number of posts should b a problem bcoz it displays the post with shortcode. I also think it has to do something with post id.Why does the code you posted above have blank spaces after each character? What are you using to write/insert this code?
i typed with mobile so it added the space by itself but.. On pc i copy and pasted the same code by keesiemeijer one by one.. it didn’t display anything.
Does it at least show “Post ID =”?
If the post ID is available it should show the post ID after the “=” character
Hi Esmi ๐
no it doesn’t display anything the output is pure blank.. Can u please tell me ur email id.. I’ll send u username and password.. If u can take a close look.. Thanks for giving ur precious time.
I’m sorry, but we like to keep the support in the forums.
Can you give link to a page where this is happening?
Are you sure the single-hindi-songs.php template is loaded?If you use
do_shortcode()in this template it displays the related posts?1> link to page here
2>yes single-hindi-songs.php is loaded. Below is the whole code of single-hindi-songs.php
3> Yes do_shortcode() in the template displays the related posts.
single-hindi-songs.php
<?php get_header(); ?> <section class="content"> <div class="page-title pad group"> <ul class="meta-single group"> <?php if ( comments_open() && ( ot_get_option( 'comment-count' ) != 'off' ) ): ?> <li class="comments"><a href="<?php comments_link(); ?>"><i class="fa fa-comments-o"></i><?php comments_number( '0', '1', '%' ); ?></a></li> <?php endif; ?> </ul> </div> <div class="pad group"> <?php while ( have_posts() ): the_post(); ?> <article <?php post_class(); ?>> <div class="post-inner group"> <h1 class="post-title"><?php the_title(); ?></h1> <p class="post-byline"> <?php _e('by','hueman'); ?> <?php the_author_posts_link(); ?> ยท </p> <?php if( get_post_format() ) { get_template_part('inc/post-formats'); } ?> <div class="clear"></div> <div class="entry-inner"> <?php // check if the function exists if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) { $args = array( 'post_types' => 'hindi-songs', 'posts_per_page' => 10, ); $taxonomies = array('version'); $post_id = $post->ID; $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id, $taxonomies, $args ); if ( $related_posts ) { echo '<ul>'; foreach ( $related_posts as $related ) { echo '<li><a style="color:green;" href="' . get_permalink( $related->ID ) . '">' . $related->post_title . '</a></li>'; } echo '</ul>'; } } ?> <div class="clear"></div> <?php the_content(); ?> <?php wp_link_pages(array('before'=>'<div class="post-pages">'.__('Pages:','hueman'),'after'=>'</div>')); ?> <?php if ( ot_get_option('sharrre') != 'off' ) { get_template_part('inc/sharrre'); } ?> <div class="clear"></div> </div><!--/.entry--> </div><!--/.post-inner--> </article><!--/.post--> <div class= "mysharediv"> <?php { echo '<img src="http://eastwst.cf/wp-content/uploads/sharing-tag-line-1.png">'; }; ?> <?php echo sharing_display(); ?> </div> <?php endwhile; ?> <div class="clear"></div> <?php the_tags('<p class="post-tags"><span>'.__('Tags:','hueman').'</span> ','','</p>'); ?> <?php if ( ( ot_get_option( 'author-bio' ) != 'off' ) && get_the_author_meta( 'description' ) ): ?> <div class="author-bio"> <div class="bio-avatar"><?php echo get_avatar(get_the_author_meta('user_email'),'128'); ?></div> <p class="bio-name"><?php the_author_meta('display_name'); ?></p> <p class="bio-desc"><?php the_author_meta('description'); ?></p> <div class="clear"></div> </div> <?php endif; ?> <ul class="post-nav group"> <li class="next"><?php next_post_link('%link', '<i class="fa fa-chevron-right"></i><strong>'.__('Next song', 'hueman').'</strong> <span>%title</span>'); ?></li> <li class="previous"><?php previous_post_link('%link', '<i class="fa fa-chevron-left"></i><strong>'.__('Previous song', 'hueman').'</strong> <span>%title</span>'); ?></li> </ul> <?php comments_template('/comments.php',true); ?> </div><!--/.pad--> </section><!--/.content--> <?php if ( is_singular( array( 'hindi-songs', 'english-songs' ) ) ) { get_template_part('sidebar-hs');} else { get_sidebar();} ?> <script async src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="/wp-content/themes/childhueman/my-custom-js/jquery.sticky-kit.js" async></script> <script src="/wp-content/themes/childhueman/my-custom-js/collapso-heading.js" async></script> <?php get_footer(); ?>1> link to page here
2>yes single-hindi-songs.php is loaded. Below is the whole code of single-hindi-songs.php
3> Yes do_shortcode() in the template displays the related posts.
sorry i pasted the wrong single-hindi-songs.php in the previous reply.
this is the real one. the earlier one was missing (array) after foreach in function.<?php get_header(); ?> <section class="content"> <div class="page-title pad group"> <ul class="meta-single group"> <?php if ( comments_open() && ( ot_get_option( 'comment-count' ) != 'off' ) ): ?> <li class="comments"><a href="<?php comments_link(); ?>"><i class="fa fa-comments-o"></i><?php comments_number( '0', '1', '%' ); ?></a></li> <?php endif; ?> </ul> </div> <div class="pad group"> <?php while ( have_posts() ): the_post(); ?> <article <?php post_class(); ?>> <div class="post-inner group"> <h1 class="post-title"><?php the_title(); ?></h1> <p class="post-byline"> <?php _e('by','hueman'); ?> <?php the_author_posts_link(); ?> ยท </p> <?php if( get_post_format() ) { get_template_part('inc/post-formats'); } ?> <div class="clear"></div> <div class="entry-inner"> <?php // check if the function exists if ( function_exists( 'km_rpbt_related_posts_by_taxonomy' ) ) { $args = array( 'post_types' => 'hindi-songs', 'posts_per_page' => 10, ); $taxonomies = array('version'); $post_id = $post->ID; $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id, $taxonomies, $args ); if ( $related_posts ) { echo '<ul>'; foreach ((array) $related_posts as $related ) { echo '<li><a style="color:green;" href="' . get_permalink( $related->ID ) . '">' . $related->post_title . '</a></li>'; } echo '</ul>'; } } ?> <div class="clear"></div> <?php the_content(); ?> <?php wp_link_pages(array('before'=>'<div class="post-pages">'.__('Pages:','hueman'),'after'=>'</div>')); ?> <?php if ( ot_get_option('sharrre') != 'off' ) { get_template_part('inc/sharrre'); } ?> <div class="clear"></div> </div><!--/.entry--> </div><!--/.post-inner--> </article><!--/.post--> <div class= "mysharediv"> <?php { echo '<img src="http://eastwst.cf/wp-content/uploads/sharing-tag-line-1.png">'; }; ?> <?php echo sharing_display(); ?> </div> <?php endwhile; ?> <div class="clear"></div> <?php the_tags('<p class="post-tags"><span>'.__('Tags:','hueman').'</span> ','','</p>'); ?> <?php if ( ( ot_get_option( 'author-bio' ) != 'off' ) && get_the_author_meta( 'description' ) ): ?> <div class="author-bio"> <div class="bio-avatar"><?php echo get_avatar(get_the_author_meta('user_email'),'128'); ?></div> <p class="bio-name"><?php the_author_meta('display_name'); ?></p> <p class="bio-desc"><?php the_author_meta('description'); ?></p> <div class="clear"></div> </div> <?php endif; ?> <ul class="post-nav group"> <li class="next"><?php next_post_link('%link', '<i class="fa fa-chevron-right"></i><strong>'.__('Next song', 'hueman').'</strong> <span>%title</span>'); ?></li> <li class="previous"><?php previous_post_link('%link', '<i class="fa fa-chevron-left"></i><strong>'.__('Previous song', 'hueman').'</strong> <span>%title</span>'); ?></li> </ul> <?php comments_template('/comments.php',true); ?> </div><!--/.pad--> </section><!--/.content--> <?php if ( is_singular( array( 'hindi-songs', 'english-songs' ) ) ) { get_template_part('sidebar-hs');} else { get_sidebar();} ?> <script async src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="/wp-content/themes/childhueman/my-custom-js/jquery.sticky-kit.js" async></script> <script src="/wp-content/themes/childhueman/my-custom-js/collapso-heading.js" async></script> <?php get_footer(); ?>got it resolved. ๐ thanks for the great support giving this plugin five star. ๐
the changes i was making in single-hindi-songs.php wasn’t taking by filezilla for dtrange reason, i tried it in the wp appearence editor. it worked.
Good catch!
I’m glad you’ve got it resolved ๐
Thanks for the review!
The topic ‘related posts not shown by function in single custom post type’ is closed to new replies.