繁体   English   中英

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

[英]Get the permalink from a loop displaying custom taxonomy titles and images

我有一个称为项目的自定义帖子类型,并在该类别后附加了一个自定义分类法。 我还添加了使用“高级自定义字段”向每个分类中添加图像的功能。

我在网站首页上显示了一个部门列表,其中包含分类法的标题及其图像:

在此处输入图片说明

到现在为止还挺好...

我正在努力用分类法的固定链接围绕图像或标题。 我希望能够单击它,然后将我带到一个页面,显示该部门内的所有项目。

我的循环如下:

<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>

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

<a href="<?php the_permalink(); ?>">
</a>

要么

<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
</a>

但是他们似乎并没有拉通链接...

谁能看到我在做什么错? 我非常感谢任何见解:)

*****编辑***************

这似乎显示了分类法的列表,并且也将链接应用到了分类法...所以我认为这是我需要的两者的结合,以下代码有效但没有图像!

<?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(); ?>

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

$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);检索它们; 确保回显它。

 $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

好的,我想我实际上设法将这两个代码结合起来,而且似乎可行!

<?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(); ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM