所以我有一个php语句:
$latest_pollsid
包含一个数字,该数字在每次提交特定表单时增加1。我将此语句解读为多个函数,对于其中一个函数,我希望它捕获当前时间的值,而不是更新。本质上,我希望它在函数运行时读取语句,获取数字,然后显示它,即使语句本身的值发生变化,它也不会在将来发生变化。有没有办法用PHP做到这一点?
情景:
当用户创建一个新的民意调查时,我正在后台创建一个帖子,其中嵌入了他们刚刚用一个短代码进行的民意调查。短代码使用最新的民意测验,通常意味着如果我现在创建民意测验,明天创建民意测验,明天两篇文章都将显示明天的民意测验。《今日邮报》不会显示今天的民意调查。
用于创建背景帖子的代码:
function programmatically_create_post() {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
// Setup the author, slug, and title for the post
$author_id = 1;
$slug = \'example-post\';
$title = \'My Example Background Post\';
// If the page doesn\'t already exist, then create it
if( null == get_page_by_title( $title ) ) {
// Set the post ID so that we know the post was created successfully
$pollq_question = wp_kses_post( trim( $_POST[\'pollq_question\'] ) );
$post_id = wp_insert_post(
array(
\'comment_status\' => \'open\',
\'ping_status\' => \'closed\',
\'post_author\' => $author_id,
\'post_name\' => $slug,
\'post_title\' => $pollq_question,
\'post_status\' => \'publish\',
\'post_type\' => \'post\',
\'post_content\' => \'[poll id="\' . $latest_pollid . \'"]\'
)
);
// Otherwise, we\'ll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
add_filter( \'after_setup_theme\', \'programmatically_create_post\' );
PHP函数代码:
function polls_latest_id() {
global $wpdb;
$poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY pollq_timestamp DESC LIMIT 1");
return intval($poll_id);
}
管理轮询代码
此页面列出了所有具有正确id的投票,但我对PHP知之甚少,因此我很难解释这一点:
echo "<tr id=\\"poll-$poll_id\\" $style>\\n";
echo \'<td><strong>\'.number_format_i18n($poll_id).\'</strong></td>\'."\\n";
echo \'<td>\';
if($current_poll > 0) {
if($current_poll == $poll_id) {
echo \'<strong>\'.__(\'Displayed:\', \'wp-polls\').\'</strong> \';
}
} elseif($current_poll == 0) {
if($poll_id == $latest_poll) {
echo \'<strong>\'.__(\'Displayed:\', \'wp-polls\').\'</strong> \';
}
} else if(in_array($poll_id, $multiple_polls)) {
echo \'<strong>\'.__(\'Displayed:\', \'wp-polls\').\'</strong> \';
}