• Resolved Tim W

    (@twalker123)


    I love this theme, but I have two custom post types and I would like to use custom page templates for their archive pages (mostly just to add a nice title and some intro text).

    What files do I need to create for a page template that runs the loop for a custom post type? Are there files included I can use as a guide?

    Thank you!

Viewing 15 replies - 16 through 30 (of 36 total)
  • Theme Author presscustomizr

    (@nikeo)

    yeah $wp_query = $wp_the_query !
    I love this soo WordPress loop secret ??

    don’t be that happy, I learned it 5 minutes ago from your snippet https://themesandco.com/snippet/three-techniques-to-alter-the-query-in-wordpress/
    ??

    Theme Author presscustomizr

    (@nikeo)

    ??

    Thread Starter Tim W

    (@twalker123)

    Thanks d4z_c0nf!

    It is running the loop, but it looks like it’s still wrapping the title anchor in its own tag (twice?) and encoding the anchor string that’s being returned. The post titles are also being displayed twice, once with the featured image and once without…

    https://dev.nuggetweb.com/clients/

    Tim

    Probably something to adjust in the way you get the link?
    Also, do you use some other filter to show the custom post content?
    I think I’d know something more about your custom posts. Is there any chance you can share it in some way, github or many pastebin?

    edit,
    can I also see how you changed my code above? ’cause I see that the thumbnail has no link..

    Thread Starter Tim W

    (@twalker123)

    Hi d4z,

    I don’t have any filters on the post content. If you look at the source, there are three anchors enclosing each other.

    Here is your code with my modifications:

    <?php
    /*
    Template Name: Clients
     */
    
    /* I use the action hook __after_loop
     * and partially replicate the customizr loop
     * to achieve:
     * 1) keep the page title
     * 2) show the page content if any, this can be used as a summary
     * before the list of posts
     */
    add_action('__after_loop', 'clients_list', 0);
    function clients_list(){
        add_filter('the_title', 'link_title_to_site', 0);
    
        global $wp_query;
        $wp_query = new WP_Query(array(
            /* change this with the query vars you need to display your cpts */
                'paged' => get_query_var('paged'),
                'post_type' => 'site'
            )
        );
        if ( have_posts() ) :
            while ( have_posts() ) :
                the_post();
                do_action ('__before_article');
                ?>
                    <article <?php tc__f('__article_selectors') ?>>
                        <?php do_action( '__loop' ); ?>
                    </article>
                <?php
                do_action ('__after_article'); 
    
            endwhile;
         endif; ##end if have posts
    }
    
    function site_link(){
        $href = get_post_meta($post->ID,'ct_Website_text_550e',true);
        return $href;
    }
    
    /* the title link */
    function link_title_to_site($title){
        /* your code to wrap the title into a link */
        return sprintf('<a href="%1$s" title="%2$s" target="_blank">%3$s</a>',
            site_link(),
            sprintf( __("Go to %s"), $title ),
            $title
        );
    }
    add_filter('tc_post_thumb_wrapper', 'change_thumbnails_link');
    function change_thumbnails_link($thumb){
        return preg_replace('/<a(.*?)href="(.*?)"(.*?)>/',
            '<a\1href="'.site_link().'"\3>',
            $thumb
        );
    }
    /*
     * in order to have the navigator we have to reset the custom query
     * with priority 30 (navigation has priority 20)
     */
    add_action('__after_loop', 'reset_query', 25);
    function reset_query(){
        global $wp_query, $wp_the_query;
        $wp_query = $wp_the_query;
    }
    
    /* let's remove customizr title filter on the title */
    remove_filter('the_title', array(TC_Headings::$instance, 'tc_content_heading_title'), 0);
    /* uncomment line below if you want to get rid of comments bubble */
    //remove_filter('the_title', array(TC_Headings::$instance, 'tc_add_comment_bubble_after_title'), 1);
    /* uncomment line below if you want to get rid of edit link */
    //remove_filter('the_title', array(TC_Headings::$instance, 'tc_add_edit_link_after_title'), 2);
    
    /*
     * let's include customizr index.php
     * this way we'll not lose eventual
     * changes on it
     */
    include_once( TC_BASE . 'index.php' );

    Here is the code I’m using to create the CPT:

    $nw_post_type = array(
              'labels' => array (
                'name' => 'Sites',
                'singular_name' => 'Site',
                'add_new_item' => 'Add New Site',
                'edit_item' => 'Edit Site',
                'new_item' => 'New Site',
                'view_item' => 'View Site',
                'search_items' => 'Search Sites',
                'not_found' => 'No sites found',
                'not_found_in_trash' => 'No sites found in trash',
              ),
              'taxonomies' => array('category'),
              'supports' =>
              array (
                'title' => 'title',
                'editor' => 'editor',
                'thumbnail' => 'thumbnail',
                'excerpt' => 'excerpt',
                'trackbacks' => 'trackbacks',
                'custom_fields' => 'custom-fields',
                'comments' => 'comments',
                'revisions' => 'revisions',
                'page_attributes' => 'page-attributes',
                'post_formats' => 'post-formats',
              ),
              'supports_reg_tax' =>
              array (
                'category' => '1',
                'post_tag' => '1',
              ),
              'capability_type' => 'post',
              'map_meta_cap' => true,
              'description' => 'NuggetWeb Websites',
              'menu_position' => 20,
              'public' => true,
              'hierarchical' => false,
              'has_archive' => true,
              'rewrite' =>
              array (
                'with_front' => true,
                'feeds' => false,
                'pages' => true,
                'ep_mask' => 0,
              ),
              'query_var' => true,
              'can_export' => true,
              'cf_columns' => NULL,
              );
        register_post_type( 'site', $nw_post_type );

    and let me understand, how I add the site link?
    Is a custom field named?
    About the “_target” issue, looks like this isn’t working for you:
    remove_filter('the_title', array(TC_Headings::$instance, 'tc_content_heading_title'), 0);
    I’m using customizr 3.1.9

    Thread Starter Tim W

    (@twalker123)

    According to the readme, I’m using Customizr Pro version 1.0.4 (just downloaded it from them last Thursday).

    I had already tried removing that filter as well (not quite so eloquently) but it didn’t make a difference for me either. Giorgio tried messing with it in a previous post as well and it threw him too.

    The custom meta box is dealt with in a separate part of the plugin. It’s not really created when the CPT is, so I kept is separate:

    add_meta_box(
            'ct_Website_text_550e', // $id
            'Website', // $title
            'show_custom_meta_box', // $callback
            'site', // $page
            'normal', // $context
            'high' // $priority
    		);

    Here is the loop from our current theme so you can see how it’s being linked:

    <?php while ( have_posts() ) : the_post(); ?>
    
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<div class="entry-utility">
    				<?php if ( comments_open() || ( '0' != get_comments_number() && ! comments_open() ) ) : ?>
    					<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'nuntius' ), __( '1 Comment', 'nuntius' ), __( '% Comments', 'nuntius' ) ); ?></span>
    				<?php endif; ?>
    				<?php edit_post_link( __( 'Edit', 'nuntius' ), '<span class="edit-link">', '</span>' ); ?>
    			</div><!-- .entry-utility -->
    
    			<a href="<?php echo get_post_meta($post->ID,'ct_Website_text_550e',true) ?>" rel="bookmark" target="_blank"><?php the_title( '<h2 class="entry-title">', '</h2><!-- .entry-title -->' ); ?></a>
    
    			<div class="entry-content">
                    Categories: <?php $separator = ", "; the_category( $separator, $parents, $post_id ); ?>
                    <?php if ( has_post_thumbnail() ) { ?><a href="<?php echo get_post_meta($post->ID,'ct_Website_text_550e',true) ?>" rel="bookmark" target="_blank"><?php echo the_post_thumbnail('thumbnail', array('class' => 'alignright'));; ?></a><?php } the_content( __( 'Continue Reading' , 'nuntius' ) . ' &raquo;'); ?>
    				<?php wp_link_pages( array( 'before' => '<p class="page-links">' . __( 'Pages:', 'nuntius' ), 'after' => '</p>' ) ); ?>
    			</div><!-- .entry-content -->
    		</div><!-- #post-## -->
    
    		<?php
    			// If comments are open or we have at least one comment, load up the comment template
    			if ( comments_open() || '0' != get_comments_number() )
    				comments_template();
    		?>
    
    		<?php endwhile; // end of the loop ?>

    Tim

    Ah, it doesn’t get the link ’cause you forget to say to php that $post is global.

    function site_link(){
        global $post;
        $href = get_post_meta($post->ID,'ct_Website_text_550e',true);
        return $href;
    }

    That said, you clearly have something, somewhere, which prints out the custom post in the way you see it.
    Here an example of the code above showing one of your custom posts with title, site link (ct_Website_text_550e), and thumbnail.

    1. Customizr-pro latest version is 1.0.6
    2. I’d bet you were using customizr 3.1.8. I’ve seen it before. You’re using a child theme of customizr, but you’re thinking you’re using a child-theme of customizr-pro?
    3. I see now, you upgraded to 3.1.9, the “_target” issue isn’t there anymore.

    Thread Starter Tim W

    (@twalker123)

    DOH! You’re right. I started with the free version and changed the path to style.css but forgot the template tag. AAARRRRGGG! I’m so sorry about that!

    I’m not sure what you mean by this…

    That said, you clearly have something, somewhere, which prints out the custom post in the way you see it.
    <img src=”https://i57.tinypic.com/a3nnlt.jpg&#8221; border=”0″ alt=”Image and video hosting by TinyPic”>”>Here an example of the code above showing one of your custom posts with title, site link (ct_Website_text_550e), and thumbnail.

    Thread Starter Tim W

    (@twalker123)

    I see what you mean – you’re right!

    I’m going to knock off for the night, but I’m going to have to figure that out. Thanks for all of your help, I really appreciate it!

    Tim

    I think that in your child-theme, functions.php you have some code which 1) makes tc-content span12 instead of span8, when there’s a thumbnail
    2) prints this stuff:

    <div id="post-1064" class="post-1064 site type-site status-publish format-standard has-post-thumbnail hentry category-food category-nuggetweb-designs">
                        <div class="entry-utility">
                                                                        </div><!-- .entry-utility -->
    
                        <a href="" rel="bookmark" target="_blank"></a><h2 class="entry-title"><a href="" rel="bookmark" target="_blank"></a><a href="" title="Go to Weber Meats Inc." target="_blank">Weber Meats Inc.</a></h2><!-- .entry-title -->
    
                        <div class="entry-content">
                            Categories: <a href="https://dev.nuggetweb.com/category/food/" rel="category tag">Food</a>, <a href="https://dev.nuggetweb.com/category/nuggetweb-designs/" rel="category tag">NuggetWeb Designs</a>                        <a href="" rel="bookmark" target="_blank"><img width="120" height="120" src="https://dev.nuggetweb.com/wp-content/uploads/2014/10/Weber-Meats-400x314-150x150.jpg" class="alignright wp-post-image" alt="Weber Meats Inc. - Carthage, Illinois"></a><p>Weber Meats Inc. of Carthage Illinois has been preparing top quality products at a fair price since 2002. Weber Meats specializes in providing top quality, locally raised meats without added preservatives with great customer service and a friendly smile.</p>
    <p>Visit Weber Meats today at <a href="https://www.webermeatsinc.com">https://www.webermeatsinc.com</a>!</p>
                                                </div><!-- .entry-content -->
                        </div>

    Can you post your child-theme functions.php in pastebin an put here the link?
    I’ll read it tomorrow morning, I’m sleepy.
    Goodnight

    Thread Starter Tim W

    (@twalker123)

    Got it! Thanks for all of your help d4z_c0nf, Giorgio and nikeo!

    https://dev.nuggetweb.com/clients/

    Here is the final page template code from d4z_c0nf (modified for my CPT) that I have working. I called it page-template-client.php:

    <?php
    /*
    Template Name: Clients
     */
    
    /* I use the action hook __after_loop
     * and partially replicate the customizr loop
     * to achieve:
     * 1) keep the page title
     * 2) show the page content if any, this can be used as a summary
     * before the list of posts
     */
    add_action('__after_loop', 'clients_list', 0);
    function clients_list(){
        add_filter('the_title', 'link_title_to_site', 0);
    
        global $wp_query;
        $wp_query = new WP_Query(array(
            /* change this with the query vars you need to display your cpts */
                'paged' => get_query_var('paged'),
                'post_type' => 'site'
            )
        );
        if ( have_posts() ) :
            while ( have_posts() ) :
                the_post();
                do_action ('__before_article');
                ?>
                    <article <?php tc__f('__article_selectors') ?>>
                        <?php do_action( '__loop' ); ?>
                    </article>
                <?php
                do_action ('__after_article'); 
    
            endwhile;
         endif; ##end if have posts
    }
    
    function site_link(){
        global $post;
        $href = get_post_meta($post->ID,'ct_Website_text_550e',true);
        return $href;
    }
    
    /* the title link */
    function link_title_to_site($title){
        /* your code to wrap the title into a link */
        return sprintf('<a href="%1$s" title="%2$s" target="_blank">%3$s</a>',
            site_link(),
            sprintf( __("Go to %s"), $title ),
            $title
        );
    }
    add_filter('tc_post_thumb_wrapper', 'change_thumbnails_link');
    function change_thumbnails_link($thumb){
        return preg_replace('/<a(.*?)href="(.*?)"(.*?)>/',
            '<a\1href="'.site_link().'"\3>',
            $thumb
        );
    }
    /*
     * in order to have the navigator we have to reset the custom query
     * with priority 30 (navigation has priority 20)
     */
    add_action('__after_loop', 'reset_query', 25);
    function reset_query(){
        global $wp_query, $wp_the_query;
        $wp_query = $wp_the_query;
    }
    
    /* let's remove customizr title filter on the title */
    remove_filter('the_title', array(TC_Headings::$instance, 'tc_content_heading_title'), 0);
    /* uncomment line below if you want to get rid of comments bubble */
    //remove_filter('the_title', array(TC_Headings::$instance, 'tc_add_comment_bubble_after_title'), 1);
    /* uncomment line below if you want to get rid of edit link */
    //remove_filter('the_title', array(TC_Headings::$instance, 'tc_add_edit_link_after_title'), 2);
    
    /*
     * let's include customizr index.php
     * this way we'll not lose eventual
     * changes on it
     */
    include_once( TC_BASE . 'index.php' );

    Tim

    Glad you solved.
    Could you mark this topic as resolved?

Viewing 15 replies - 16 through 30 (of 36 total)
  • The topic ‘Custom Post Type Archive Template’ is closed to new replies.