2017-04-06 81 views
1

我想創建一個循環,獲取特定類別ID的帖子。 第一篇文章是這個類別的最新文章,其他文章是下面的文章。WordPress的 - 特定類別後循環

所以我有一個類別的ID =「190」,如何獲得這個職位,按照這種設計? >>Check the Design here

<!-- Featured Post --> 
 
<div class="module-one-typo"> 
 
    <a href="#"><img src="img/sample/370x250.png?1491474115266" alt=""></a> 
 
    <h3><a href="#"><b>Trump names climate change skeptic and Oil industry ally to lead the EPA</b></a></h3> 
 
    <h5> 
 
     <span><i class="fa fa-calendar"></i> November 02, 2017 &nbsp;</span> 
 
     <span> &nbsp;<i class="fa fa-comments-o"></i> 10</span> 
 
     <span> &nbsp; Eyad Ashraf</span> 
 
    </h5> 
 
    <p>Lorem ipsum dolor sit amet, consectmagna aliqua. ex ea commodo consequat. Duis aute irure etur adipisicing elit, dolor in reprehenderit in voluptate velit .</p> 
 
</div> 
 

 
<!-- /Featured Post --> 
 

 
<!-- Older post Loop --> 
 
<div class="module-two-strips"> 
 
    <div class="module-one-strip row align-middle"> 
 
     <div class="columns shrink"><a href="#"><img src="img/sample/100x80.png" alt=""></a></div> 
 
     <div class="columns"> 
 
     <h5><a href="#"><b>The first Pirate Party was created in Sweden in 2006</b></a></h5> 
 
     <h5> 
 
      <span><i class="fa fa-calendar"></i> November 02, 2017 &nbsp;</span> 
 
      <span> &nbsp;<i class="fa fa-comments-o"></i> 100</span> 
 
     </h5> 
 
     </div> 
 
    </div> 
 
    <div class="module-one-strip row align-middle"> 
 
     <div class="columns shrink"><a href="#"><img src="img/sample/100x80.png" alt=""></a></div> 
 
     <div class="columns"> 
 
     <h5><a href="#"><b>The first Pirate Party was created in Sweden in 2006</b></a></h5> 
 
     <h5> 
 
      <span><i class="fa fa-calendar"></i> November 02, 2017 &nbsp;</span> 
 
      <span> &nbsp;<i class="fa fa-comments-o"></i> 100</span> 
 
     </h5> 
 
     </div> 
 
    </div> 
 
    <div class="module-one-strip row align-middle"> 
 
     <div class="columns shrink"><a href="#"><img src="img/sample/100x80.png" alt=""></a></div> 
 
     <div class="columns"> 
 
     <h5><a href="#"><b>The first Pirate Party was created in Sweden in 2006</b></a></h5> 
 
     <h5> 
 
      <span><i class="fa fa-calendar"></i> November 02, 2017 &nbsp;</span> 
 
      <span> &nbsp;<i class="fa fa-comments-o"></i> 100</span> 
 
     </h5> 
 
     </div> 
 
    </div> 
 
    <div class="module-one-strip row align-middle"> 
 
     <div class="columns shrink"><a href="#"><img src="img/sample/100x80.png" alt=""></a></div> 
 
     <div class="columns"> 
 
     <h5><a href="#"><b>The first Pirate Party was created in Sweden in 2006</b></a></h5> 
 
     <h5> 
 
      <span><i class="fa fa-calendar"></i> November 02, 2017 &nbsp;</span> 
 
      <span> &nbsp;<i class="fa fa-comments-o"></i> 100</span> 
 
     </h5> 
 
     </div> 
 
    </div> 
 
</div> 
 
<!-- Older post Loop -->

更新:

我有,我會採取從VC類別ID,並創建該元素的視覺作曲家簡碼功能。 ($類是類別ID值

<?php 
 

 
$args = array(
 
    'cat' => 194, //your category ID 
 
    'posts_per_page' => 10 
 
); 
 
$the_query = new WP_Query($args); 
 
?> 
 

 
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : the_post(); ?> 
 

 
<h1><?php the_title(); ?> </h1> 
 

 
<?php endwhile; else : ?> 
 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
 
<?php endif; ?> 
 

 

 

 

 
<!-- CUSTOM CONTENT --> 
 

 
    <?php 
 
    return ob_get_clean(); 
 

 
     
 
    } 
 
    add_shortcode('et_module', 'et_module');

這是我到目前爲止測試,我得到這個錯誤。 PHP Error i'm getting

+0

您是否嘗試過任何循環之前添加

 $the_query->
? –

+0

對於該內存問題,您可以在wp-config.php中添加此項,並嘗試使用 //增加WP內存限制 define('WP_MAX_MEMORY_LIMIT','256M'); – Prabu

+0

感謝它工作正常 –

回答

1

試試這個,

$args = array(
    'cat' => 190, --> your category ID 
    'posts_per_page' => 10 
); 
$the_query = new WP_Query($args); 

// The Loop 
if ($the_query->have_posts()) { 

    while ($the_query->have_posts()) { 

     // ## write your code here.. 
    } 
} 

你必須把類別ID。如果您有類別名稱,則使用以下內容:

$args = array(
    'category_name' => <your category name> 
); 
+0

您需要在代碼中添加Order。 –

+0

感謝您的回覆,我使用了您的代碼邏輯並做了一些更新,請檢查一下。 –

0

問題已解決。非常感謝Prabu, 問題修復是

the_post();

/* Function 2 */ 
 
    function et_module($atts, $content = null) { 
 
     extract(
 
      shortcode_atts(
 
       array(
 
        'type' => '', 
 

 
       ), 
 
       $atts 
 
      ) 
 
     ); 
 

 
    ob_start(); 
 
    ?> 
 

 

 

 
<!-- CUSTOM CONTENT --> 
 

 

 
<?php 
 

 
$args = array(
 
    'cat' => $type, //your category ID 
 
    'posts_per_page' => 10 
 
); 
 
$the_query = new WP_Query($args); 
 
?> 
 

 
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 
 

 
<h1><?php the_title(); ?> </h1> 
 

 
<?php endwhile; else : ?> 
 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
 
<?php endif; ?>