如何在函数.php代码中使用html?

时间:2021-03-19 作者:Ginni Singh

实际上,我在尝试在功能代码中使用html时遇到了一个小问题。是这样的。

/* AJAX for Posts */
function misha_loadmore_ajax_handler(){
    $args = json_decode( stripslashes( $_POST[\'query\'] ), true );
    $args[\'paged\'] = $_POST[\'page\'] + 1;
    $args[\'post_status\'] = \'publish\';
    
    query_posts( $args );
    if( have_posts() ) :while( have_posts() ): the_post();
    get_template_part( \'template-parts/post/content\', get_post_format() );
    endwhile;
    endif;
    die;
}
现在我想要的是改变这个部分

    get_template_part( \'template-parts/post/content\', get_post_format() );

<div class="griditem">
                <a class="producturl" href="<?php the_permalink(); ?>">
                    <div class="productimagewrapper">
                        <img class="lazyload" data-src="<?php the_post_thumbnail_url(); ?>" />
                    </div>
                    <div class="productinfo">
                        <h2 class="productname"><?php the_title(); ?></h2>
                        <span class="productprice">$<?php echo esc_html( get_post_meta( get_the_ID(), \'price\', true ) ); ?>.00</span>
                    </div>
                </a>
            </div>
由于我已经试过一次,它显示出一个错误。我很确定我不能直接在函数中获得htmls和闭括号php代码。php代码。你能帮我一下吗?

1 个回复
SO网友:Kirtan

function misha_loadmore_ajax_handler() {
    $args                = json_decode ( stripslashes ( $_POST[ \'query\' ] ), true );
    $args[ \'paged\' ]       = $_POST[ \'page\' ] + 1;
    $args[ \'post_status\' ] = \'publish\';

    query_posts ( $args );
    if( have_posts () ) :while ( have_posts () ): the_post ();
            ?>
            <div class="griditem">
                <a class="producturl" href="<?php the_permalink (); ?>">
                    <div class="productimagewrapper">
                        <img class="lazyload" data-src="<?php the_post_thumbnail_url (); ?>" />
                    </div>
                    <div class="productinfo">
                        <h2 class="productname"><?php the_title (); ?></h2>
                        <span class="productprice">$<?php echo esc_html ( get_post_meta ( get_the_ID (), \'price\', true ) ); ?>.00</span>
                    </div>
                </a>
            </div>
            <?php
        endwhile;
    endif;
    die;
}
或者可以使用ob\\U start()

function misha_loadmore_ajax_handler() {
    $args                  = json_decode ( stripslashes ( $_POST[ \'query\' ] ), true );
    $args[ \'paged\' ]       = $_POST[ \'page\' ] + 1;
    $args[ \'post_status\' ] = \'publish\';
    $new_html = array();
    query_posts ( $args );
    if( have_posts () ) :while ( have_posts () ): the_post ();
            ob_start ();
            ?>
            <div class="griditem">
                <a class="producturl" href="<?php the_permalink (); ?>">
                    <div class="productimagewrapper">
                        <img class="lazyload" data-src="<?php the_post_thumbnail_url (); ?>" />
                    </div>
                    <div class="productinfo">
                        <h2 class="productname"><?php the_title (); ?></h2>
                        <span class="productprice">$<?php echo esc_html ( get_post_meta ( get_the_ID (), \'price\', true ) ); ?>.00</span>
                    </div>
                </a>
            </div>
            <?php
            echo ob_get_clean ();
        endwhile;
    endif;
    die;    
}