doulu7921 2014-05-30 14:28
浏览 30
已采纳

当在同一页面上显示时,Wordpress将post_class添加到页面部分

I am trying to build a single page wordpress theme. I've been able to display all my pages content on one page and scroll between it using menu items. Now I want to add the <?php post_class(); ?> to each of my page sections so they show the proper wordpress classes.

Here is my php that outputs all of the page contents into one page,

<?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>';
}
?>  

There are 2 issues with this code that I can't seem to resolve:

1) This code just outputs the same class for each page, not different classes for each page

2) The class is just outputted into my raw HTML so is visable on my site - it's not adding into where i've written class="' . $class . '">';

  • 写回答

1条回答 默认 最新

  • duanke3985 2014-05-30 14:38
    关注

    Try this instead:

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

    post_class() is designed to echo the resulting class. get_post_class() returns the post classes as an array.

    Since you're operating outside the loop, you need to manually pass the post ID to the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算