Products
GG网络技术分享 2025-03-18 16:12 9
I am using a simple wordpress shortcode
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:
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>
function my_recent_post( ){
echo\'hello\';
}
add_shortcode(\'recent\',\'my_recent_post\');
</ code> </ pre>
使用短代码[recent]和 它的工作正常并在首页中可见,
但问题是,它还在仪表板中打印 hello </ em>。
截图,任何人都可以帮忙。</ p>
更新:</ strong> </ p>
我其实是在尝试 显示帖子,你能帮我解决这个问题吗,因为它会在仪表板中呈现帖子列表,就像“你好”一样。 我试过了:</ p>
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>
网友观点:
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:
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:
$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
function my_recent_post(){
return \'hello\';
}
add_shortcode( \'recent\', \'my_recent_post\' );
Demand feedback