HMTL原稿:
<div class="col-md-4">
<div class="row">
<a href="http://localhost/PHP/wordpress/3-blog-post/">3 - Blog Post</a>
</div>
<div class="row">
<img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />
</div>
</div>Setup_postdata后的HTML ( $post );
<div class="row">
<div class="col-md-4">
<div class="row">
<a href="http://localhost/PHP/wordpress/3-blog-post/">
3 - Blog Post </a>
</div>
<div class="row">
<img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />
Bem-vindo ao WordPress. Esse é o seu primeiro post. Edite-o ou exclua-o, e então comece a escrever!
</div>
</div>奇怪的事:

守则:
<div class="container">
<div class="row">
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post)
{
?><div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']); ?>">
<?php echo $post['post_title']; ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div><?php
}
wp_reset_query();
?>
</div>
</div>
</br>
</br>
<div class="container">
<div class="row">
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']) ?>">
<?php echo $post['post_title'] ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
</div>
<div class="row">
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
</div>当我更改代码的位置并删除标题和缩略图时,代码运行良好:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?> 我在wordpress / woocommerce页面上给最近的文章打电话,但是当我调用节选时,它没有出现,标题和缩略图都可以。我工作了一整天,但我不知道发生了什么。当我用一个简单的词更改"echo $my_excerpt“时,它就能工作了。问题应该是当我将内容放在数组中时。如您所见,我遵循了这些文档:excerpt
示例get_the_excerpt()可以用于检索和存储变量中的值,而无需将其输出到页面。
<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>我的代码:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<div class="col-md-4">
<div class="row">
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo $post['post_title'] ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
</div>发布于 2017-05-03 20:31:00
经过大量的选择,测试,反馈和研究,我找到了一种方法。
谢谢大家!
我的最后代码:
<!-- Testando novo formato -->
<p class="display-4" style="text-align:center">Testando - Posts Recentes</p>
<div class="container">
<div class="row">
<?php
query_posts( array('posts_per_page'=>3) );
while ( have_posts() ) : the_post();
?>
<div class="col-md-4">
<div class="card" style="width: 20rem; margin-bottom:3rem; margin-top:3rem;">
<img class="card-img-top img-fluid" src="<?php the_post_thumbnail(); ?>">
<div class="card-block">
<h4 class="card-title"><?php the_title(); ?></h4>
<p class="card-text">
<?php
the_excerpt();
?>
</p>
<a class="btn btn-primary" href="<?php the_permalink(' ') ?>">Ler o post</a>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query(); // resets main query
?>
</div>
</div>发布于 2017-05-03 14:52:01
编辑:,我已经修改了您的代码,尝试使用这个代码。
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post)
{
setup_postdata( $post ); // Edit: This will force wordpress to setup the data
?><div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']); ?>">
<?php echo $post['post_title']; ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div><?php
}
wp_reset_query();
?>尝尝这个。我已经为你提供了邮寄证明以获取摘录。
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>https://stackoverflow.com/questions/43763375
复制相似问题