HMTL原:後setup_postdata($ POST)the_excerpt沒有出現 - woocomerce/WordPress的渲染
<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>
HTML;
<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頁面,但是當我打電話的摘錄,它並沒有出現,標題和縮略圖都OK。我整天都在努力,但我無法弄清楚會發生什麼。當我用一個簡單的字改變「echo $ my_excerpt」時,它就可以工作。問題應該是當我把內容放在一個數組中時。我曾跟隨的文檔,你可以看到: https://codex.wordpress.org/Function_Reference/get_the_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>
謝謝大家!
你不提供文章ID來獲得摘錄喜歡你與你的其他功能做的事情。既然你沒有使用wordpress的正式循環機制,你需要手動給出id。看到我的答案。 – shazyriver
在foreach開始後寫這個setup_postdata($ post);它會工作,只是檢查它。 – shazyriver
截圖相當無用。 CSS或其他古怪可能會隱藏你的輸出。相反,請執行「查看源代碼」並向我們顯示呈現的HTML。 –