首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要添加特定于文章的代码,以避免出现在索引和主页中

需要添加特定于文章的代码,以避免出现在索引和主页中
EN

Stack Overflow用户
提问于 2016-12-15 00:31:12
回答 1查看 26关注 0票数 0

我是新手,但我正在尝试为ACF添加一些在一个主题中工作的Wordpress代码到另一个设置略有不同的主题中。

代码使用ACF插件将重复的列表项添加到帖子中。

新主题使用单个部分- content.php来生成索引页和单个post页的输出。在前面的主题中,有一个single.php,顾名思义,它包含了一个单独的post代码,我只是把代码放在里面,没有问题,它不会影响索引页面。

关于我需要编辑的新主题的content.php代码看起来像这样…

代码语言:javascript
复制
    <div class="entry-content">
    <?php

    // On archive views, display post thumbnail, if available, and       excerpt.
    if ( ! is_singular() ) {

        if ( has_post_thumbnail() ) { ?>

            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Link to %s', 'alienship' ), the_title_attribute( 'echo=0' ) ); ?>">
                <?php echo get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'class' => 'alignleft', 'title' => "" ) ); ?>
            </a>
        <?php
        } // has_post_thumbnail()

        the_excerpt();

    } // if ( ! is_singular() )

    // On singular views, display post thumbnails in the post body if it's not big enough to be a header image
    else {
        $header_image = alienship_get_header_image( get_the_ID() );
        if ( has_post_thumbnail() && 'yes' != $header_image['featured'] ) { ?>

            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Link to %s', 'alienship' ), the_title_attribute( 'echo=0' ) ); ?>">
                <?php echo get_the_post_thumbnail( $post->ID, 'medium', array( 'class' => 'alignright', 'title' => "" ) ); ?>
            </a>
        <?php
        }

        the_content();
    }

    wp_link_pages(); ?>
</div><!-- .entry-content -->

我需要我的代码出现在the_content下方和wp_link_pages上方

问题是我的代码以它自己的If语句开头。如下所示:

代码语言:javascript
复制
        <!-- List Stuff Starts -->

        <?php if( have_rows('list_items') ): ?>



                <?php while( have_rows('list_items') ): the_row(); 

                    // vars
                $item_name = get_sub_field('item_name');
                $item_image = get_sub_field('item_image');
                $item_meta1_label = get_sub_field('item_meta_1_label');
                $item_meta1_content = get_sub_field('item_meta_1_content');
                $item_content = get_sub_field('item_content');
                $item_price = get_sub_field('item_price');
                $item_old_price = get_sub_field('item_old_price');
                $item_url = get_sub_field('item_url');
                ?>

                <div class="row blog-list-row">
                    <?php if($item_url): ?>
                    <div class="col-sm-4 mobile-center mobile-padding">
                        <a href="<?php echo $item_url; ?>" onclick="__gaTracker('send', 'event', 'outbound-list-image', '<?php echo $item_url; ?>', 'List Item Link');" alt="<?php echo $item_name; ?>" target="_blank"><img class="bloglistimage" src="<?php echo $item_image['url']; ?>" alt="<?php echo $item_name; ?>" /></a></div>
                    <?php else: ?>  
            <div class="col-sm-4 mobile-center mobile-padding">         <img class="bloglistimage" alt="<?php echo $item_name; ?>" src="<?php echo $item_image['url']; ?>" /> </div>
                    <?php endif; ?> 

<div class="col-sm-8">
                    <?php if( $item_name ): ?>
                        <h3 class="nmt pull-left"><?php echo $item_name; ?></h3>
                        <?php endif; ?>
                        <div class="clearing"></div>
                    <?php if( $item_price ): ?>
                                <p class="bloglist-price"><span class="blogpricelabel">Price: </span><span class="blogpriceprice">£<?php echo number_format((float)$item_price, 2, '.', ''); ?></span></p>
                            <?php endif; ?>

                            <?php if( $item_old_price ): ?>
                                <p class="old-price"><span class="blogpricelabel">Was: </span><span style="text-decoration: line-through;font-size: 0.9em;" class="bloglist-oldprice">£<?php echo number_format((float)$item_old_price, 2, '.', ''); ?></span></p>
                            <?php endif; ?>

                            <?php if( $item_meta1_label ): ?>
                                <p class="bloglist-meta"><span class="blogmetalabel"><?php echo $item_meta1_label; ?>&nbsp;</span><span class="blogmetacontent"><?php echo $item_meta1_content; ?></span></p>
                            <?php endif; ?>

                            <span class="item-copy"><?php echo $item_content; ?></span>



                            <?php if( $item_url ): ?>

                                <a class="btn btn-xs btn-default pull-right" href="<?php echo $item_url ?>" onclick="__gaTracker('send', 'event', 'outbound-list-button', '<?php echo $item_url; ?>', 'List Item Button');"><span class="button-text">Find Out More...</span></a>   

 <?php endif; ?>
  </div>
                </div>

                        <?php endwhile; ?>



            <?php endif; ?>

            <!-- List Stuff Ends -->

目前,我的代码可以在post页面上运行,但它也会在索引和主页上输出标题、缩略图和摘录下面的自定义字段。我只想让代码出现在单个帖子页面上,我猜是把它放在代码的Else部分。

但是当我把它放到博客页面上时,页面就断了。我知道这是一个语法和确保正确的{}在正确的位置的问题,但我正在努力将它们放在正确的位置和正确的顺序。

感谢收到的任何帮助、指导或指示。干杯……

EN

回答 1

Stack Overflow用户

发布于 2016-12-15 03:00:37

您可以复制该文件并重命名为single.php。一旦你这样做了,你就可以插入你的代码,然后它应该将该模板用于单个帖子页面。

一个更简单的选择是包含一个" if“语句,它封装了检查它是否是主页的代码。

代码语言:javascript
复制
if(!is_home()){ 
//your code here
}

这将仅在页面不是主页时执行代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41147569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档