Hiding Part of WordPress Content with a Shortcode
Website development, one of the main tasks, facing developers, is to attract new users in order to offer them new interesting lessons, articles or services.
Today I will tell you one of the popular ways to attract new visitors to register.. Sometimes when adding a new article to the site, you can simply hide the link or part of the content. At the same time, displaying a message asking you to register. First, paste the following code into function.php
Code Example:
1 2 3 4 5 6 7 8 9 |
add_shortcode( 'access', 'access_check_shortcode' ); function access_check_shortcode( $attr, $content = null ) { extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) ); if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() ){ return $content; }else{ return 'Часть статьи была скрыта. Для просмотра подалуйста <a href="#link">зарегистрируйтесь</a>.'; } } |
To hide, add the code to the text of the article:
1 2 3 |
[access capability="switch_themes"] А вот и сам скрытый текст. [/access] |
And there is no need to use plugins.. Everything works flexibly and well.
Here I fantasized a little and did it my way )
[access capability=”switch_themes”]
And here is the hidden text itself.[/access]
In code fragments, it is easy to change individual parts of the design and supplement them. This is not always available in plugins., and sometimes even for a fee. This is what makes working with plugins and code snippets different.. So learn and develop your own. Good luck with development!
Consider one more code. More precisely, a shortcode that hides part of the page until a certain date in WordPress.
The code can be useful if you want to hide part of the text for a while, e.g. for promotions. Can also be used to count days, until the new Year:
1 2 3 4 5 6 7 8 9 |
function content_countdown($atts, $content = null){ extract(shortcode_atts(array( 'month' => '', 'day' => '', 'year' => '' ), $atts)); $remain = ceil((mktime( 0,0,0,(int)$month,(int)$day,(int)$year) - time())/86400); if( $remain > 1 ){ return $daysremain = " |
1 2 3 |
"; }else if($remain == 1 ){ return $daysremain = " |
1 2 3 4 5 6 |
"; }else{ return $content; } } add_shortcode('cdt', 'content_countdown'); |
Sample code to hide part of the content on the site until a certain date:
1 2 3 |
[cdt month="12" day="17" year="2012"] This is content that will only be shown after a set number of days. [/cdt] |
After inserting the shortcode, the line will be displayed:
Just (54) days until content is available
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.