How to modify the WP posts layout?
-
Hello,
I hope everyone is sound and safe on your side.
I have a plugin (used to hide/show content by clicking on a button) inviting to place a shortcode before and after the content to hide. Here I am trying to hide/show the content of posts, but I do not know exactly where to place these shortcodes.
The part of the post I need to hide/show is the texts part of the post ‘only’. The one we edit on the text editor (Not the featured image or any other element.) And I need to make that change for all posts, not individually. And I didn’t manage to find the portion of code representing this part only.
Can you help me to place these shortcodes? Thank you very much.
-
This topic was modified 6 years, 1 month ago by
vmackey1.
-
This topic was modified 6 years, 1 month ago by
vmackey1.
-
This topic was modified 6 years, 1 month ago by
vmackey1.
-
This topic was modified 6 years, 1 month ago by
vmackey1.
-
This topic was modified 6 years, 1 month ago by
Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
-
This topic was modified 6 years, 1 month ago by
-
If the short code can work within your template, then create a child theme for your site, and edit the template. It’s more complicated this way – and I think you would need to use some sort of custom post entry where the restricted field of content could be accessed by the_content :
https://developer.ww.wp.xz.cn/reference/functions/the_content/Say you want to protect everything in a field called Important Content.
You could enter your data into that field, and in the template you would simply go :
[restrict-this-content]
<?php get_content->the_field_name; ?>
[/restrict-this-content]If you are using the default body of the template, say you have a template in the child theme called tpl-full-width.php, you would try the shortcode here for example :
[restrict-this-content]
<p><?php the_content(); ?></p>
[/restrict-this-content]But, you would need to test if the shortcode works properly in the template.
If not, there are methods to initiate that.Hey, Corrina. Thank you for your answer.
I have used the shortcode in the template by setting it directly in an individual post, and it worked. So I guess it is compatible with the template.
Also, I am already working on a child theme as it is the wordpress default “twentynineteen-child-theme” theme.However, I do not find there the portion of code around which I have to set the shortcodes.
If you are familiar with the theme, would you happen to know where this particular portion of code is?Template path for twentynineteen is here :
~/wp-content/themes/twentynineteen/template-parts/content/If you take a look at line 23 in content-page.php, wrap your shortcode around the_content();
Same or similar would be the method for the other default templates :
content-single.php
content.phpThanks.
I tried to place the shortcodes there but all elements disappeared except for the header.
Basically, the original shortcodes is:
[bg_collapse_level2 view="button-orange" color="#4a4949" expand_text="Show More" collapse_text="Show Less" ]> CONTENT [/bg_collapse_level2]And I added the necesarry codes to make it compatible with the theme files. Which means:
<?php echo do_shortcode("[bg_collapse_level2 view="button-orange" color="#4a4949" expand_text="Show More" collapse_text="Show Less" ]"); ?> CONTENT <?php echo do_shortcode("[/bg_collapse_level2]"); ?>And I place it there:
<div class="entry-content"> <?php <?php echo do_shortcode("[bg_collapse_level2 view="button-orange" color="#4a4949" expand_text="Show More" collapse_text="Show Less" ]"); ?> the_content( <?php echo do_shortcode("[/bg_collapse_level2]"); ?> sprintf( wp_kses( /* translators: %s: Name of current post. Only visible to screen readers */ __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ), array( 'span' => array( 'class' => array(), ), ) ), get_the_title() ) );I also tried:
<div class="entry-content"> <?php <?php echo do_shortcode("[bg_collapse_level2 view="button-orange" color="#4a4949" expand_text="Show More" collapse_text="Show Less" ]"); ?> the_content( sprintf( wp_kses( /* translators: %s: Name of current post. Only visible to screen readers */ __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ), array( 'span' => array( 'class' => array(), ), ) ), get_the_title() ) ); <?php echo do_shortcode("[/bg_collapse_level2]"); ?>But a blank page is displayed for all these options.
Do you see an error in the way I set the code?
You’re getting a blank page bc your code is wrong.
What restrict content plugin are you using?
Replace everything from
<div class="entry-content"> ......... </div> <!-- .entry-content -->with this :
<div class="entry-content"> <?php echo do_shortcode("[bg_collapse_level2 view='button-orange' color='#4a4949' expand_text='Show More' collapse_text='Show Less']"); the_content( sprintf( wp_kses( /* translators: %s: Name of current post. Only visible to screen readers */ __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ), array( 'span' => array( 'class' => array(), ), ) ), get_the_title() ) ); wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentynineteen' ), 'after' => '</div>', ) ); ?> <?php echo do_shortcode("[/bg_collapse_level2]"); ?> </div><!-- .entry-content -->I am using this plugin: https://ww.wp.xz.cn/plugins/show-hidecollapse-expand/
I replaced the code according to what you gave me. The content didn’t disappear, and the hide/show button shows on top, but isn’t hiding any content.
By the way, here is one of the post page of the site: https://randommm.com/2020/05/08/twitter-philanthropy-influencer-charities-are-they-legit/
The content I am trying to hide/show is the text content between the video and the like/dislike buttons.
-
This reply was modified 5 years, 10 months ago by
Jan Dembowski. Reason: Expanded short link
It seems like you are are not implementing in the right template.
Use this plugin to make sure the code is in the template that is rendering the page :
https://ww.wp.xz.cn/plugins/which-template-am-i/Ah yes, indeed. Using the plugin, I see that the template is
/home/randomx/www/wp-content/themes/twentynineteen/single.phpHowever, this file doesn’t contain the piece of code the_content();
I found the line
get_template_part( 'template-parts/content/content', 'single' );and set the shortcodes around it, but I get a blank page.
Same for:the_post();Yes.
That references this file :
/wp-content/themes/twentynineteen/template-parts/content/content-single.phpthe_content is in there.
if you have a blank page, then you still have errors in your code.
Alright.
No, the blank page was when I tried to edit single.php file, not the content-single.php.I set the code you told me again on content-single.php but I still have the same results. The button is on top, but the text isn’t hidden.
https://randommm.com/2020/05/08/twitter-philanthropy-influencer-charities-are-they-legit/
-
This reply was modified 5 years, 10 months ago by
Jan Dembowski. Reason: Short link expanded
Here is the full code of the template:
<?php /** * Template part for displaying posts * * @link https://developer.ww.wp.xz.cn/themes/basics/template-hierarchy/ * * @package WordPress * @subpackage Twenty_Nineteen * @since 1.0.0 */ ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php if ( ! twentynineteen_can_show_post_thumbnail() ) : ?> <header class="entry-header"> <?php get_template_part( 'template-parts/header/entry', 'header' ); ?> </header> <?php endif; ?> <div class="entry-content"> <?php echo do_shortcode("[bg_collapse_level2 view='button-orange' color='#4a4949' expand_text='Show More' collapse_text='Show Less']"); the_content( sprintf( wp_kses( /* translators: %s: Name of current post. Only visible to screen readers */ __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ), array( 'span' => array( 'class' => array(), ), ) ), get_the_title() ) ); wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentynineteen' ), 'after' => '</div>', ) ); ?> <?php echo do_shortcode("[/bg_collapse_level2]"); ?> </div><!-- .entry-content --> <footer class="entry-footer"> <?php twentynineteen_entry_footer(); ?> </footer><!-- .entry-footer --> <?php if ( ! is_singular( 'attachment' ) ) : ?> <?php get_template_part( 'template-parts/post/author', 'bio' ); ?> <?php endif; ?> </article><!-- #post-${ID} -->Hm … Remarkably frustrating.
I’ll get it sorted on my end, and report back.I know right π π
Thank you very much Corrina. I appreciate a lot.
-
This reply was modified 5 years, 10 months ago by
The topic ‘How to modify the WP posts layout?’ is closed to new replies.