【问题标题】:Wordpress, storing article publishing date and time in variableWordpress,将文章发布日期和时间存储在变量中
【发布时间】:2013-01-06 17:44:37
【问题描述】:

首先,让我解释一下,我想做什么。 我为每个用户都有一个独特的表格,他们可以在其中查看他们的帖子、编辑、在前端删除它们。但是当他们创建一篇文章时,它的状态是“待定”,因为只有我作为管理员才能给它“已发布”状态。 我想做的是向用户显示他们的帖子创建或上次修改的日期,然后是发布的日期。

如何显示我知道的发布日期。因为get_the_date('Y-m-d'); 可以正常工作,因为它显示了任何用户修改帖子的日期。 但我需要将日期存储在某个地方,然后用户最后修改它。

我设法做的是这样的:

<?php $id = get_the_ID(); ?>
<?php $the_last_time="the_time_". $id; ?>
<?php $the_last_date="the_date_". $id; ?>

<?php if ( in_array( $post->post_status, array('draft', 'future', 'pending') ) ) { ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'wpuf' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    <p><?php echo get_the_time('G:i'); ?></p>
    <p><?php echo get_the_date('Y-m-d'); ?></p>
    <?php $$the_last_time=get_the_time('G:i'); ?>
    <?php $$the_last_date=get_the_date('Y-m-d'); ?>
    <?php setcookie("$the_last_time", $$the_last_time, time()+2592000); ?>
    <?php setcookie("$the_last_date", $$the_last_date, time()+2592000); ?>
<?php } else { ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'wpuf' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    <p><?php echo htmlspecialchars($_COOKIE["$the_last_time"]) ?></p>
    <p><?php echo htmlspecialchars($_COOKIE["$the_last_date"]) ?></p>
<?php } ?>


<?php wpuf_show_post_status( $post->post_status ) ?>
        <p><?php echo get_the_time('G:i'); ?></p>
        <p><?php echo get_the_date('Y-m-d'); ?></p>

如您所见,我使用 cookie 来存储这些变量。但问题是,它仅在一台用户计算机中存储可变信息,而我需要的是从任何计算机登录的每个用户都可以看到这些日期。

提前感谢任何可以帮助我的线索。 ;)

---------编辑----------

我尝试创建一个 .php 文件并在其中存储这些变量。然后,当我需要 thenm 时,我可以包含该文件,但我做错了,因为它不起作用。

//Creating variables    
<?php $id = get_the_ID(); ?>
        <?php $the_last_time="the_time_". $id; ?>
        <?php $the_last_date="the_date_". $id; ?>
        <?php $the_last_time_file="the_time_file_". $id; ?>
        <?php $the_last_date_file="the_date_file_". $id; ?>

//Every time after user modification
<?php $$the_last_time=get_the_time('G:i'); ?>
<?php $$the_last_date=get_the_date('Y-m-d'); ?>
<?php $$the_last_date_file=var_export($$the_last_date, true);?>
<?php $$the_last_date_file="<?php\n\n\$$the_last_date_file = $$the_last_date;\n\n?>";?>
<?php file_put_contents('/cookies/time-date-variables.php', $$the_last_date_file);?>

//And when I publish that post it should show echo variable
    <?php include '/cookies/time-date-variables.php'; ?>
    <?php echo $$the_last_date_file; ?>

我做错了什么?有线索吗?

【问题讨论】:

  • 这有点令人困惑。您是说要显示创建文件的用户上次编辑它的时间吗?或者文件最后一次被任何人编辑的时间?
  • 创建文件的用户最后一次编辑(未发布)的时间。还有管理员发布的时间。

标签: php javascript wordpress variables cookies


【解决方案1】:

Wordpress 已经提供了一种获取最后修改日期的方法。您可以在循环中使用它。

&lt;?php the_modified_date( $d, $before, $after, $echo ); ?&gt;

如果您不使用循环,您可以直接查询数据库以获取最新版本的帖子。 Wordpress 使用 post_parent 字段中原始帖子的 id 将帖子的每个编辑存储为数据库中的单独行。因此,您拥有数据库中每个编辑的完整历史记录。这也应该为您提供用户 ID,以便您可以挑选出特定用户的编辑。

我在这里吐口水,但它可能看起来像这样

$postID = 1; // the id of the post
$userID = 1; // The users id for whom you want to show the last edit

$sql = "SELECT post_date FROM wp_posts WHERE post_parent = $postID AND post_author = $userID ORDER BY post_date DESC LIMIT 1";

$userModifiedDate = $wpdb->query($sql);

会有一种更有效/wordpressy 的方式来做到这一点,但它应该可以工作。

【讨论】:

  • 嗯,我尝试使用 'the_modified_date()' 做一些事情,但发布后它显示的是发布日期,而不是修改。而且我找不到任何线索如何将其用于特定用户的疯狂。我从未与 DB 合作过能够从那里检索和排序数据。你能带领我更进一步吗?
  • 你对 SQL 了解多少?
  • 对不起,正在等待能够尝试您的方法,但不幸的是它不起作用。也许我做错了什么? Thats how it looks
  • 它显示了任何用户最后修改的日期。不是创建帖子的特定用户。
  • 好的,那可能是因为您使用的是 1。将“SELECT post_date”更改为“SELECT *”然后再试一次。
猜你喜欢
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多