wordpress短代码也在仪表板中呈现内容
问题描述:
I am using a simple wordpress shortcode
1 2 3 4 5 6 |
function my_recent_post() { echo \'hello\'; } add_shortcode( \'recent\', \'my_recent_post\' ); |
with the shortcode [recent] and its working fine and visible in front page,
but the problem is, its printing the hello in the dashboard also.
below is the screenshot, can anyone please help.
Update:
I was actually trying to show posts, so can you help me with this, because it renders the lists of posts in the dashboard itself like the \"hello\". I tried:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function lorem_function() { global $post; $args = array( \'posts_per_page\' => 10, \'order\'=> \'ASC\', \'orderby\' => \'title\' ); $postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata( $post ); ?> <div> <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> </div> <?php endforeach; wp_reset_postdata(); return; } add_shortcode(\'lorem\', \'lorem_function\'); |
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议
我使用简单的wordpress短代码</ p>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
function my_recent_post( ) { echo\'hello\'; } add_shortcode(\'recent\',\'my_recent_post\'); </ code> </ pre> <p>使用短代码[recent]和 它的工作正常并在首页中可见, 但问题是,它还在仪表板中打印<em> hello </ em>。 截图,任何人都可以帮忙。</ p> <p> <a href="https://i.stack.imgur.com/XQ26W.png"> <img src> </a> </ p> <p> <strong>更新:</ strong> </ p> <p>我其实是在尝试 显示帖子,你能帮我解决这个问题吗,因为它会在仪表板中呈现帖子列表,就像“你好”一样。 我试过了:</ p> <pre> <code> function lorem_function(){ global $ post; $ args = array(\'posts_per_page\'=&gt; 10,\'order\'=&gt;\'ASC\',\'orderby\'=&gt;\'title\'); $ postslist = get_posts($ args); foreach($ postslist as $ post): setup_postdata($ post); ?&gt; &lt; div&gt; &lt;?php the_date(); ?&GT; &lt; br /&gt; &lt;?php the_title(); ?&GT; &lt;?php the_excerpt(); ?&GT; &lt; / div&gt; &lt;?php endforeach; wp_reset_postdata(); 返回; } add_shortcode(\'lorem\',\'lorem_function\'); </ code> </ pre> </ div> <h5>网友观点:</h5> <div> <p>Based on your comments to me & Nikita Dudarev, what you need to do is create a variable to hold all the post information and then return it. Using the function you posted as an example:</p> <pre><code>function lorem_function() { global $post; $args = array( \'posts_per_page\' => 10, \'order\'=> \'ASC\', \'orderby\' => \'title\' ); $postslist = get_posts( $args ); // create a variable to hold the post information $html =\\\"\\\"; foreach ( $postslist as $post ) : setup_postdata( $post ); $backgroundstyle = \\\"\\\"; // get the featured image and set it as the background if ( has_post_thumbnail() ) { // make sure the post has a featured image $imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'medium\' ); // you can change \\\"medium\\\" to \\\"thumbnail or full depending on the size you need // add the css for the background image. You can include background-size etc ad required $backgroundstyle = \\\"background-image: url(\'\\\".$imageurl[0].\\\"\');\\\"; } // add the information to the variable $html .= \'<div style=\\\"\'.$backgroundstyle.\'\\\">\'; $html .= get_the_date(); $html .= \\\"<br />\\\"; $html .= get_the_title(); $html .= get_the_excerpt(); $html .= \\\"</div>\\\"; endforeach; wp_reset_postdata(); return $html; } add_shortcode(\'lorem\', \'lorem_function\'); |
Note that the_date()
, the_title()
and the_excerpt()
all display the information (just like echo
).
Instead you must use get_the_date()
, get_the_title()
and get_the_excerpt()
- these get the same information, but instead of displaying it directly, they return it as a variable which you can then store in your html string to be returned.
Update:
As you don\'t want to use the variable name on each line for whatever reason, you can do it like this:
1 2 |
$html .= \\\"<div>\\\".get_the_date().\\\"<br />\\\".get_the_title().get_the_excerpt().\\\"</div>\\\"; |
I\'m not sure why you specifically want to change it to do that - it makes absolutely no difference to how it works, it just makes it harder to read and identify any errors :-)
###
Your function must return a value, not output
1 2 3 4 5 6 |
function my_recent_post() { return \'hello\'; } add_shortcode( \'recent\', \'my_recent_post\' ); |
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » wordpress短代码也在仪表板中呈现内容