• Resolved chazalthibault

    (@chazalthibault)


    Hi
    I’ve found in this post showing-one-pages-content-on-another-page a shortcode which work very well (thanks gabrielcastillo)

    you can wrap this into a shortcode as well. Paste this in your functions.php file and you can now use the shortcode
    ” [my_content id=”Enter your page id number” title=Set this to true if you want to show title /] “

    function get_post_page_content( $atts ) {
    			extract( shortcode_atts( array(
    				'id' => null,
    				'title' => false,
    			), $atts ) );
    
    			$the_query = new WP_Query( 'page_id='.$id );
    			while ( $the_query->have_posts() ) {
    				$the_query->the_post();
    			        if($title == true){
    			        the_title();
    			        }
    			        the_content();
    			}
    			wp_reset_postdata();
    
    		}
    		add_shortcode( 'my_content', 'get_post_page_content' );

    But I would like to use it in an other shortcode, a tab (I have 3 tab).

    My code look like this :

    [tabgroup]
    
    [tab title="Forum"][my_content id="2149" title="" /][button size=medium style=less_round color=blue align=right url=https://zangomedia.fr/forum]Accéder au Forum[/button][/tab]
    [tab title="Mes copains"][my_content id="2145" title=""/][button size=medium style=less_round color=blue align=right url=https://zangomedia.fr/friends]Aller voir les copains[/button][/tab]
    
    [tab title="Mon Profil"][my_content id="2142" title="" /][button size=medium style=less_round color=blue align=right url=https://zangomedia.fr/profil]Voir mon profil[/button][/tab]
    
    [/tabgroup]

    The problem is that the post are displays first and outside the tabs…
    Here is the result https://zangomedia.fr/salle-commune/

    If you have any idea to correct it, I would be glad!

    Thanks and have fun!
    Thibault

Viewing 4 replies - 1 through 4 (of 4 total)
  • your shortcode does ‘echo‘ the reulsts – to work properly as shortcode, it should ‘return‘ the results.

    example:

    function get_post_page_content( $atts ) {
    			extract( shortcode_atts( array(
    				'id' => null,
    				'title' => false,
    			), $atts ) );
    
    			$the_query = new WP_Query( 'page_id='.$id );
    			$output = '';
    			while ( $the_query->have_posts() ) {
    				$the_query->the_post();
    			        if($title == true){
    			        $output .= get_the_title();
    			        }
    			        $output .= apply_filters( 'the_content'), get_the_content() );
    			}
    			wp_reset_postdata();
    			return $output;
    		}
    		add_shortcode( 'my_content', 'get_post_page_content' );

    not tested.

    Thread Starter chazalthibault

    (@chazalthibault)

    Okay I see
    I don’t know why yet but your code put my whole site down… I don’t see where is the mistake… I’m not that good in php!

    I had a typo in the code:
    excess bracket in this line:

    $output .= apply_filters( 'the_content'), get_the_content() );

    – corrected line:

    $output .= apply_filters( 'the_content', get_the_content() );

    Thread Starter chazalthibault

    (@chazalthibault)

    Perfectly working
    Thank you so much!!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Wrong display in a post’ is closed to new replies.