1
votes

I want to create a loop which get post of a specific category ID. first post is the latest for this category and the others are the posts under.

So i have a category with ID = "190", How to get this posts as per this design? >> 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  -->

Update:

I have a Visual composer shortcode function that i'll take the category ID from the VC and create an element with this. ( $type is the category ID value )

<?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');

That's what i tested so far and i got this error. PHP Error i'm getting

2
Have you tried any loops?Antonios Tsimourtos
For that memory issue, you can add this in wp-config.php and try, //increase WP Memory Limit define(‘WP_MAX_MEMORY_LIMIT’, ‘256M’);Prabu
Thanks it worked fineEiad Ashraf

2 Answers

1
votes

Try this one,

 $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..
    }
 }

You have to put the category ID. If you have the category name, then use this:

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

Issue is resolved. Many Thanks Prabu, the problem fix was to add

 $the_query-> 
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; ?>