• interwebsites

    (@interwebsites)


    I have set up a Custom post type and I’m wanting to create a short code to display the title and content of that CPT in various places

    The code below works but any page that contains the short code then has comments enabled even when settings say comments not allowed.

    How can I stop this causing the comments to appear?

    /* -----Shortcode Testimonials ----- */
    add_shortcode( 'testimonials', 'display_testimonials' );
    function display_testimonials($atts){
    		global $post;
    		$html= '<div class="testimonials">
    					<ul>';
            $args = array(
                'post_type' => 'testimonial',
                'post_status' => 'publish',
                'tax_query' => array(
    			array(
    				'taxonomy' => 'testimonial_category',
    				'field'    => 'slug',
    				'terms'    => $atts['category']
    				)
    			   )
    			);
    
    			$the_query = new WP_Query( $args );
    			if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
    				$html.= '<li>
    				'.get_the_content().'
    				<span class="byline">'.get_the_title().'</span>
    
    				</li>';
    
    			endwhile; endif;
    			$html.= '</ul>
    				</div>';
    			return $html;
        }
Viewing 1 replies (of 1 total)
  • TrishaM

    (@trisham)

    Where are you placing this shortcode? On a page using the WP editor (text or visual)? Or directly in your Theme template files (by wrapping it in PHP)?

    Here’s what I do when I’m trying to debug odd things like this:

    1. I have the following code in my functions.php file (at the very top so I can find it easily). I normally keep it commented out and then uncomment it so it’s active when I need it – it shows at the very top of the browser window (above the header) what template file that page is using (sometimes it’s different than what you expect!).

    function show_template() {
      if ( current_user_can( 'create_users' ) ) {
        global $template;
        print_r($template);
     }
    }
    add_action('wp_head', 'show_template');

    And don’t worry, it will only show the template to logged Admin users not site visitors or other users.

    2. Once I know what template file is being used, then I can modify that if needed – it could be that the template file being used by your page(s) where the shortcode is may have the comments block of code on it.

    Remember that even when you disable commenting under Settings>Discussion, any pages that have a different setting will override that setting – so if you’ve created a Page in WP and put your shortcode there, check that page (scroll down) to the discussion settings and be sure that the box to allow comments is Unticked – otherwise it will take precedence over your general discussion settings and show the comments section.

    I hope this helps you figure it out…..

Viewing 1 replies (of 1 total)

The topic ‘Adding short code to Custom post types’ is closed to new replies.