0

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).

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.

0

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.

  • This is the route I'm going. The developer before me didn't use the archives, but everything else was done through CPT and pages. I had a friend look over the code with me. We haven't figured out why it's not loading, but I'll accept this answer because this is definitely the way to handle it! Thanks for your help @gfaw (: – Perry Jan 21 '15 at 23:40
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(); ?>">
      <?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.

-1

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

  • Sorry for the late reply. Thanks for your idea, but it won't work in this case. The organization already has established categories for products and whatnot. Changing any of that would break everything and I don't think cleaning that up would be fun D; P.S. I dunno who downvoted this, but I appreciate the response! – Perry Jan 21 '15 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.