Home Forums Xi Theme Discussion & Support Template with Page and Post Category

This topic is: resolved

This topic contains 11 replies, has 2 voices, and was last updated by  Robert DeGray 4 years, 6 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #3160

    Robert DeGray
    Participant
    Post count: 14

    I’m trying to create a template which both contains a page content and displays recent posts from the Category whose name (slug) is the same as the Page’s slug.

    I’ve seen examples of doing this with custom loops, but I was wanting, instead, to reuse the capability in the los_showPosts widget. So I actually built my template (in a child theme) to customize the page for a few other things, then to try to call los_showPosts with a cat_name field, then I added support into show-widgets.php:los_showWidgets which I thought would handle that cat_name but didn’t:

    	// Set the arguments of the get_posts function 
    	if ( $args['post_type'] == "pages" ) {
    		// Set the arguments of the get_posts function 
    		$get_posts_args = array(
    			'post_parent' => $args['parent_id'],
    			'numberposts' => $args['num_posts'],
    			'post_type' => 'page',
    			'orderby' => 'menu_order',
    			'order' => 'ASC'
    		);
    	} elseif ( isset($args['cat_name']) ) {
    	    $get_posts_args = array(
    	        'category_name' => $args['cat_name'],
    			'numberposts' => $args['num_posts']
            );
    	} else {
    		$get_posts_args = array(
    			'cat' => $args['cat_id'],
    			'numberposts' => $args['num_posts']
    		);
    	}

    I also tested with an actual shortcode in another page, and neither worked. Both showed posts from all categories.

    Also, I realize this isn’t the most portable approach, but I wanted to find somethign that worked for now.

    So I’d appreciate any help on why this isn’t working, any suggestion on how to do the same thing a better way, or a new page template provided that does this for me 🙂

    #3165

    themeweaver
    Keymaster
    Post count: 730

    Actually, won’t the existing blog template do this for you?

    1. Create a page and use the blog template that comes with the theme.
    2. You can add content to the page which will appear above the posts.
    3. Posts will be displayed in blog-like fashion and can be controlled with custom fields to set category etc (see http://themeloom.com/support/framework-codex/controlling-features-with-custom-fields/)

    To change the layout you could create another template based on the blog template if needed, but the basic requirement of page content plus posts from a category is covered by this template.

    #3169

    Robert DeGray
    Participant
    Post count: 14

    Thanks, that looks like it will do the trick. I’ll mark this as resolved if I’m able to get my custom template working too.

    #3170

    Robert DeGray
    Participant
    Post count: 14

    Ok, that does basicall works. Although it doesn’t have the look of the Show Posts widget, which is what I really want. Any way to get that embedded into the template?

    What I’m really trying to do is get a template which automatically adds the Show Posts behavior rather than having to manually add the Show Posts shortcode to multiple pages.

    #3171

    themeweaver
    Keymaster
    Post count: 730

    Well that’s what the blog template does. The presentation in columns etc is mainly css styles. You would need to create some extra css if you want to change the layout. You might need the odd tweak to the template (or a copy of the template) to get easier markup.

    We do offer customisation services if you wanted us to do it for you. See http://themeloom.com/custom-wordpress-themes/

    #3178

    Robert DeGray
    Participant
    Post count: 14

    The widget/shortcode and blog template’s “loop” don’t show the same output. For instance, the blog template uses for the title, but the widget uses . But there are many other display differences which are not just CSS.

    I could copy the code from the widget into the template, but that seems like worst-practice 🙂

    #3179

    Robert DeGray
    Participant
    Post count: 14

    Sorry, previous post lost the formatting:

    For instance, the blog template uses h1 for the title, but the widget uses h3

    #3184

    themeweaver
    Keymaster
    Post count: 730

    Yes, but you could add some css to re-style some of these. There will be some customisation to do whichever route you take with this either in css or in php.

    #3186

    Robert DeGray
    Participant
    Post count: 14

    Right, I’m not surprised customization is required. I just don’t know why the approach I tried didn’t work. That is, why giving the widget the ability to query by category_name didn’t seem to return anything. (Or, rather, seemed to return everything.)

    And if I wanted to reuse what the widget does, is the best/only way to call its method directly from my PHP?

    #3187

    themeweaver
    Keymaster
    Post count: 730

    Yes , the function you need to call is los_showPosts();

    The shortcode calls this function but you need to call it directly with an array of args.

    but ‘cat’ is the category id not name. there is a wp function I think that will turn a name into an id though.

    #3188

    themeweaver
    Keymaster
    Post count: 730

    so..

    $args = array( 
    		'title' => '',
    		'before_widget' => '',
    		'after_widget' => '',
    		'cat_id' => 0,
    		'post_type' => '',
    		'parent_id' => 0,
    		'num_posts' => 5,
    		'columns' => 2,
    		'thumbs' => true,
    		'thumbsize' => 'widget',
    		'entrytag' => 'h3',
    		'author' => false,
    		'categories' => false,
    		'tags' => false,
    		'heading' => true,
    		'dates' => false,
    		'content' => true,
    		'post_formats' => false,
    		'more_link' => '',
    		'post_class'=> 'los-custom-post' 
    	);
    
    los_showPosts( $args );

    You can also use get_cat_ID( $cat_name ) to get the cat id.

    #3194

    Robert DeGray
    Participant
    Post count: 14

    Ok, that approach worked. A few minor additions were required. Here’s the final code:

    				<?php
    				//get custom field data
    				$blog_cat = get_post_meta($post->ID, 'category_name', true);
    				$cat_id = get_category_by_slug($blog_cat)->term_id;
    				
    				$posts_per_page = get_post_meta($post->ID, 'posts_per_page', true);
                    
                    $args = array(
                            // 'title' => 'Related Posts',
                            // 'before_title' => '<h3>',
                            // 'after_title' => '</h3>',
                            'cat_id' => $cat_id,
                            'post_type' => 'post',
                            'parent_id' => 0,
                            'num_posts' => $posts_per_page,
                            'columns' => 1,
                            'thumbs' => true,
                            'thumbsize' => 'widget',
                            'entrytag' => 'h3',
                            'author' => false,
                            'categories' => false,
                            'tags' => false,
                            'heading' => true,
                            'dates' => false,
                            'content' => true,
                            'post_formats' => false,
                            'more_link' => 'More',
                            'more_url' => '/category/'.$blog_cat,
                            'post_class'=> 'los-custom-post'
                        );
                     
                    echo los_showPosts( $args );
    				?>
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.