Custom Posts on Different Pages

So first off let me start off saying I’m new to WordPress development. I know HTML/CSS/JS like the back of my hand, but PHP and it’s functions are new.

Anyway. My problem may be simple, so here goes: I’m working with a client who has several pages. He has a blog page and a testimonial page that both use posts. I’ve managed to get the WP Dashboard to show a tab to add the custom post (Posts, Speaking, Press, and GiveFirst pictured below). Now I need to get just these posts to appear on the specific page (i.e. Speaking posts go to example.com/speaking and Press goes to example.com/press).

0pe7d 1

My research has only led me to figuring out that I can create custom-post-types. Now I just need to know where to go from there. Any help or redirection would be appreciated!

P.S. I also can’t get custom fields to work…so if anyone can guide me there that’d be awesome. But lower priority for now.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Make sure that the Custom Post Type is public. Then create a page with the same name (slug). That shall do. To customize them make sure the CPT has an archive, then create archive templates such as archive-press.php, archive-speaking.php.

Method 2

as per below code custom taxonomy post_type is “event” , taxonomy is “eventcategory” , so inside theme folder create event.php file and add below code inside it.

     <?php
/**
 * Template Name: Event
 *
 * Used to display archive-type pages if nothing more specific matches a query.
 * For example, puts together date-based pages if no date.php file exists.
 *
 * If you'd like to further customize these archive views, you may create a
 * new template file for each one. For example, tag.php (Tag archives),
 * category.php (Category archives), author.php (Author archives), etc.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */
get_header(); ?>
        <?php $temp = $wp_query; $wp_query= null;
                $Args = array(
                            'post_type' => 'event',
                            'showposts' => 6,
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'eventcategory',
                                    'field' => 'slug',
                                ),
                            ),
                            'paged'=>$paged,
                        );
               $wp_query = new WP_Query();
               $wp_query->query($Args);
               while ($wp_query->have_posts()) : $wp_query->the_post(); 
                get_template_part( 'content', 'event' );    
              endwhile;

then create content-event.php inside theme folder and here you can add below code (you can modify as per your needs and fields you want)

<li>          

<?php 
    if ( has_post_thumbnail() ) {
         $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
         $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full', false );
?>
<div class="events-img">
  <?php 
                   if($image_attributes[1] > 325){
                              $width = 325;
                          }else{
                              $width = $image_attributes[1]; 
                          }

                          if($image_attributes[2] > 240){
                              $height = 240;
                          }else{
                              $height = $image_attributes[2]; 
                          }

     echo '<img src="'.$image_attributes[0].'" width="'.$width.'" height="'.$height.'"/>';

  ?>
</div>  
<?php } ?>

 <h3><a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener">
      <?php  if (strlen($post->post_title) > 25) {
             echo substr(the_title($before = '', $after = '', FALSE), 0, 25) . '...'; } else {
           the_title();
        }  ?></a></h3>

<?php echo get_the_time('F d', $post->ID); ?>
 <?php
        $terms = get_the_terms( $post->ID, 'eventcategory' );

        if ( $terms && ! is_wp_error( $terms ) ) : 

            $draught_links = array();

            foreach ( $terms as $term ) {
                $draught_links[] = $term->name;
            }

            $on_draught = join( " , ", $draught_links );
        ?>
         <!--<?php echo ", ".$on_draught; ?>!-->
         <?php endif; ?>

   <p><?php echo excerpt(150); ?></p>

   <?php
    //the_content();

    wp_link_pages( array(
        'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'solid_rock' ) . '</span>',
        'after'       => '</div>',
        'link_before' => '<span>',
        'link_after'  => '</span>',
        'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'solid_rock' ) . ' </span>%',
        'separator'   => '<span class="screen-reader-text">, </span>',
    ) );
    ?>

then inside your themes functions.php file add below function

function excerpt($limit) {
    return wp_trim_words(get_the_excerpt(), $limit);
}
function solid_trim_excerpt($text)
{
    return '...';
}
add_filter('excerpt_more', 'solid_trim_excerpt');

Create 1 page in admin named “Events” and select custom template events like this screenshot : http://prntscr.com/flvw4j

So you can access page like yourwebsiteurl/events , slug of your custom page where you want to load custom post type .
Hope it helps you.

Method 3

Why not just use categories? Everything posted under the category “Speaking” goes to domain.com/speaking if you set the permalinks right 🙂


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x