首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从显示自定义分类标题和图像的循环中获取固定链接

从显示自定义分类标题和图像的循环中获取固定链接
EN

Stack Overflow用户
提问于 2019-08-12 21:59:41
回答 2查看 495关注 0票数 1

我有一个名为projects的自定义帖子类型,以及一个附加到称为sectors的自定义分类。我还添加了使用高级自定义字段将图像添加到每个分类法的功能。

我在我的网站主页上显示了一个行业列表,其中包括分类法的标题及其图像:

到目前为止,一切顺利..。

我正在努力使用分类的永久链接来包围图像或标题。我希望能够点击它,它会将我带到一个页面,显示该领域内的所有项目。

我的循环如下:

代码语言:javascript
运行
复制
<div class="container">
<div class="row no-gutters">

    <?php
        // loop through terms of the Sectors Taxonomy
        $getArgs = array(
        'parent'       => 0,
        'order' => 'DESC',
        'orderby' => 'count',
        'hide_empty'    => false,
        );
        // get the taxonomy we are going to loop through. 
        $taxonomy = get_terms('sectors', $getArgs);
        $terms = get_terms($taxonomy);

    // Start the loop through the terms
    foreach ($taxonomy as $term) { 

        // Get acf field Name
        $image = get_field('sector_image', $term ); 
        $url = $image['url'];
        $title = $image['title'];
        $alt = $image['alt'];
        // which size?
        $size = 'large';
        $thumb = $image['sizes'][ $size ];
        ?>

        <div class="col-md-6">
         <div class="sector-item-container" style="position: relative;">

            <div class="box-overlay"><?php echo $term->name; ?></div><!-- box overlay -->

            <a href="<?php the_permalink(); ?>">
            <img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" />
            </a>

        </div>
        </div>

<?php } // end foreach ?>

</div>
</div>

我试着在我想要点击的元素周围添加这样的标签:

代码语言:javascript
运行
复制
<a href="<?php the_permalink(); ?>">
</a>

代码语言:javascript
运行
复制
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
</a>

但他们似乎并不能把链接拉到...

有没有人看到我做错了什么?我非常感谢您的见解:)

*编辑*

这似乎显示了分类法的列表,并将链接应用到它们...所以这是我需要的两者的混合,我想,下面的代码可以工作,但没有图像!

代码语言:javascript
运行
复制
<?php

$taxonomy = 'sectors';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <div class="container-flex">
    <div class="row no-gutters">

        <?php foreach ( $terms as $term ) { ?>

            <div class="col-md-6">
                <div class="sector-item-container" style="position: relative;">


                    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
                        <div>  
                           <?php  
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail('page-thumb-mine');
                            }
                            ?>

                            <?php 

                            $image = get_field('sector_image');

                            if( !empty($image) ): ?>

                                <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

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


                    <div class="sector-item-title">
                        <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
                            <h4><?php echo $term->name; ?></h4>
                        </a>
                    </div>


                </div>
            </div>


        <?php } ?>
    </div>
    </div>
<?php endif;?>
<?php wp_reset_postdata(); ?>
EN

回答 2

Stack Overflow用户

发布于 2019-08-12 23:50:55

这是来自https://developer.wordpress.org/reference/functions/get_term_link/上的codex。

代码语言:javascript
运行
复制
$terms = get_terms( 'species' );

echo '<ul>';

foreach ( $terms as $term ) {

    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );

    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }

    // We successfully got a link. Print it out.
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}

echo '</ul>';

对于您的特定情况,尝试将上面的代码替换为:$terms = get_terms( 'sectors‘);

编辑:对于您的图像,使用get_field('sector_image',$id_of_term_or_post_or_whatever)检索它们;一定要回显它。

代码语言:javascript
运行
复制
 $image = get_field('sector_image', id_of_term_or_post_or_whatever);
 echo $image; //this might be $image['url'] or whatever, depending on how you set up ACF
票数 1
EN

Stack Overflow用户

发布于 2019-08-13 00:21:12

好了,我想我真的成功地把这两段代码组合在一起了,而且看起来还不错!

代码语言:javascript
运行
复制
<?php

$taxonomy = 'sectors';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <div class="container-flex">
    <div class="row no-gutters">

        <?php foreach ( $terms as $term ) { 
            $image = get_field('sector_image', $term ); 
            $url = $image['url'];
            $title = $image['title'];
            $alt = $image['alt'];

            $size = 'large';
            $thumb = $image['sizes'][ $size ];
        ?>



            <div class="col-md-6">
                <div class="sector-item-container" style="position: relative;">


                    <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
                        <div>  
                            <img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" />
                        </div>
                    </a>


                    <div class="sector-item-title">
                        <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
                            <h4><?php echo $term->name; ?></h4>
                        </a>
                    </div>


                </div>
            </div>


        <?php } ?>
    </div>
    </div>
<?php endif;?>
<?php wp_reset_postdata(); ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57462659

复制
相关文章

相似问题

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