简体   繁体   中英

Loop custom post type for Archive page in Wordpress

This is for me very hard code for the moment. I want create a Year Archive Page of a custom post type. My custom post type is a magazine with articles. In 1 year there is 6 magazines. Every magazine has a image.

This my code for year loop:

<?php 
  $my_archives=wp_get_archives(array(
    'post_type'=>'issue_number', 
    'type'=>'yearly', 
    'format' => 'custom',


    'before' => '
    <h3 class="entry-title mh-loop-title archivio-anno-list">Table of contents<br>
    ',
    'after' => '

    <br></h3>',
    'show_post_count'=>true, 
    'limit'=>20, 

      ));

   print_r($my_archives); 
 ?>

I also want to see the image of magazine for year as in this image:

在此处输入图片说明

How to do? Give me a way for solution!

create your taxonomy file for example "taxonomy-yourtaxonomyslug.php" then use this below code for taxonomy/category.

<?php
    $magazine_category_object=get_queried_object();
    $magazine_category_taxonomy_slug = $magazine_category_object->slug;
    $magazine_category_term_id = $magazine_category_object->term_id;

    $magazine_type = 'magazine';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $magazine_args=array(
        'type'                     => $magazine_type,
        'post_status'              => 'publish',
        'posts_per_page'           => 4,
        'paged'                    => $paged, 
        'caller_get_posts'         => -1,
        'child_of'                 => 0,
        'parent'                   => 0,
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'tax_query'                => array(
                                            array(
                                                'taxonomy' => $magazine_category_taxonomy_slug,
                                                'field' => 'id',
                                                'terms' => $magazine_category_term_id
                                            )
                                        ),
        'pad_counts'               => false 
    );
    $magazine_my_query = null;
    $magazine_my_query = new WP_Query($magazine_args);

    if( $magazine_my_query->have_posts() ) 
    {

        while ($magazine_my_query->have_posts()) : $magazine_my_query->the_post(); 
            $magazine_description = get_the_excerpt($post->ID);

             the_title( '<h1>', '</h1>' );

            if ( has_post_thumbnail() ) {
                    //get_the_post_thumbnail( $post->ID, array( 100, 100) ); 
                    echo  get_the_post_thumbnail($post->ID,"thumbnail"); //thumbnail,medium,large,full,array(100,100)

            }
            echo $magazine_description; 
            echo '<a href="<?php echo get_permalink(); ?>">Read More...</a>';
        endwhile;

    }
    wp_reset_query($magazine_my_query);
 ?>

post type : issue_number

<?php
        $issue_number_type = 'issue_number';
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $issue_number_args=array(
            'type'                     => $issue_number_type,
            'post_status'              => 'publish',
            'posts_per_page'           => 20,
            'paged'                    => $paged, 
            'caller_get_posts'         => -1,
            'type'=>'yearly', 
            'format' => 'custom',
            'show_post_count'=>true, 
            'limit'=>20 
        );
        $issue_number_my_query = null;
        $issue_number_my_query = new WP_Query($issue_number_args);

        if( $issue_number_my_query->have_posts() ) 
        {

            while ($issue_number_my_query->have_posts()) : $issue_number_my_query->the_post(); 
                $issue_number_description = get_the_excerpt($post->ID);

                 the_title( '<h1>', '</h1>' );

                if ( has_post_thumbnail() ) {
                        //get_the_post_thumbnail( $post->ID, array( 100, 100) ); 
                        echo  get_the_post_thumbnail($post->ID,"thumbnail"); //thumbnail,medium,large,full,array(100,100)

                }
                echo $issue_number_description; 
                echo '<a href="<?php echo get_permalink(); ?>">Read More...</a>';
            endwhile;

        }
        wp_reset_query($issue_number_my_query);
     ?>

This is my post type with CPT UI plugin.

在此处输入图片说明

RESOLVED

I resolved the problem adding a new custom post type called ANNO and 1 relationship field called anno_issue from year to issue_number. The code is this for query :

    <?php

     // args
   $args = array(
     ‘numberposts’  => -1,
     ‘post_type’    => ‘anno’,
      ‘posts_per_page’ => 100,
       );

     // query
        $the_query = new WP_Query( $args );

         ?>

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

        <?php

        /*
       * Query posts for a relationship value.
            * This method uses the meta_query LIKE to match the string “123” 
 to the database value a:1:{i:0;s:3:”123″;} (serialized array)
    */

    $posts = get_posts(array(
      ‘post_type’ => ‘issue_number’, //nome post type da dove recupero le 
         info
  ‘posts_per_page’ => -1,
    ‘meta_query’ => array(
   array(
  ‘key’ => ‘anno_issue’, // nome custom field all’interno di post che mi da 
     l’info
    ‘value’ => ‘”‘ . get_the_ID() . ‘”‘, // matches exaclty “123”, not just 
    123. This prevents a match for “1234”
   ‘compare’ => ‘LIKE’
   )
  )
  ));

   ?>
     <?php /*ciclo stampa articoli */   ?>
  <?php if( $posts ): ?>

      <?php foreach( $posts as $post ): ?>
   <?php /*layout 1 del tema per articoli */    ?>
    <article >

    —HTML CODE DISPLAYNG YOUR LOOP—
     </article>
    <?php /*fine layout 1 */    ?>
  <?php endforeach; ?>

 <?php endif; ?>

  <?php /*fine ciclo stampa articoli */ ?>

   <?php endwhile; // end of the loop. ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM