Wordpress 获取要在homepage.php中显示的自定义HTML

Wordpress 获取要在homepage.php中显示的自定义HTML,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我必须在客户WordPress网站的主页上添加一些简单的HTML。首先,我将代码添加到header.php中,所有内容看起来都很好,并且工作正常;但是,后来我意识到,当我只需要在主页上显示它时,它就会显示在每个页面上 因此,我尝试在homepage.php中的get_头之后添加相同的HTML,然后在index.php中添加相同的HTML,但是当我将代码添加到这些页面时,没有显示任何内容 无论我将HTML放在homepage.php或index.php中的何处,它都不会显示。毫无疑问,我是Word

我必须在客户WordPress网站的主页上添加一些简单的HTML。首先,我将代码添加到header.php中,所有内容看起来都很好,并且工作正常;但是,后来我意识到,当我只需要在主页上显示它时,它就会显示在每个页面上

因此,我尝试在homepage.php中的get_头之后添加相同的HTML,然后在index.php中添加相同的HTML,但是当我将代码添加到这些页面时,没有显示任何内容

无论我将HTML放在homepage.php或index.php中的何处,它都不会显示。毫无疑问,我是WordPress的新手

我是否需要制作一个模板或使用一些代码在这两个页面中介绍HTML?为什么它会显示在header.php中

我试着毫无乐趣地使用:

if ( is_home() ) {
如果有帮助,主页代码位于此处:

    <?php
/**
Template Name: Homepage
*
*/
get_header(); ?>
<?php $general_options = get_option( 'meanthemes_theme_general_options' ); ?>
<?php $content_options = get_option ( 'meanthemes_theme_content_options' ); ?>
<?php if( isset( $general_options[ 'hide_slider' ] ) ) {
// do nothing
} else {
?>
<section class="hero flexslider">
<article class="wrapper">
<div class="flex-container"></div>
<ul class="slides">
<?php
$bannerPortfolio = $general_options[ 'swap_slider' ];
if(!$bannerPortfolio) {
query_posts( array ( 'posts_per_page' => 3 ) );
while ( have_posts() ) : the_post();
?>
<li>
<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
<div>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo excerpt_clip(get_the_excerpt(), '28'); ?> <?php _e('[...]','meanthemes'); ?></a>
</div>
<time class="time" datetime="<?php the_time('Y-m-d', '', ''); ?>"><?php the_time('jS M Y') ?></time>
</li>
<?php endwhile; wp_reset_query(); ?>
<?php
} else {
?>
<?php
query_posts( array( 'post_type' => 'portfolio', 'order' => 'desc', 'posts_per_page' => '3' ) );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li>
<hgroup>
<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
<div>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo excerpt_clip(get_the_excerpt(), '48'); ?> <?php _e('[...]','meanthemes'); ?></a></div>
</hgroup>
</li>
<?php endwhile;  ?>
<?php } ?>
</ul>
</article><!-- / article.wrapper -->
</section>
<script>
jQuery(window).load(function() {
    jQuery('.flexslider').flexslider({
          animation: "fade", // animation type fade or slide
          slideshowSpeed: <?php echo sanitize_text_field( $general_options    ['slider_timer'] ); ?>, // sections between transitions
          directionNav: true, // show previous and back
          controlNav: false,
          controlsContainer: '.flex-container'
    });
});
</script>
<?php } ?>
<?php if( $general_options[ 'show_portfolio' ] ) { ?>

    <section class="wrapper portfolio">
        <article>
            <hgroup>
                <h2><?php echo balanceTags( $content_options['we_do'] ); ?></h2>
            </hgroup>
            <div><?php echo balanceTags( $content_options['we_do_summary'] ); ?></div>
        </article>
        <?php
        query_posts( array( 'post_type' => 'portfolio', 'posts_per_page' => '4', 'orderby' => 'date', 'order' => 'desc' ) );
    ?>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <aside>
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('portfolio-thumb'); ?></a>
                <hgroup>
                    <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
                </hgroup>
                <div><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo excerpt_clip(get_the_excerpt(), '14'); ?><?php _e('[...]','meanthemes'); ?></a></div>
            </aside>
<?php endwhile;  wp_reset_query(); ?>
    </section>
<?php } ?>
<script>
jQuery(document).ready(function() {
jQuery('body').addClass("home");
});
</script>
<?php get_footer();  ?>

你可能需要试试


如果主页上显示了博客帖子,
是主页
,如果主页设置为静态页面,
是首页

霍林的例子应该可以。另一个解决方案是在主题目录中创建
front page.php
,将page.php中的内容复制到其中,并为主页(frontpage)添加特定的HTML代码

访问以了解在何种情况下调用哪个模板


访问获取条件标记,您可以在
header.php中使用这些标记,谢谢您的帮助。当我打开header.php时,我突然想到homepage.php模板文件中没有doctype。你认为可能是这样吗?我可以像其他文件一样将doctype添加到文件中吗?可能应该添加doctype。不,添加doctype不起作用。我甚至尝试过将HTML添加到索引页面,但这样做也不会让它显示出来。如果HTML只通过标题显示,我似乎在做一些根本错误的事情。编辑-我尝试过制作一个模板,并使用和get_header();?>,从主页调用它,但是这也不行。在header.php文件中使用is_front_page()也不行吗?谢谢Howlin。如果我将代码添加到php的头文件中,代码就可以正常工作,只是它也会显示在其他页面上。我只希望html框显示在主页上。但是,当我将它添加到homepage.php或index.php时,它根本不会显示出来?is_front_page()位于index.php上。我不知道你是不是在说别的。谢谢。你真的救了我。我创建了一个front-page.php并将我的代码放入其中,它成功了!有一些问题,;未显示名为page right的自定义侧栏模板。但我使用了一个边栏小部件来替换这些内容。
if(is_front_page()){