Wordpress 循环并使用 CTP 重复

发布于 2025-01-10 04:10:23 字数 5727 浏览 0 评论 0原文

https://codepen.io/SitePoint/pen/GQoVey

我正在使用找到的选项卡和砌体这里。

我一共有4个类别。 4 个类别 20 个内容

我用 WordPress 打印这个字段没有任何问题。

<main role="main" class="grid">

  <div class="row">
    <div class="col-md">
      <div class="starter-template">
        
            <ul class="nav nav-tabs" id="myTab" role="tablist">
    
                
        
                
                
            <?php 
    $args = array(
        'type'                     => 'hizmetler', 
        'parent'                   => '',
        'orderby'                  => 'id',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,   
        'taxonomy'                 => 'kategoriler'  /* custom post type texonomy name */
    ); 
    $cats = get_categories($args);
    foreach ($cats as $cat) {           
        $cat_id= $cat->term_id;
        $cat_name= $cat->name; ?>
        
                
                <li class="tag is-dark">
            <a class="nav-link" id="contact-tab" data-toggle="tab" href="#<?php echo ''.$cat->term_id.''; ?>" role="tab" aria-controls="<?php echo ''.$cat->term_id.''; ?>" aria-selected="false">
                <?php echo ''.$cat->name.''; ?>
                </a></li>
            <?php wp_reset_postdata(); ?>

    <?php  } ?> 
        
        </ul>

我可以获取类别名称、id 和链接作为 CPT。 问题从这里开始。

我不知道如何循环这个字段。

 <div class="tab-content" id="myTabContent">
         
    <div class="tab-pane masonry-container fade show active" id="home" role="tabpanel" aria-labelledby="1-tab">
         <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Card with stretched link</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
      </div>
    </div>
           </div>
   

循环区域;

<div class="card-body">
    <h5 class="card-title">Card with stretched link</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
  </div>

但该字段必须有一个与父div相关的类别名称。

例如:id=“家” 例如: term_id.''; ?>

这就是应该的

 <div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-1" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-1-tab">
                   
                   
           <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      
      <div class="card-body">
        <h5 class="card-title">POST 1 TİTLE (cat-1)</h5>
        <p class="card-text">POST 1 CONTENT</p>
        <p class="card-text">CAT 1 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
      <div class="card-body">
        <h5 class="card-title">POST 2 TİTLE (cat-1)</h5>
        <p class="card-text">POST 2 CONTENT</p>
        <p class="card-text">CAT 1 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
       <div class="card-body">
        <h5 class="card-title">POST 3 TİTLE (cat-1)</h5>
        <p class="card-text">POST 3 CONTENT</p>
        <p class="card-text">CAT 1 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
       
         
         
              </div>
         
         
              </div>
     
     
     
    <div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-2" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-2-tab">
                   
                   
           <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      
      <div class="card-body">
        <h5 class="card-title">POST 1 TİTLE (cat-2)</h5>
        <p class="card-text">POST 1 CONTENT</p>
        <p class="card-text">CAT 2 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
      <div class="card-body">
        <h5 class="card-title">POST 2 TİTLE (cat-2)</h5>
        <p class="card-text">POST 2 CONTENT</p>
        <p class="card-text">CAT 2 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
       <div class="card-body">
        <h5 class="card-title">POST 3 TİTLE (cat-2)</h5>
        <p class="card-text">POST 3 CONTENT</p>
        <p class="card-text">CAT 2 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
       
         
         
              </div>

https://codepen.io/SitePoint/pen/GQoVey

I am using Tabs and Masonry found here.

I have a total of 4 categories. 4 categories 20 contents

I'm printing this field with wordpress without any problems.

<main role="main" class="grid">

  <div class="row">
    <div class="col-md">
      <div class="starter-template">
        
            <ul class="nav nav-tabs" id="myTab" role="tablist">
    
                
        
                
                
            <?php 
    $args = array(
        'type'                     => 'hizmetler', 
        'parent'                   => '',
        'orderby'                  => 'id',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,   
        'taxonomy'                 => 'kategoriler'  /* custom post type texonomy name */
    ); 
    $cats = get_categories($args);
    foreach ($cats as $cat) {           
        $cat_id= $cat->term_id;
        $cat_name= $cat->name; ?>
        
                
                <li class="tag is-dark">
            <a class="nav-link" id="contact-tab" data-toggle="tab" href="#<?php echo ''.$cat->term_id.''; ?>" role="tab" aria-controls="<?php echo ''.$cat->term_id.''; ?>" aria-selected="false">
                <?php echo ''.$cat->name.''; ?>
                </a></li>
            <?php wp_reset_postdata(); ?>

    <?php  } ?> 
        
        </ul>

I can get category name, id and link as CPT.
The problem starts here.

I don't know how to loop this field.

 <div class="tab-content" id="myTabContent">
         
    <div class="tab-pane masonry-container fade show active" id="home" role="tabpanel" aria-labelledby="1-tab">
         <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Card with stretched link</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
      </div>
    </div>
           </div>
   

loop area;

<div class="card-body">
    <h5 class="card-title">Card with stretched link</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
  </div>

but this field must have a category name related to the parent div.

ex: id="home"
example: <?php echo ''.$cat->term_id.''; ?>

this is what should be

 <div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-1" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-1-tab">
                   
                   
           <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      
      <div class="card-body">
        <h5 class="card-title">POST 1 TİTLE (cat-1)</h5>
        <p class="card-text">POST 1 CONTENT</p>
        <p class="card-text">CAT 1 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
      <div class="card-body">
        <h5 class="card-title">POST 2 TİTLE (cat-1)</h5>
        <p class="card-text">POST 2 CONTENT</p>
        <p class="card-text">CAT 1 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
       <div class="card-body">
        <h5 class="card-title">POST 3 TİTLE (cat-1)</h5>
        <p class="card-text">POST 3 CONTENT</p>
        <p class="card-text">CAT 1 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
       
         
         
              </div>
         
         
              </div>
     
     
     
    <div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-2" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-2-tab">
                   
                   
           <div class="card" style="width: 18rem;">
      <img src="..." class="card-img-top" alt="...">
      
      <div class="card-body">
        <h5 class="card-title">POST 1 TİTLE (cat-2)</h5>
        <p class="card-text">POST 1 CONTENT</p>
        <p class="card-text">CAT 2 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
      <div class="card-body">
        <h5 class="card-title">POST 2 TİTLE (cat-2)</h5>
        <p class="card-text">POST 2 CONTENT</p>
        <p class="card-text">CAT 2 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
     
       <div class="card-body">
        <h5 class="card-title">POST 3 TİTLE (cat-2)</h5>
        <p class="card-text">POST 3 CONTENT</p>
        <p class="card-text">CAT 2 NAME</p> 
        <a href="#" class="btn btn-primary stretched-link">link Go somewhere</a>
      </div>
       
         
         
              </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不羁少年 2025-01-17 04:10:23

将以下代码放入您的functions.php 文件中。您可以将其用作任何短代码 - [tab_posts] 或 echo do_shortcode('[tab_posts]'); 。请记住,当您需要使用选项卡时修复 html。

function posts_per_tab($atts) {
    
    $output = array();

    //Setup our custom post args
    $args = array(
        'post_type'=>'post',
        'posts_per_page' => 3,
        'tax_query' => array(
            'taxonomy' => 'cat', // Change it with your taxonomy
            'field'    => 'id',
            'terms'    => $atts, 
        )
    );

    $results = new WP_Query($args);
    while($results->have_posts()): $results->the_post();
        $output[] .= '<div class="card-body">';
            $output[] .= '<h5 class="card-title">'.get_the_title().'</h5>';
            $output[] .= '<p class="card-text">'.get_the_content().'</p>';
            $output[] .= '<a href="'.get_the_permalink().'" class="btn btn-primary stretched-link">Go somewhere</a>';
        $output[] .= '</div>';
    endwhile;
    wp_reset_query();

    return $output;
}

function posts_in_category_tabs() {

    $tab_links   = array();
    $tab_contents = array();
    $tab_posts = array();
    $cargs = array(
        'type'                     => 'post', 
        'parent'                   => '',
        'orderby'                  => 'id',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,   
        'taxonomy'                 => 'category'
        ); 
    
    $cats = get_categories($cargs);
    
    if($cats):
        foreach($cats as $cat):

            //For each category build your tab 
            $tab_links[] = sprintf('<li><a href="#" class="cat-link">%s</a></li>',$cat->name);

            //We are executing 2nd loop in posts_per_tab function passing category id as parameter
            $tab_posts = posts_per_tab($cat->term_id);

            //For each tab push the posts per category 
            $tab_contents[] = sprintf('<div class="tabs-panel">%s</div>',implode( '', $tab_posts ));

        endforeach;
        wp_reset_postdata();
    endif;
    
    //Our navigation
    $tab_links = sprintf(
        '<ul class="tabs-nav">%s</ul>',
        implode( '', $tab_links ),implode( '', $tab_contents )
    );

    //Change container if needed
    return sprintf('<div class="tabs-wrapper">%s %s</div>',$tab_links,implode( '', $tab_contents ));
}
add_shortcode('tab_posts','posts_in_category_tabs');

Place the following code in your functions.php file. You can use it as any shortcode - [tab_posts] or echo do_shortcode('[tab_posts]'); . Just keep in mind to fix html as you need to work your tabs.

function posts_per_tab($atts) {
    
    $output = array();

    //Setup our custom post args
    $args = array(
        'post_type'=>'post',
        'posts_per_page' => 3,
        'tax_query' => array(
            'taxonomy' => 'cat', // Change it with your taxonomy
            'field'    => 'id',
            'terms'    => $atts, 
        )
    );

    $results = new WP_Query($args);
    while($results->have_posts()): $results->the_post();
        $output[] .= '<div class="card-body">';
            $output[] .= '<h5 class="card-title">'.get_the_title().'</h5>';
            $output[] .= '<p class="card-text">'.get_the_content().'</p>';
            $output[] .= '<a href="'.get_the_permalink().'" class="btn btn-primary stretched-link">Go somewhere</a>';
        $output[] .= '</div>';
    endwhile;
    wp_reset_query();

    return $output;
}

function posts_in_category_tabs() {

    $tab_links   = array();
    $tab_contents = array();
    $tab_posts = array();
    $cargs = array(
        'type'                     => 'post', 
        'parent'                   => '',
        'orderby'                  => 'id',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,   
        'taxonomy'                 => 'category'
        ); 
    
    $cats = get_categories($cargs);
    
    if($cats):
        foreach($cats as $cat):

            //For each category build your tab 
            $tab_links[] = sprintf('<li><a href="#" class="cat-link">%s</a></li>',$cat->name);

            //We are executing 2nd loop in posts_per_tab function passing category id as parameter
            $tab_posts = posts_per_tab($cat->term_id);

            //For each tab push the posts per category 
            $tab_contents[] = sprintf('<div class="tabs-panel">%s</div>',implode( '', $tab_posts ));

        endforeach;
        wp_reset_postdata();
    endif;
    
    //Our navigation
    $tab_links = sprintf(
        '<ul class="tabs-nav">%s</ul>',
        implode( '', $tab_links ),implode( '', $tab_contents )
    );

    //Change container if needed
    return sprintf('<div class="tabs-wrapper">%s %s</div>',$tab_links,implode( '', $tab_contents ));
}
add_shortcode('tab_posts','posts_in_category_tabs');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文