Post foreach 循环中的标题不正确但内容正确 - Wordpress
Post title incorrect in the foreach loop but content is correct - Wordpress
function quotes_shortcode() {
if ( false === ( $quotes = get_transient( 'random_quote' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$args = array(
'post_type' => 'quotes',
'orderby' => 'rand',
'fields' => 'id',
'posts_per_page' => '1'
);
$quotes = get_posts( $args );
//Now we store the array for one day.
//Just change the last parameter for another timespan in seconds.
$seconds_until_next_day = strtotime('tomorrow') - time();
set_transient( 'random_quote', $quotes, MINUTE_IN_SECONDS );
}
foreach ( $quotes as $posts ) : setup_postdata( $posts );
?>
<div class="quote_container">
<em><?php the_content(); ?> - <p><?php the_title(); ?></p></em>
</div>
<?php
endforeach;
wp_reset_postdata();
}
add_shortcode('random_quotes','quotes_shortcode');
我创建了一个 wordpress 短代码来显示每天的随机报价,报价的内容似乎显示正常但标题不正确,因为它正在获取页面标题或 post简码被插入到。
如文档中所示:
setup_postdata() does not assign the global $post variable so it’s important that you do this yourself. Failure to do so will cause problems with any hooks that use any of the above globals in conjunction with the $post global, as they will refer to separate entities.
所以试试这个:
function quotes_shortcode() {
if ( false === ( $quotes = get_transient( 'random_quote' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
global $post; // Calls global $post variable
$args = array(
'post_type' => 'quotes',
'orderby' => 'rand',
'fields' => 'id',
'posts_per_page' => '1'
);
$quotes = get_posts( $args );
//Now we store the array for one day.
//Just change the last parameter for another timespan in seconds.
$seconds_until_next_day = strtotime('tomorrow') - time();
set_transient( 'random_quote', $quotes, MINUTE_IN_SECONDS );
}
foreach ( $quotes as $quote ) :
$post = $quote; // Set $post global variable to the current post object
setup_postdata( $post );
?>
<div class="quote_container">
<em><?php the_content(); ?> - <p><?php the_title(); ?></p></em>
</div>
<?php
endforeach;
wp_reset_postdata();
}
add_shortcode('random_quotes','quotes_shortcode');
您需要使用 <?php echo $post->post_title; ?>
才能显示正确的标题。
function quotes_shortcode() {
if ( false === ( $quotes = get_transient( 'random_quote' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$args = array(
'post_type' => 'quotes',
'orderby' => 'rand',
'fields' => 'id',
'posts_per_page' => '1'
);
$quotes = get_posts( $args );
//Now we store the array for one day.
//Just change the last parameter for another timespan in seconds.
$seconds_until_next_day = strtotime('tomorrow') - time();
set_transient( 'random_quote', $quotes, MINUTE_IN_SECONDS );
}
foreach ( $quotes as $posts ) : setup_postdata( $posts );
?>
<div class="quote_container">
<em><?php the_content(); ?> - <p><?php the_title(); ?></p></em>
</div>
<?php
endforeach;
wp_reset_postdata();
}
add_shortcode('random_quotes','quotes_shortcode');
我创建了一个 wordpress 短代码来显示每天的随机报价,报价的内容似乎显示正常但标题不正确,因为它正在获取页面标题或 post简码被插入到。
如文档中所示:
setup_postdata() does not assign the global $post variable so it’s important that you do this yourself. Failure to do so will cause problems with any hooks that use any of the above globals in conjunction with the $post global, as they will refer to separate entities.
所以试试这个:
function quotes_shortcode() {
if ( false === ( $quotes = get_transient( 'random_quote' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
global $post; // Calls global $post variable
$args = array(
'post_type' => 'quotes',
'orderby' => 'rand',
'fields' => 'id',
'posts_per_page' => '1'
);
$quotes = get_posts( $args );
//Now we store the array for one day.
//Just change the last parameter for another timespan in seconds.
$seconds_until_next_day = strtotime('tomorrow') - time();
set_transient( 'random_quote', $quotes, MINUTE_IN_SECONDS );
}
foreach ( $quotes as $quote ) :
$post = $quote; // Set $post global variable to the current post object
setup_postdata( $post );
?>
<div class="quote_container">
<em><?php the_content(); ?> - <p><?php the_title(); ?></p></em>
</div>
<?php
endforeach;
wp_reset_postdata();
}
add_shortcode('random_quotes','quotes_shortcode');
您需要使用 <?php echo $post->post_title; ?>
才能显示正确的标题。