BBP_Shortcodes::display_topic( array $attr, string $content = '' )
Display the contents of a specific topic ID in an output buffer and return to ensure that post/page contents are displayed first.
Description
Parameters
- $attr
-
(Required)
- $content
-
(Optional)
Default value: ''
Return
(string)
Source
File: bp-forums/common/shortcodes.php
public function display_topic( $attr, $content = '' ) {
// Sanity check required info
if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
return $content;
// Unset globals
$this->unset_globals();
// Set passed attribute to $forum_id for clarity
$topic_id = bbpress()->current_topic_id = $attr['id'];
$forum_id = bbp_get_topic_forum_id( $topic_id );
// Bail if ID passed is not a topic
if ( !bbp_is_topic( $topic_id ) )
return $content;
// Reset the queries if not in theme compat
if ( !bbp_is_theme_compat_active() ) {
$bbp = bbpress();
// Reset necessary forum_query attributes for topics loop to function
$bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
$bbp->forum_query->in_the_loop = true;
$bbp->forum_query->post = get_post( $forum_id );
// Reset necessary topic_query attributes for topics loop to function
$bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
$bbp->topic_query->in_the_loop = true;
$bbp->topic_query->post = get_post( $topic_id );
}
// Start output buffer
$this->start( 'bbp_single_topic' );
// Check forum caps
if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
bbp_get_template_part( 'content', 'single-topic' );
// Forum is private and user does not have caps
} elseif ( bbp_is_forum_private( $forum_id, false ) ) {
bbp_get_template_part( 'feedback', 'no-access' );
}
// Return contents of output buffer
return $this->end();
}
Changelog
| Version | Description |
|---|---|
| bbPress (r3031) | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.