Putting content in a sidebar

Hi there - I need to put elements of a custom post into a sidebar. Basically the actual post content I would like to put into a sidebar widget (I can remove it from the main post listing). Ive looked at all sorts of sidebar widgets but none seem to pull anydata from the current post. Is this possible?

  • Nahid

    Hey there Derek !
    Hope you are having a great day!

    Reading your query above, it seems you are trying to put the current post content into a sidebar widget. If that is the case, putting the "[current-post-content]" shortcode (that we're going to create now) in a WordPress "Text" widget would be the best solution. In order to create the shortcode, please add the following PHP code:

    function current_post_content() {
    	return get_the_content();
    }
    add_shortcode( 'current-post-content', 'current_post_content' );

    Please try adding the above snippet of code in your site's theme's (more preferably, child theme's) functions.php file. Alternatively, you can also add the code in a mu-plugin. That'll require you to paste the mentioned code in a PHP file that ends with the extension .php, e.g. "anyname.php". The mentioned code should have a "<?php" definition at the start of the file. This PHP file should be placed in {your root WordPress folder}/wp-content/mu-plugins (you'll need to create the folder named "mu-plugins" if it is not there already).

    The above code should generate a shortcode named "[current-post-content]" for you. You can add this shortcode to any page/post/text widget and it should output the content of the current post.

    Hope this helps. Please let us know if this works for you. Thanks!

    Kind regards,
    Nahid

  • Derek

    OK - so that went horribly wrong, I put that in the functions.php of the child theme and it lost it's mind. Errors on screen are

    function current_post_content() { return get_the_content(); } add_shortcode( 'current-post-content', 'current_post_content' );
    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/derekk2110/public_html/wp-content/themes/listingpro-child/functions.php:7) in /home/derekk2110/public_html/wp-content/themes/listingpro/include/paypal/campaign-save.php on line 3

    Warning: Cannot modify header information - headers already sent by (output started at /home/derekk2110/public_html/skin/functions.php:7) in /home/derekk2110/public_html/ext/hide_my_wp/init.php on line 57

    Warning: Cannot modify header information - headers already sent by (output started at /home/derekk2110/public_html/skin/functions.php:7) in /home/derekk2110/public_html/ext/hide_my_wp/hide-my-wp.php on line 510

    Warning: Cannot modify header information - headers already sent by (output started at /home/derekk2110/public_html/skin/functions.php:7) in /home/derekk2110/public_html/ext/hide_my_wp/hide-my-wp.php on line 511

    Warning: Cannot modify header information - headers already sent by (output started at /home/derekk2110/public_html/skin/functions.php:7) in /home/derekk2110/public_html/ext/hide_my_wp/hide-my-wp.php on line 512

  • Dimitris

    Hello there Derek,

    hope you're doing well today! :slight_smile:

    OK - so that went horribly wrong, I put that in the functions.php of the child theme and it lost it's mind.

    Could you please try to add it as a separate MU plugin? Simply create a new PHP file like:
    wp-content/mu-plugins/content-shortcode.php
    Just create the /mu-plugins/ folder if it doesn't exist already and insert in this file the exact following snippet (be careful not to add any empty lines or spaces in the very beginning of the file):

    <?php
    function current_post_content() {
    	return get_the_content();
    }
    add_shortcode( 'current-post-content', 'current_post_content' );

    PS - whilst I am here do you happen to know how I can resize by sidebars for a specific page type (It's a custom post type called listing)

    Not sure I understand what you're trying to accomplish there, could you please elaborate a bit more on this? If it's something a bit complicated though, please do open a separate thread about it or come find us in our live chat service. https://premium.wpmudev.org/hub/support/#get-support

    Thank you,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.