简体   繁体   中英

How to loop through custom posts in Wordpress and display according to page title

Does anyone know how you would display a custom post type according to what post type you are on?

For example if I go to my www.url.com/services/digital page I want to show the digital portfolio or if I go to my www.url.com/services/audio page I want to show the audio portfolio.

Just to clarify I have two post types -

  1. Services
  2. Portfolio

The code I have wrote works fine but surely there must be a simpler way to do it as I have six categories I don't want to write out this code six different times on the same page

    <?php

        if ( is_single( 'digital' ) ) {

            $the_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page'=>'3', cat=>'9') );

            while ( $the_query->have_posts() ) : $the_query->the_post();

                echo'<div class="group service_portfolio">';

                    echo'<div class="service_portfolio_left">';

                        echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'Client'; echo'</h2>';

                        echo'<p style="color:#757573; margin-bottom:5%;">'; echo the_title(); echo'</p>';

                        echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'What we done'; echo'</h2>';

                        echo '<p style="color:#757573; margin-bottom:5%;">'; echo the_field('what_was_done'); echo'</p>';

                        echo'<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'See for yourself'; echo'</h2>';

                        echo '<p class"bottom_p">'; echo'<a href="http://'; the_field('portfolio_url'); echo'">'; echo the_field('portfolio_url'); echo' </a>'; echo '</p>';

                    echo'</div>';

                echo'<div class="service_portfolio_left" style="text-align:right;">';

                echo the_post_thumbnail();

                echo'</div>';

                echo'</div>';


                    endwhile;
                    wp_reset_postdata();
                }

                ?>

   <!-- Output print work -->

    <?php

        if ( is_single( 'print' ) ) {

            $the_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page'=>'3', cat=>'10') );  

            while ( $the_query->have_posts() ) : $the_query->the_post();

                echo'<div class="group service_portfolio">';

                    echo'<div class="service_portfolio_left">';

                        echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'Client'; echo'</h2>';

                        echo'<p style="color:#757573; margin-bottom:5%;">'; echo the_title(); echo'</p>';

                        echo '<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'What we done'; echo'</h2>';

                        echo '<p style="color:#757573; margin-bottom:5%;">'; echo the_field('what_was_done'); echo'</p>';

                        echo'<h2 style="color:#52514e; font-size:1.5em; margin-bottom:5%;">'; echo'See for yourself'; echo'</h2>';

                        echo '<p class"bottom_p">'; echo'<a href="http://';  the_field('portfolio_url'); echo'">'; echo the_field('portfolio_url'); echo' </a>'; echo '</p>';

                    echo'</div>';

                echo'<div class="service_portfolio_left" style="text-align:right;">';

                echo the_post_thumbnail();

                echo'</div>';

                echo'</div>';


                    endwhile;
                    wp_reset_postdata();
                }

                ?>

Sounds to me that you want to use a 6x archive templates ( archive-services.php , archive-digital.php etc.) since that is the standard way of presenting CPTs and it removes the need to the conditional and query parts. Then in each one of those you can use a template part the markup within the loop; this way you can reuse the same markup for all 6x templates easily, using:

<?php get_template_part( $slug, $name ); ?> 

Doing it this way gives you some flexibility whilst avoiding all the repetition. So your 6x archive templates might look like this:

<?php
    get_header();

    if(have_posts()) : while(have_posts()) : the_post();

        get_template_part( 'loop', 'preview' );

    endwhile; endif;

    get_footer();
?>

That will load loop-preview.php , which will contain the mark up to go within the loop.

EDIT: @AndrewBartel's comment made me realised that I was over-thinking this. If the 6x categories you mentioned are only being used for your custom post types, then you can create 6x category templates instead ( category-digital.php , category-audio.php etc.) These will display all posts that are within that category. That will probably be the most straightforward way of handling it; the template part bit is still valid. Check the WP Codex entry for templates for more info.

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