【问题标题】:Advanced Custom field for each post in WordPressWordPress中每个帖子的高级自定义字段
【发布时间】:2015-09-23 18:33:17
【问题描述】:

我正在使用高级自定义字段,以便通过 qTranslate 插件为每种语言显示不同的横幅图像。

<?php get_header(); ?>

<section id="linea">
            <?php if(qtrans_getLanguage()=='en') {
 ?>
                <img src="<?php the_field('banner')?>">
<?php }
else if(qtrans_getLanguage()=='es') {?>
                <img src="<?php the_field('banner_image_es')?>">
<?php }?>
        </section>
<section id="categoria1">
            <div class="pagewidth clearfix">
                    <div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">
                        <?php if(function_exists('bcn_display'))
                        {
                            bcn_display();
                        }?>
                    </div>
                    <h1 class="categoria-title"><span class="left"></span><b><?php single_term_title(); ?></b><span class="right"></span></h1>
            </div>
        </section>

        <section id="categoria2">
            <div class="pagewidth clearfix">

                <ul class="product-list">
                    <?php $my_query = new WP_Query(array('post_type' => 'productos','taxonomy'=>'categoria','term'=>'conformink-cunero-luxy','paged' => get_query_var('paged'))); ?>

                    <?php while ( $my_query->have_posts() ) : $my_query->the_post();?> 

                        <li>
                            <h5><a style="color: #f79646;"  href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>  
                            <a class="verdetalle" href="<?php the_permalink(); ?>"><?php if( qtranxf_getLanguage() == 'es' ){ ?>Ver detalle<?php }else { ?>See More<?php } ?></a>
                            <!-- <p><?php echo substr(get_the_excerpt(), 0,120); ?></p> -->
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>   
                        </li>

                    <?php
    endwhile;
    wp_pagenavi( array( 'query' => $my_query ) );?>
    <?php wp_reset_query(); ?>
                </ul>   
</div>
        </section>
<?php get_footer(); ?>

问题是它在所有模板上只显示一个帖子的图像,尽管我已经将不同的图像分配到不同的类别。

谁能帮忙?

【问题讨论】:

  • 上面这段代码应该不是问题。在您的问题中,您提到了类别。您的代码与类别无关。
  • 我有不同类别的不同产品,并为此帖子类型分配了两个自定义字段,我如何根据类别在每个页面上获取横幅图像?现在它在所有页面上显示相同的图像。
  • 您上面的代码 sn-p 不必对类别做任何事情。这段代码在哪里?在你的index.phpsingle.phpcategory.php、...?
  • 感谢您的回复我的代码在这个文件“taxonomy-categoria-organica-bebe.php”中,我的类别名称是“organica-bebe”。
  • 你检查过这个模板是否真的被使用过吗?我认为应该是category-organica-bebe.php(根据codex.wordpress.org/Category_Templates)。帖子或类别是否附加了不同的标志?上面的代码是否在循环中?您应该从模板中添加更多代码以获得答案。

标签: php wordpress custom-post-type advanced-custom-fields


【解决方案1】:

如果您希望横幅显示在每个帖子上,您必须将部分移动到循环内:

<section id="categoria2">
    <div class="pagewidth clearfix">

        <ul class="product-list">
            <?php 
            $my_query = new WP_Query(array('post_type' => 'productos','taxonomy'=>'categoria','term'=>'conformink-cunero-luxy','paged' => get_query_var('paged')));

            if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post();?> 

            <li>
                <section id="linea">
                    <?php if ( qtrans_getLanguage() == 'en' ) : ?>
                        <img src="<?php the_field('banner'); ?>">
                    <?php elseif ( qtrans_getLanguage() == 'es' ) : ?>
                        <img src="<?php the_field('banner_image_es'); ?>">
                    <?php endif; ?>
                </section>
                <h5><a style="color: #f79646;"  href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>  
                <a class="verdetalle" href="<?php the_permalink(); ?>"><?php if( qtranxf_getLanguage() == 'es' ){ ?>Ver detalle<?php }else { ?>See More<?php } ?></a>
                <!-- <p><?php echo substr(get_the_excerpt(), 0,120); ?></p> -->
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>   
            </li>
            <?php
            endwhile; endif;
            wp_pagenavi( array( 'query' => $my_query ) );
            wp_reset_query(); ?>
        </ul>   
    </div>
</section>

【讨论】:

  • 我已经更新了我的答案。删除 category_&lt;ID&gt; 部分。帖子 ID 默认传递给 get_field()
  • 非常感谢您的帮助,我认为它正在工作,让我检查一下。
  • 我知道了,它显示了所有具有此类别的帖子的横幅。有什么办法限制吗?
  • 其实这里很混乱,我只是想展示同一类别的横幅。问题是如果这个类别有多个帖子,这意味着每个帖子都有 2 个横幅,因此循环显示当前类别中所有帖子的横幅。
  • 但是上面你说横幅是附加到帖子而不是类别......我想我不明白你想要达到的目标,对不起。
猜你喜欢
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 2019-06-21
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
相关资源
最近更新 更多