簡體   English   中英

Wordpress 標簽和類別顯示在后端(后期編輯頁面)但不在前端?

[英]Wordpress Tags and Categories show on backend (post edit page) but not on frontend?

我創建了一個自定義帖子類型“項目”並附加了一個客戶分類“服務”。 我已將分類法設置為類別(我也嘗試了標簽,但仍然有同樣的問題)。 它顯示在 wordpress 管理員中,我可以在項目帖子上創建標簽/類別。 標簽/類別保存在數據庫中,因為它們仍然出現在帖子編輯頁面上。 但是,我很難將它們顯示在前端。 請從我的 function.php、archive-projects.php 和 single-projects.ZE091BEECFD76203E81E. 請注意,single-project.php 文件還包括一個名為“project-collection”的自定義字段,它遍歷各個項目組合圖像和副本。 此外,標簽根本不會在 HTML 中呈現(在 Google Chrome 中檢查元素時)。 謝謝

功能。php

    // Custom Post Types
    
    function create_projects_post_type() {
    
            $args = array(
                'labels' => array(
                    'name' => 'Projects',
                    'singular_name' => 'Project'
                ),
                'hierarchical' => false,
                'menu_icon' => 'dashicons-portfolio',
                'public' => true,
                'has_archive' => true
            );
    
            register_post_type('projects', $args);
    
    }
    
    add_action('init', 'create_projects_post_type');


// Custom Taxonomy

function create_taxonomy(){

    $args = array(
        'labels' => array(
            'name' => 'Services',
            'singular_name' => 'Service'
        ),
        'public' => true,
        'has_archive' => true,
        'hierarchical' => true
    );

    register_taxonomy('services', array('projects'), $args);

}
add_action('init', 'create_taxonomy');

存檔項目.php

<?php

get_header();

?>


<div class="row vertical-align d-flex align-items-center pt-0" data-aos="fade-up">
        <div class="col-md-8 offset-md-2 col-lg-6 offset-lg-3 text-center z-index">
          <h1>Our Projects</h1>
          <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form.</p>
        </div>
      </div>
    </div>
      <div id="particles-js"></div>
  </section>
  <section id="portfolio" class="container-fluid">
    <div class="container-fluid">
      <div class="row">

    <?php if(have_posts('projects')) : ?>

        <?php while(have_posts('projects')) : the_post(); ?>
        
        <div class="col-md-6">
            <figure data-aos="fade-up">
                <a href="<?php the_permalink(); ?>">
                    <?php $image = get_field('project_main_image'); ?>
                    <img class="img-fluid" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
                </a>
                <figcaption>
                    <h3>
                        <a href="<?php the_permalink(); ?>">
                            <?php echo get_field('project_name'); ?>
                        </a>
                    </h3>
                    <dl>

                    <?php 
                        $tags = get_the_tags();
                        if($tags):
                            foreach($tags as $tag): ?>
                            
                                <dd>
                                    <a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>           
                                </dd>
                                
                    <?php endforeach; endif; ?>


                    </dl>
                </figcaption>
            </figure>
        </div>  


        <?php endwhile; ?>

        <?php endif; ?>

      </div>
    </div>
  </section>

<?php get_footer(); ?>

單個項目。php

<?php

$GLOBALS['background'] = get_field('gradient_class');

get_header();

?>
<div class="row vertical-align d-flex align-items-center pt-0">
        <div class="col-md-8 offset-md-2 text-center z-index">
          <h1><?php the_field('project_name'); ?></h1>
          <?php the_field('project_description'); ?>
        </div>
      </div>
    </div>
    <div id="particles-js"></div>
  </section>

    <?php if(have_rows('project_collection')): ?>

        <?php $i = 1; ?>

        <?php while(have_rows('project_collection')): the_row(); ?>

        <section class="container-fluid portfolio-item">
            <div class="container-fluid">
                <div class="row d-flex align-items-center">
                    <div class="col-md-6 <?php if($i%2 != 0) : ?>order-md-2 <?php endif; ?>portfolio-image">
                        <?php $image = get_sub_field('image'); ?>
                        <img class="img-fluid" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
                    </div>  
                    <div class="col-md-6 portfolio-content">
                        <h2><?php the_sub_field('sub_title'); ?></h2>
                        <?php the_sub_field('section_description'); ?>
                    </div>
                </div>
            </div>
        </section>        
       
        
        <?php $i++; ?>

        <?php endwhile; ?>

    <?php endif; ?>

    
    <?php 
    // Get Tage NOT WORKING!
        $tags = get_the_tags();
        if($tags):
            foreach($tags as $tag): ?>
            
                <dd>
                    <a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>           
                </dd>
                
    <?php endforeach; endif; ?>

    
    <?php 
    // Get Catergories NOT WORKING!
    
    $categories = get_the_category();

        foreach($categories as $cat): ?>
            <dd>
                <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a>           
            </dd>
    <?php endforeach; ?>

    <?php get_template_part('includes/section', 'result-slider'); ?>


<?php get_footer(); ?>

我認為不要為您的標簽使用此代碼塊

 $tags = get_the_tags();
    if($tags):
        foreach($tags as $tag): ?>
        
            <dd>
                <a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>           
            </dd>
            
<?php endforeach; endif; ?>

試試這個代碼塊。 我認為它會解決你的問題

 $tags = wp_get_post_terms(get_the_ID(), 'Your tag/taxonomy slug name', array("fields" => "all"));
    if($tags):
        foreach($tags as $tag): ?>
        
            <dd>
                <a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a>           
            </dd>
            
<?php endforeach; endif; ?>

明確地說,不要使用get_the_tags()使用wp_get_post_terms() function。 我希望這能解決您的標簽問題。 :)

增強現實。 阿里夫,非常感謝你的回答。 它幫助我解決了下一個問題。 我為投資組合項目添加了類別。 在管理面板中沒問題,但前端的 output 很奇怪。 一個投資組合項目的類別是 output 靠近另一個項目。

所以我替換了這個 $portfolio_categories = get_the_terms(get_the_ID(), 'portfolio_category');

有了這個 $portfolio_categories = wp_get_post_terms(get_the_ID(), 'portfolio_category', array("fields" => "all"));

在主題的模板/分類法/categories-portfolio.php 中(當然在子主題中)

它最終正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM