【问题标题】:How do I create the category pages?如何创建类别页面?
【发布时间】:2017-11-04 22:21:07
【问题描述】:

我正在做类似的事情。我有一个食谱博客,我会有不同的类别,如早餐、甜点、午餐等……但我不知道如何将它们链接到类别页面。例如,如果用户点击早餐类别的“查看全部”,它将显示早餐类别中的所有早餐帖子。这意味着它将链接到主要的早餐类别页面。对于每个类别,我将显示 10 个最近的帖子,因此用户不会看到所有帖子。因此,如果他们单击“查看全部”链接,他们可以选择查看所有帖子。它会将它们链接到包含所有早餐帖子的主类别页面。当然如果他们点击甜点“查看全部”链接,他们会链接到甜点类别页面,他们可以看到所有甜点帖子。

这是我当前的代码: (但是我应该为“查看全部”链接添加什么 php 代码?如果您查看我的“查看全部”标签,现在是 #。它没有链接到某个地方,但是我如何使其动态链接到类别页面。例如,如果我在甜点部分,如果我点击“查看全部”,它将链接到甜点类别。)

也请看看我的设计

<?php 

get_header();

?> 

<!-- recipe -->
<section class="recipe-wrap">

	<?php
	/*
	 * Loop through Categories and Display Posts within
	 */
	$post_type = 'recipe';
	$category_link = get_category_link($cat->cat_ID);
	 
	// Get all the taxonomies for this post type
	$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
	 
	foreach( $taxonomies as $taxonomy ) :
	 
	    // Gets every "category" (term) in this taxonomy to get the respective posts
	    $terms = get_terms( $taxonomy );
	 
	    foreach( $terms as $term ) : ?>

	<div class="recipe-category owl-carousel-slide">
		<div class="row">

			<h2><?php echo $term->name; ?><a href="#">see all</a></h2>
	        
	        <div class="recipe-category-carousel owl-carousel owl-theme">

	        	<?php
		        $args = array(
		                'post_type' => $post_type,
		                'posts_per_page' => 10,  //show all posts
		                'tax_query' => array(
		                    array(
		                        'taxonomy' => $taxonomy,
		                        'field' => 'slug',
		                        'terms' => $term->slug,
		                    )
		                )
		 
		            );
		        $posts = new WP_Query($args);
		 
		        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

	            <div class="item recipe-box">

	            	<a href="<?php the_permalink(); ?>">
	                	<img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
	                	<p><?php the_title(); ?></p>
	                </a>
	            </div> 

                <?php endwhile; endif; ?>
			        </div>
			       
			        </section>
			 
			    <?php endforeach;
			 
			endforeach; ?>

	        </div>

        </div>
    </div>
</section>
<!-- /recipe -->

<?php 

get_footer();

?>

请查看我提供的示例,以便您知道我在说什么。 我附上了一些图片和链接。

这是链接的示例。

这是博客链接

https://iamsteve.me/blog

Design Category link

https://iamsteve.me/blog/category/design

An example what I want

【问题讨论】:

    标签: php html wordpress categories advanced-custom-fields


    【解决方案1】:

    如果您尝试链接到每个类别页面,您可以像这样使用get_term_link()

    $category_page_link = get_term_link($term);
    
    <h2><?php echo $term->name; ?><a href="<?php echo esc_url( $category_page_link ); ?>">see all</a></h2>
    

    以上内容第二个 foreach 循环中。

    编辑:

    这是完整的代码,我把函数get_term_link()放在它应该在的地方。此外,您在打开/关闭 html 标签时遇到了一些问题:

    <?php get_header(); ?>
    
    <?php
    /*
     * Loop through Categories and Display Posts within
     */
    $post_type = 'recipe';
    $category_link = get_category_link($cat->cat_ID);
    
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
    ?>
    <section class="recipe-wrap">
    
    <?php foreach ( $taxonomies as $taxonomy ) :
    
        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy );
    
        foreach( $terms as $term ) :
            $term_page_link = get_term_link($term); ?>
    
            <div class="recipe-category owl-carousel-slide">
                <div class="row">
                    <h2><?php echo $term->name; ?><a href="<?php echo esc_url ( $term_page_link ); ?>">see all</a></h2>
                    <div class="recipe-category-carousel owl-carousel owl-theme">
                        <?php
                        $args  = array(
                            'post_type'      => $post_type,
                            'posts_per_page' => 10,  //show all posts
                            'tax_query'      => array(
                                array(
                                    'taxonomy' => $taxonomy,
                                    'field'    => 'slug',
                                    'terms'    => $term->slug,
                                )
                            )
    
                        );
                        $posts = new WP_Query( $args );
    
                        if ( $posts->have_posts() ):
                            while ( $posts->have_posts() ) : $posts->the_post(); ?>
                                <div class="item recipe-box">
                                    <a href="<?php the_permalink(); ?>">
                                        <img src="<?php echo( types_render_field( 'artwork', array( 'raw' => true ) ) ); ?>">
                                        <p><?php the_title(); ?></p>
                                    </a>
                                </div>
                            <?php endwhile; ?>
                        <?php endif; ?>
    
                    </div><!-- .owl-theme -->
                </div><!-- .row -->
            </div><!-- owl-carousel-slide -->
    
        <?php endforeach; // terms loop ?>
    
    <?php endforeach; // taxonomies loop ?>
    
    </section><!-- /recipe -->
    
    <?php get_footer(); ?>
    

    希望对您有所帮助。

    【讨论】:

    • 我尝试过,但它不起作用。我应该在哪里添加这个 $category_page_link = get_term_link($term);?
    • 把它放在第二个foreach循环中,下面foreach( $terms as $term ): ?&gt;。如果还是不行,写var_dump($category_page_link)下面$category_page_link = get_term_link($term);复制结果。
    • 我试了,还是不行。我想把截图发给你,但是我怎么把它发到这里?
    • 我得到了这个代码 "$category_page_link = get_term_link($term); var_dump($category_page_link);"当我预览我的网站时。你知道为什么吗?
    • 确保将此代码放入 php 标签中:&lt;?php $category_page_link = get_term_link($term); var_dump($category_page_link); ?&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多