0

I created a custom post type to show my products. The taxonomy name is called field. My products page (products.php) is customized as the following:

<div id="products_wrapper">
    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_type'=>'product', // Your post type name
            'posts_per_page' => 5,
            'paged' => $paged,
        );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <div class="box">
                    <div class="box-inner">
                    <div class="thumbnail_box">
                        <a href="<?php the_permalink(); ?>" class="permalink_image">
                            <div class="box_thumbnail" style="background-image: url(<?php echo sunset_get_attachment(); ?>);"></div>
                        </a>
                    </div>
                    <div class="right">
                        <div class="title_excerpt">
                            <div class="title_box">                                 
                            <?php                                    
                            $terms_list = wp_get_post_terms($post->ID, 'field');                                        
                            $i = 0;                                     
                            foreach($terms_list as $term){ $i++;                                            
                            if($i > 1){                                             
                            echo ', ';                                          }                                           
                            ?><span class="<?php echo $term->name ?> font-icon"></span><?php                                        
                            }                                   
                            ?>
                                <h4 class="product_title">                                      
                                <?php the_title(); ?>                                   
                                </h4>
                            </div>
                            <p class="category_excerpt"><?php echo get_the_excerpt(); ?></p>                            
                        </div>
                        <div class="box_buttons flex">
                            <a class="category_title" href="/field/<?php echo $term->name ?>">
                                <?php
                                 $terms_list = wp_get_post_terms($post->ID, 'field');
                                    $i = 0;
                                    foreach($terms_list as $term){ $i++;
                                        if($i > 1){
                                            echo ', ';
                                        }
                                        echo $term->name. ' ';
                                    }
                                 ?>
                            </a>
                            <a href="<?php the_permalink(); ?>" class="read_more">
                                Learn More
                            </a>
                        </div>
                    </div>

                    </div>
                </div>

           <?php endwhile;
            $total_pages = $loop->max_num_pages;
        }
        wp_reset_postdata();
    ?>
</div>

For each product I have a read more that leads to the single products file and the category name shown. When I click on a category name it leads to the taxonomy page that I created which looks like this:

<?php
if ( have_posts() ) : ?>    
    <?php
    /* Start the Loop */
    while ( have_posts() ) : the_post();
        echo '<div class="box">';
        the_post_thumbnail();
        the_title();
        the_excerpt();
        echo '<a href="http://www.parilinx.burnnotice.co.za/product/<?php the_title() ?>" class="read_more">';
        echo 'Learn More';  
        echo '</a>';
        echo '</div>';
    endwhile;
    the_posts_navigation();
else :
    get_template_part( 'template-parts/content', 'none' );
endif; 
?>

But now the problem in the category page is that I am not being able to hyperlink the read more button.

Every time I click on the read more it should lead me back to the single product page but it does not.

I first tried to use just: " class="read_more"> but it does not work.

Hope you can help.

1
  • 1
    Have you considered using archive-products.php and the standard products CPT archive at /products/ rather than recreating it with a page template?
    – Tom J Nowell
    Commented Oct 23, 2017 at 11:11

1 Answer 1

2

use the_permalink or get_the_permalink to display/retrieve the URL of a post ( a post of any type, they're all the same internally )

e.g.

the_permalink();

orx

<a href="<?php echo esc_url( get_the_permalink( $post_id ) ); ?>">

Notice I used esc_url to escape the value for security reasons.

Here are some similar functions you might find useful:

  • get_post_type_archive_link('product') should return /product/ or the equivalent archive for that post type that WP generates
  • get_term_link( 'purple', 'colours') fetches the archive link for posts that have the term purple in the custom taxonomy colours. Note that this can also be used for tags and categories. The functions such as tag_link etc all use this function inside them so cut out the middle man!
  • get_author_posts_url(1) gets the URL of the author posts archive for the user with ID 1
  • get_day_link( $year, $month, $day ) gets the link to a date archive by day
  • get_month_link, get_year_link, same as above
  • get_comment_link( $comment ), get the permalink to a comment

Note that all of these should be escaped with esc_url at the moment of output, with the exception of the_permalink which escapes internally

3
  • I've seen this before, but are there any circumstances under which get_permalink would be something that needs to be escaped? Commented Oct 23, 2017 at 12:18
  • I used the second option and it works just perfect. Appreciate the support Commented Oct 23, 2017 at 12:19
  • @JacobPeattie I always escape get_permalink, the convention in core is that if it starts with get_ it needs escaping. This is because when you escape you always do it at the moment of output. If the get_ functions escaped then you'd end up double escaping which can be dangerous, so they pass that responsibility on to you. the_ style functions however output internally, and so they have to be responsible for their own escaping. If in doubt escape, and never rely on trust
    – Tom J Nowell
    Commented Oct 23, 2017 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.