WordPress: how to display only posts that are in a certain category?

I'm new to WordPress, but I spent about 50 odd hours studying it, trying to figure it out, and I feel like now I got a pretty good pen.

However, one thing that I just can't get is for the page to spit out a list of posts in a certain category.

Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters

I have a message that if I go to it, it works correctly, but does not appear on this page.

Direct post link.

The category identifier is "3" and the category name is "typographic posters."

I have a custom page template for a typographic poster page that looks like this:

<?php
/*
Template Name: Typographic Posters
*/
?>

<?php get_header(); ?>
<?php get_sidebar(); ?>

<?php if (in_category('3')): ?>
<div class="post">

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


  <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
   <div class="post-description">
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
   </div>
   <?=get_image('flutter-image');?>
  </div>


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

</div>
<?php endif; ?>

<?php get_footer(); ?>

Using this code, however, the page only shows the title, sidebar and nothing else.

If someone can help me, it really helps me figure out this wordpress category filtering.

Thanks for reading,

Jannis

+3
source share
9 answers

in_categorywill only work outside the loop on one page. I suggest using a function query_poststo solve this problem. You can use query_posts('cat=3')or query_posts('category_name=typographic-posters')to get the messages you are looking for.

Once received, simply use the regular WordPress loop to access these posts.

+11

- category-3.php index.php category.php. Wordpress id = 3 .

+1

in_category . query_posts . query_posts ('cat = 3') query_posts ('category_name = typographic-posters'), , .

WordPress .

, , "" > "" "" .

+1

Id, :

               query_posts('cat=1&showposts=3');
                if (have_posts()) : while (have_posts()) :

                // if(1) {
                    //echo the_category_ID();
                 the_post();
                /**
                 * The default post formatting from the post.php template file will be used.
                 * If you want to customize the post formatting for your homepage:
                 * 
                 *   - Create a new file: post-homepage.php
                 *   - Copy/Paste the content of post.php to post-homepage.php
                 *   - Edit and customize the post-homepage.php file for your needs.
                 * 
                 * Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
                 */

                $is_post_wrap++;
                    if($is_post_wrap == '1') {
                        ?><div class="post-wrap clearfix"><?php
                    }
                    get_template_part('post', 'homepage');

                    if($is_post_wrap == '3') {
                        $is_post_wrap = 0;
                        ?></div><?php
                    }



            endwhile;

            else :
                get_template_part('post', 'noresults');
            endif; 
+1

:

<?php query_posts="cat=3&showposts=5">

5 (showposts = 5) 3 (cat = 3).

0

. 3.php . index.php category.php 3.php , . , .

, . Page of posts.

0

, . index.php category.php 3.php , .

-1

Source: https://habr.com/ru/post/1719203/


All Articles