Wordpress - 特定类别的帖子循环

时间:2017-04-06 10:28:11

标签: php wordpress

我想创建一个获取特定类别ID的帖子的循环。 第一篇文章是此类别的最新文章,其他文章是其中的帖子。

所以我有一个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  -->

更新:

我有一个Visual Composer短代码函数,我将从VC中获取类别ID并使用它创建一个元素。 ( $ type是类别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

2 个答案:

答案 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>
);

答案 1 :(得分:0)

问题已解决。非常感谢 Prabu , 问题的解决方法是在

之前添加

 $the_query-> 
the_post();

&#13;
&#13;
    /* 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; ?>
&#13;
&#13;
&#13;