php - Wordpress my query post doesn't display my post with category

520

I am new to wordpress, I have a post that has a category_name of offer and it has a content, here is the permalink :http://localhost/jcjohn/2016/09/20/what-we-offer/

Now I want to display the contents of my post from my section page.

Here is my code inside the section :

<section id = "offer">
<?php    
    $args = array(
        'type' => 'post',
        'posts_per_page' => 3,
        'category_name' => 'offer',
    );
    $offerBlog = new WP_Query($args);

    if ($offerBlog->have_post()):
        while ($offerBlog->have_posts()):
            $offerBlog->the_post();
            get_template_part('content', get_post_format()); 
        endwhile;
    endif;
    wp_reset_postdata();    
?>
</section>
127

Answer

Solution:

So here is what you would need to do to display the post on the single page. You seem to have missed thes fromhave_post so it needs to be like below

Note: This would go insideindex.php

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'category_name' => 'offer',
);
$offerBlog = new WP_Query( $args );
?>
<!-- Blog Article -->
<section id="blog">
    <?php
    if ( $offerBlog->have_posts() ) :
        while ( $offerBlog->have_posts() ) : $offerBlog->the_post();

        the_content();

        endwhile;
    else : ?>
        <div class="no-content">
            <h3>Well... it looks like we forgot to put content here.</h3>
        </div>
    <?php
    endif;
    wp_reset_postdata();
    ?>
</section>
388

Answer

Solution:

You try using this loop with thecat_id that you have for the category you create.

query_posts( array( 'cat' => '1', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'DESC' ) );

You can have a try of the replaced code as follows.

<section id = "offer">
<?php    
    $args = array(
        'post_type' => 'post',
        'cat' => '1',
        'posts_per_page' => 5,
        'orderby' => 'title', 
        'order' => 'DESC' 
    );
    $offerBlog = new WP_Query($args);

    if ($offerBlog->have_posts()):
        while ($offerBlog->have_posts()):
            $offerBlog->the_post();
            get_template_part('content', get_post_format()); 
        endwhile;
    endif;
    wp_reset_postdata();    
?>
</section>

You have missed the loops format.

Reference:

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

People are also looking for solutions to the problem: php - Laravel sounds like magic

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.