【问题标题】:Wordpress add post_class to page sections when shown on the same pageWordpress 在同一页面上显示时将 post_class 添加到页面部分
【发布时间】:2014-05-30 14:28:34
【问题描述】:

我正在尝试构建单页 wordpress 主题。我已经能够在一页上显示我的所有页面内容并使用菜单项在其之间滚动。现在我想将 <?php post_class(); ?> 添加到我的每个页面部分,以便它们显示正确的 wordpress 类。

这是我的 php,它将所有页面内容输出到一个页面中,

<?php $pages = get_pages(array('sort_column' => 'menu_order'));
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$slug = $page_data->post_name;
$class = post_class('content_wrapper');
echo '<div id="' . $slug . '" class="' . $class . '">';
echo $content;
echo '</div>';
}
?>  

这段代码有两个问题我似乎无法解决:

1)这段代码只是为每个页面输出相同的类,而不是为每个页面输出不同的类

2) 该类只是输出到我的原始 HTML 中,所以在我的网站上是可见的 - 它没有添加到我写的地方 class="' . $class . '"&gt;';

【问题讨论】:

    标签: php wordpress class post


    【解决方案1】:

    试试这个:

    $class = get_post_class('content_wrapper', $page_data->ID);
    printf ( '<div id="%s" class="%s">', $slug, implode(' ', $class) );
    

    post_class() 设计为echo 生成的类。 get_post_class() 将帖子类作为数组返回。

    由于是在循环外操作,所以需要手动将帖子 ID 传递给函数。

    【讨论】:

    • 效果很好。只是一个想法。你知道如何将帖子 ID 添加到 div 的 ID 中吗?
    • 当然。试试这样的:$slug = $page_data-&gt;post_name . '-' . $page_data-&gt;ID;
    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2022-12-15
    • 2015-06-10
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多