【问题标题】:wordpress php content formattingwordpress php内容格式化
【发布时间】:2015-12-16 16:35:18
【问题描述】:

我在我的 wordpress 模板中添加了一个自定义 php 函数,我想在其中回显某个父级下的页面内容:

Departments
   -department 1 (get the title and the content clean)
   -department 2 (get the title and the content clean)
   (...)

到目前为止,我的代码没有按我的意愿工作,标题很好,但我需要过滤内容,所以我只能抓取"<p>" 标记之间的文本。这可能吗?谢谢。

functions.php

function echo_childs_of( $postID ) {
    $args = array(
        'order' => 'ASC',
        'post_parent' => $postID,
        'post_status' => null,
        'post_type' => 'any'
    );

    $page_childs = get_children( $args );

    if ( $page_childs ) {

        foreach ( $page_childs as $child ) {

            $title = get_the_title( $child );
            $content = get_the_content($child);
            $content = strip_shortcodes( $content );
            $content = apply_filters('the_content', $content);
            $content = str_replace(']]>', ']]>', $content);
            $content = strip_tags($content);

            echo $title;
            echo $content;
        }




    }
}

在我的 php 页面上

echo_childs_of( 7 );

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以使用 DOM 查找 p 标签元素并使用 for 循环遍历它们以执行您想做的任何事情。

    $content = get_the_content($child);
    $doc = new DOMDocument();
    @$doc->loadHTML($content);
    $p_elements = $doc->getElementsByTagName('p');
    foreach ($p_elements as $p) {
                  //Do something with the p element... Strip tags maybe?
    }
    

    【讨论】:

    • @PeterSmith 我收到错误消息:加载资源失败:服务器响应状态为 500(内部服务器错误)
    【解决方案2】:

    试试这个:

    $content = get_the_content($child);
    preg_match('/<p>(.*)<\/p>/', $content, $match);
    $content = $match[1];
    

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多