从插件模板文件内部调用_excerpt

时间:2011-06-16 作者:MartinJJ

我正在尝试使用插件模板文件中的\\u摘录表单,插件是wp favorite posts默认情况下,插件仅列出并显示保存的帖子标题,我已设法让它显示所有内容、税务条款、图像等,但对\\u摘录很感兴趣,添加时会发生什么<?php the_excerpt( $post_id ); ?> 代码中不断循环着最喜欢的帖子,没有摘录。

Im使用global $post 这样我就可以得到所有的信息(除了摘录)

<?php

global $post;

if (!empty($user)):
    if (!wpfp_is_user_favlist_public($user)):
        echo "$user\'s Favorite Posts.";
    else:
        echo "$user\'s list is not public.";
    endif;
endif;

if ($wpfp_before):
    echo "<p>".$wpfp_before."</p>";
endif;


if ($favorite_post_ids):
    foreach ($favorite_post_ids as $post_id) {
       $p = get_post($post_id); ?>

       <div class="homepage_props">

           <div class="homepage_props_inner">
               <div class="homepage_propsbanner">
                   <div class="homepage_new">
                       <?php if (strtotime($post->post_date) > strtotime(\'-7 days\')) { ?>
                           <img src="<?php echo get_template_directory_uri(); ?>/images/new.png" alt="latest property listings" />
                       <?php } ?>
                   </div>
                   <h2>
                       <span style="float:left; font-weight:bold;">
                           <?php 
                           if ( \'sales\' == get_post_type($post_id) ) { 
                               echo \'Property For Sale\';
                           } elseif ( \'rentals\' == get_post_type($post_id) ) { 
                               echo \'Property For Rent\'; 
                           } elseif ( \'business\' == get_post_type($post_id) ) { 
                               echo \'Business For Sale</span>\'; 
                           } elseif ( \'bandb\' == get_post_type() ) { 
                               echo \'Bed And Breakfast</span>\'; 
                           }
                           ?>

                        </span>
                        <span style="float:right; font-weight:normal;">
                            <a href="<?php echo get_permalink($post_id); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'themename\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php echo get_the_title ( $post_id ); ?></a>
                        </span>
                    </h2>
                </div>

                <div class="clear"></div>
                <div class="homepage_props_image">

                    <?php  
                    echo "<a href=\'".get_permalink($post_id)."\'>";
                    echo get_the_post_thumbnail ( $post_id, \'medium\' );
                    echo "</a>";
                    ?>

                </div><!-- / homepage_props_image -->

                <div class="homepage_props_info hyphenate">

                    <!-- heres where im trying to put the_excerpt -->

                </div><!-- / homepage_props_info -->
                <div class="clear"></div>

                <div class="homepage_props_tax">

                    <?php
                    if ( \'sales\' == get_post_type($post_id) ) { 
                        echo \'<h3><span style="float:right; font-weight:normal;">\' . get_the_term_list( $post_id, \'property_type\', \'Property Type: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\' . get_the_term_list( $post_id, \'location\', \'Location: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\'.get_the_term_list( $post_id, \'region\', \'Region: \', \' \', \'\' );
                    } elseif ( \'rentals\' == get_post_type($post_id) ) { 
                        echo \'<h3><span style="float:right; font-weight:normal;">\' . get_the_term_list( $post_id, \'property_type\', \'Property Type: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\' . get_the_term_list( $post_id, \'location\', \'Location: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\' . get_the_term_list( $post_id, \'region\', \'Region: \', \' \', \'\' );
                    } elseif ( \'business\' == get_post_type($post_id) ) { 
                        echo \'<h3><span style="float:right; font-weight:normal;">\' . get_the_term_list( $post_id, \'property_type\', \'Property Type: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\' . get_the_term_list( $post_id, \'location\', \'Location: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\' . get_the_term_list( $post_id, \'region\', \'Region: \', \' \', \'\' ); 
                    } 
                    ?>
                    </span></h3>
                </div><!-- / homepage_props_tax -->          

            </div><!-- / homepage_props_inner -->

        </div><!-- / homepage_props -->

    <?php
    }
else:

    echo $wpfp_options[\'favorites_empty\'];

endif; ?>
<span style="float:left; font-size:0.7em;"><?php wpfp_clear_list_link(); ?></span>
<?php wpfp_cookie_warning(); ?>  
第一张图片使用了\\u摘录

with the_excerpt

第二张图片没有\\u摘录
without the excerpt

1 个回复
最合适的回答,由SO网友:Hameedullah Khan 整理而成

你几乎没有做错什么:

\\u摘录不使用post id。

  • 修改全局$post时,应始终将其设置回原始值
  • 您正在将get\\u post的返回值指定给代码中未使用的$p
  • 我对您的代码进行了一些修复。下面的代码只是您的代码与我的修复程序的复制粘贴,请尝试下面的代码,如果您仍然有问题,请告诉我。抱歉,我不得不更改您的代码格式。

    <?php
        if (!empty($user)):
            if (!wpfp_is_user_favlist_public($user)):
                echo "$user\'s Favorite Posts.";
            else:
                echo "$user\'s list is not public.";
            endif;
        endif;
    
        if ($wpfp_before):
            echo "<p>".$wpfp_before."</p>";
        endif;
    
        if ($favorite_post_ids):
            foreach ($favorite_post_ids as $post_id) {
                $p = get_post($post_id);
    ?>
          <div class="homepage_props">
    
                           <div class="homepage_props_inner">
                                <div class="homepage_propsbanner">
                                    <div class="homepage_new">
                                    <?php if (strtotime($p->post_date) > strtotime(\'-7 days\')) { ?>
                                        <img src="<?php echo get_template_directory_uri(); ?>/images/new.png" alt="latest property listings" />
                                    <?php } ?>
                                    </div>
                                    <h2><span style="float:left; font-weight:bold;">
                                    <?php 
                                        if ( \'sales\' == $p->post_type ) { 
                                            echo \'Property For Sale\'; 
                                        } elseif ( \'rentals\' == $p->post_type ) {
                                            echo \'Property For Rent\'; 
                                        } elseif ( \'business\' == $p->post_type ) { 
                                            echo \'Business For Sale\'; 
                                        } elseif ( \'bandb\' == $p->post_type ) {
                                            echo \'Bed And Breakfast\'; 
                                        } 
                                    ?>
                                    </span>
                                    <span style="float:right; font-weight:normal;">
                                        <a href="<?php echo get_permalink($post_id); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'themename\' ), get_the_title( $post_id ) ); ?>" rel="bookmark"><?php echo get_the_title ( $post_id ); ?></a>
                                    </span>
                                    </h2>
                                </div>
    
                                <div class="clear"></div>
                                      <div class="homepage_props_image">
    <?php  
    
            echo "<a href=\'".get_permalink($post_id)."\'>";
            echo get_the_post_thumbnail ( $post_id, \'medium\' );
            echo "</a>";
    
    ?>
                                       </div><!-- / homepage_props_image -->
    
                                               <div class="homepage_props_info hyphenate">
    
                                               <?php echo $p->post_excerpt; ?>
    
                                               </div><!-- / homepage_props_info -->
    
                                                         <div class="clear"></div>
    
                                                <div class="homepage_props_tax">
    
                                        <?php 
                                            if ( in_array( $p->post_type, array( \'sales\', \'rentals\', \'business\' ) ) ) {
                                                echo \'<h3><span style="float:right; font-weight:normal;">\' .get_the_term_list( $post_id, \'property_type\', \'Property Type: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\' .get_the_term_list( $post_id, \'location\', \'Location: \', \' \', \'\' ),\'&nbsp;&nbsp;&nbsp;\'.get_the_term_list( $post_id, \'region\', \'Region: \', \' \', \'\' );
                                                echo \'</span></h3>\';
                                            } 
                                        ?>
                                                </div><!-- / homepage_props_tax -->
    
    
    
                       </div><!-- / homepage_props_inner -->
    
                     </div><!-- / homepage_props -->
    
    <?php
            }
        else:
            echo $wpfp_options[\'favorites_empty\'];
        endif;
    ?>
    

    结束

    相关推荐

    如何使用WPEC 3.8回显loop-page.php上的标记描述

    我正在尝试在标记的页面上显示“标记描述”。我在循环页面中添加了一个if语句。php已经无法在显示标记页面时仅显示自定义文本。。。<?php } elseif ( is_tax ( \'product_tag\' ) ){ ?><h1 class=\"entry-title\"><?php the_title(); ?> print out this text on page</h1> 。。。但我不知道输出标记描述的代码。有人知道显示标签描述的代码吗