【问题标题】:Return post date in specific format wordpress以特定格式返回发布日期 wordpress
【发布时间】:2017-02-10 08:01:02
【问题描述】:

我正在尝试在 WordPress 中以特殊格式显示发布日期。我成功地做到了,但日期与帖子不对应。似乎我的 while 循环只占用了帖子的第一个日期并将其应用于其他帖子。例如:我在一个网格中有 3 个帖子(post1、post2、post3),最近的帖子是今天 2 月 10 日发布的,另外 2 个日期不同。我的网格将显示所有帖子的最新帖子的日期。 while 循环位于一个文件中,该文件在生成每个帖子时都会被调用。

这是我的代码:

$check = 1;
while (have_posts()) : the_post();
if ($check == 1) {
    echo "<div class='vc-datewrapper'>";
    echo "<div class='vc-datebox'>";
    echo "<span class='vc-date'>";
        $day = the_time('d');
    echo "</span>";
    echo "<span class='vc-month'>";
        $day2 = the_time('M');
    echo "</span>";
    echo "</div></div></div>";
    $check = 0;
}   
endwhile; 

我页面上的输出是:2 月 10 日/2 月 10 日/2 月 10 日。现在我的问题是如何从每个帖子中具体获取日期,而不是最近的 3 个?

整个代码(可视化作曲模板:vc_gitem_post_data.php)

<?php
if ( ! defined( 'ABSPATH' ) ) {
    die( '-1' );
}

/**
 * Shortcode attributes
 * @var $atts
 * Shortcode class
 * @var $this WPBakeryShortCode_VC_Gitem_Post_Data
 */

$output = $text = $google_fonts = $font_container = $el_class = $css = $google_fonts_data = $font_container_data = $link_html = '';
extract( $this->getAttributes( $atts ) );

extract( $this->getStyles( $el_class, $css, $google_fonts_data, $font_container_data, $atts ) );

$data_source = $this->getDataSource( $atts );
if ( isset( $atts['link'] ) && '' !== $atts['link'] && 'none' !== $atts['link'] ) {
    $link_html = vc_gitem_create_link( $atts );
}
$use_custom_fonts = isset( $atts['use_custom_fonts'] ) && 'yes' === $atts['use_custom_fonts'];
$settings = get_option( 'wpb_js_google_fonts_subsets' );
$subsets = '';
if ( is_array( $settings ) && ! empty( $settings ) ) {
    $subsets = '&subset=' . implode( ',', $settings );
}
$content = '{{ post_data:' . esc_attr( $data_source ) . ' }}';
if ( ! empty( $link_html ) ) {
    $content = '<' . $link_html . '>' . $content . '</a>';
}
$css_class .= ' vc_gitem-post-data';
if ( $data_source ) {
    $css_class .= ' vc_gitem-post-data-source-' . $data_source;
}
if ( $use_custom_fonts && ! empty( $google_fonts_data ) && isset( $google_fonts_data['values']['font_family'] ) ) {
    wp_enqueue_style( 'vc_google_fonts_' . vc_build_safe_css_class( $google_fonts_data['values']['font_family'] ), '//fonts.googleapis.com/css?family=' . $google_fonts_data['values']['font_family'] . $subsets );
}
$output .= '<div class="' . esc_attr( $css_class ) . '" >';
$style = '';
if ( ! empty( $styles ) ) {
    $style = 'style="' . esc_attr( implode( ';', $styles ) ) . '"';
}
$output .= '<' . $font_container_data['values']['tag'] . ' ' . $style . ' >';
$output .= $content;
$output .= '</' . $font_container_data['values']['tag'] . '>';
$output .= '</div>';


$check = 1;
while (have_posts()) : the_post();
if ($check == 1) {
    echo "<div class='vc-datewrapper'>";
//  $categories = get_the_category();
//  if ( ! empty( $categories ) ) {
//      $vc_cat = esc_html( $categories[0]->name );
//      echo "<div class='" . $vc_cat . "'>";  
//  }
    echo "<div class='vc-datebox'>";
    echo "<span class='vc-date'>";
        get_the_date('d');
    echo "</span>";
    echo "<span class='vc-month'>";
        the_time('M');
    echo "</span>";
    echo "</div></div></div>";
    $check = 0;
}   
endwhile; 

echo $output;

提前感谢任何线索!

【问题讨论】:

  • 首先,你能给我们三个日期吗?如果你只用 the_time('d') 替换 $day = the_time('d') 会得到什么
  • 这三个日期是:2 月 10 日/2 月 5 日/2 月 3 日。所以它显示 2 月 10 日 3 次。如果我只用 the_time('d') 替换,它会给出相同的结果。
  • 可以分享完整的代码吗?
  • @ArneBanck :使用 the_time() 获取时间,使用 get_the_date() 获取日期,因此将 the_time() 替换为 get_the_date()
  • 如果我用 get_the_date 替换它,它不会返回任何东西@bfahmi 编辑:忘记回显它,但仍然显示相同的结果 2 月 10 日 3 次

标签: php wordpress date


【解决方案1】:

我通过在 Visual Composer wordpress 插件中查找调用日期的函数来解决它。

看起来像这样:

function vc_gitem_template_attribute_post_datetime( $value, $data ) {
    /**
     * @var null|Wp_Post $post ;
     */
    extract( array_merge( array(
        'post' => null,
    ), $data ) );

    return get_the_time( 'F j, Y g:i', $post->ID );
}

您可以在此处调整日期的显示。再次关闭问题,感谢您的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多