Wordpress: Display the content custom page - wordpress

I want to display the content of a specific page using the theme files.
I tried this :
$import_page = get_page_by_title(page-title-here);
echo "$import_page->post_content";
It works except that it doesnt display the formatting of the page. only text and the images, It avoids the line breaks, paragraph formatting ect.
Thank you for helping me to get this sorted.

You need to apply the_content filter to post_content before printing it.
Like so
echo apply_filters('the_content', $import_page->post_content);
You can find out more about the_content filter.

Related

Custom template doesn't accept shortcodes

I've created a custom template by copying page.php and then removing basically everything except for the CSS link, so it looks like this:
The div on line 20 just puts a red box on the page so I can recognize that this template, and the linked CSS file, are handling the page. (The idea is to create a large blank page where I can put large tables. I don't want any header markup or sidebar markup, etc., to distract attention from the table.)
I see the red box fine, but when I try to insert a shortcode on the page, to insert a table from TablePress, the shortcode isn't processed. I assume I removed something from page.php that is needed to process shortcodes, since a normal new page, that uses page.php, processes the shortcode fine. Does anyone know what I might have removed that killed the shortcodes?
Thanks.
Two thoughts about your code / problem:
1) I think you should not insert the stylesheet link like this. See: https://codex.wordpress.org/Function_Reference/wp_register_style
2) You can insert the shortcode with
<?php
echo do_shortcode('[ShortcodeName]');
?>

Strip shortcode, keep content in between

The idea behind this question lies in getting excerpt of the posts created with avada but I can't strip the shortcodes from the post content to display the excerpt of the post.
Here is example of my post (using avada):
[fullwidth background_color="" background_image="" class="" id=""]
[one_full last="yes" spacing="yes" class="" id=""][fusion_text]
Content text ...
[/fusion_text][/one_full][/fullwidth]`
The default the_excerpt(); doesn't work because of shortcodes. get_content() returns the full post content including shortcodes. Using strip_shortcodes() also removes the content between the shortcodes.
So my plan would be to strip shortcodes using pattern? and trim the message to mimic the excerpt functionality.
PS: This pattern doesn't work.
Use this regex:
$excerpt = get_the_excerpt();
$excerpt = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $excerpt);
It's worth commenting that you can do this same thing using a text editor, for instance Notepad++ or EditPad Pro, using this Regex: \[/?[^/\]]+/?\]
That will match all shortcode, and you can then replace with nothing or a space, whatever.

wordpress shortcodes show up in preview posts

http://www.mytwins.gr/site/
In the third blog post you will see that a shortcode shows up [frame align="none"]Της Εύης Σταθάτου[/frame] which is ugly. Only if you click it, it works fine.
Why is that?, How can you hide the code from the previews posts thingy? Thanks in advance.
P.S I tried to hide the shortcode from the visual mode and enter it in the text mode, but still the same.
As #Ravi suggests, make sure that your plugins are enabled. However, it looks like your shortcodes are appearing within your excerpts (assuming you're using the_excerpt() for the snippets appearing on your homepage?)
I would suggest a handy snippet of RegEx:
$myExcerpt = get_the_excerpt();
$myExcerpt = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $myExcerpt);
echo $myExcerpt;
That should stop the raw shortcodes from appearing.

Paragraph tags and get_page_by_title()

I'm using the $page = get_page_by_title('$name'); to get the contest of a page to display. However, when I use echo $page->post_content; the paragraphs are not wrapped in p tags as is usually the case when you use the_content() inside the wordpress loop. Is there a way to fix this or a better method to get the content that will have the tags?
The value of post_content is the same as the unformatted input the user put in the post editor. In order to get the formatted output you have to apply the appropriate filter.
echo apply_filters('the_content', $page->post_content);
That should apply the appropriate output filter to the post_content field and give you the same value you would normally get from the_content().

WordPress - displaying posts in a page

I would like to display the posts the way they are shown when you have "show latest post" selected under reading settings.
I've tried using query_posts, but that displays the whole post, not the chunk that appears in the preview with the link at the bottom.
any ideas what I should be querying to get that?
Edit
I forgot to mention
I tried using the_excerpt(), but that doesn't show the image in the post. Also the index.php file is using the the_content() and it's displaying it the way I want it to
If you don't want to show the whole post, you should use the_excerpt() instead of the_post()
Edit:
From codex:
<?php the_content('Read more...'); ?> Displays the content of the post and uses "Read more..." for the more link text when the Quicktag is used.
Basically, use the_content('your text for the link') and add the quicktag to your article, this will automatically just show the text before

Resources