Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分别显示WordPress文章内容图像和文本_Php_Wordpress - Fatal编程技术网

Php 分别显示WordPress文章内容图像和文本

Php 分别显示WordPress文章内容图像和文本,php,wordpress,Php,Wordpress,我正在看WordPress的博客,我试图弄清楚在显示内容时如何分别显示图像和文本。我之所以要这样做是因为我想创建一个自定义布局,并且我需要能够在不同时抓取所有文本的情况下移动图像。有人知道怎么做吗 我已经得到的示例:(更新) 错误: 警告:DOMDocument::saveHTML()只需要0个参数,给定1个 在里面 /hsphere/local/home/poulaum/isosec.co.uk/blog/wp-includes/functions.php 在线4874 和

我正在看WordPress的博客,我试图弄清楚在显示内容时如何分别显示图像和文本。我之所以要这样做是因为我想创建一个自定义布局,并且我需要能够在不同时抓取所有文本的情况下移动图像。有人知道怎么做吗

我已经得到的示例:(更新)



  • 错误:

    警告:DOMDocument::saveHTML()只需要0个参数,给定1个 在里面 /hsphere/local/home/poulaum/isosec.co.uk/blog/wp-includes/functions.php 在线4874

    和显示上述代码显示内容的图像:

    你可以利用。此特色图片将不包含在帖子内容中。然而,如果你已经在帖子内容中包含了你的图片,那么你需要将帖子内容分为两部分,正如你在问题中所说的

    我有两个函数,一个返回内容中的所有图像(或任何html标记内容),另一个返回没有所需标记/图像的文本。这两个函数都使用

    第一个函数
    get\u content\u without\u tag()
    返回已从图像中剥离的内容。有两个参数

    • $html
      ->包含要剥离的图像的文本,在这种情况下,使用
      应用过滤器('The_content',get_The_content())
      使用帖子内容

    • $tag
      ->要删除的标记的名称,在本例中,“a”作为
      a
      标记保存图像

    下面是函数

    function get_content_without_tag( $html, $tag )
    {
        // Return false if no html or tag is passed
        if ( !$html || !$tag )
        return false;
    
        $dom = new DOMDocument;
        $dom->loadHTML( $html );
    
        $dom_x_path = new DOMXPath( $dom );
        while ($node = $dom_x_path->query( '//' . $tag )->item(0)) {
            $node->parentNode->removeChild( $node );
        }
        return $dom->saveHTML();
    }
    
    function get_tag_without_text( $html, $tag )
    {
        // Return false if no html or tag is passed
        if ( !$html || !$tag )
        return false;
    
        $document = new DOMDocument();
        $document->loadHTML( $html );  
    
        $tags = [];
        $elements = $document->getElementsByTagName( $tag );
        if ( $elements ) {
            foreach ( $elements as $element ) {
                $tags[] = $document->saveHtml($element);
            }   
        }   
        return $tags;
    }
    
    然后,您将使用它来代替只需要显示文本的内容(),去掉完整的
    
    
    


    您可以使用正则表达式来捕获图像标记,但您是否会使用
    CSS
    更改布局?@BenPearlKahan我将使用CSS是的,但我希望在执行CSS之前将图像放置在某个元素中。这就是为什么我要将其从文本中拆分。在您的示例中,这一行的“[”出现意外错误-$tags=[];那么您的php版本太旧了。这是短数组语法(
    []
    )这只适用于PHP5.4及更高版本。注意,以下所有版本都已过时,因此存在潜在的安全问题。您可以将短数组语法更改为旧的
    array()
    语法,因此
    $tags=[];
    将变成
    $tags=array();
    我收到此错误-“致命错误:没有文本()无法重新声明获取标记”(之前在/hsphere/local/home/poulaum/isosec.co.uk/TestingBlog.php:19中声明)在第19行的/hsphere/local/home/poulaum/isosec.co.uk/TestingBlog.php中”-不知道我做错了什么。我已经更新了上面的代码,向您展示了错误的情况,代码块带有函数定义(块1和块3)进入
    functions.php
    ,代码块2和4进入了您的loopOops!我的坏!我已经对它进行了排序:)要测试吗
    echo get_content_without_tag( apply_filters( 'the_content', get_the_content() ), 'a' )
    
    function get_tag_without_text( $html, $tag )
    {
        // Return false if no html or tag is passed
        if ( !$html || !$tag )
        return false;
    
        $document = new DOMDocument();
        $document->loadHTML( $html );  
    
        $tags = [];
        $elements = $document->getElementsByTagName( $tag );
        if ( $elements ) {
            foreach ( $elements as $element ) {
                $tags[] = $document->saveHtml($element);
            }   
        }   
        return $tags;
    }
    
    $image = get_tag_without_text( apply_filters( 'the_content', get_the_content() ), 'a' );
    echo $image[0];
    
    <?php
    $args = array( 
        'numberposts' => 2, 
        'post_status' => 'publish',
        'post_type'   => 'post',
        'orderby'     => 'date'
    );
    $postslist = get_posts( $args );
    
    echo '<ul id="latest_posts">';
    
        foreach ( $postslist as $post ) {  
            setup_postdata($post); 
    
            $content = get_content_without_tag( $post->post_content, 'a' );
            $image = get_tag_without_text( $post->post_content, 'a' );
    
            if ( $content ) 
                echo apply_filters( 'the_content', $content );
            ?> 
    
            <li>
    
                <strong>
                    <?php the_date(); ?>
                </strong>
    
                <br />
    
                <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> 
                    <?php the_title(); ?>
                </a>
    
                <?php 
                    if ( $image ) { 
                        ?>
    
                    <p>
                        <?php echo apply_filters( 'the_content', $image[0] ); ?>
                    </p>
    
                        <?php 
                    } 
                ?>
    
            </li>
    
            <?php 
        } 
    
    echo '</ul>';