• Resolved Yeshan Perera

    (@yeshansachithak)


    I’m a self learner. So, I have few questions. I create my own custom template and it’s working now. Now I want to add short code to page. and then it will show the post by the id. How I do it?

Viewing 10 replies - 1 through 10 (of 10 total)
  • You can dreate a statit page in your site and set up it as “front page” in reading settings (you’ll need create anothet static page to regular post loop, call it blog or something)

    Then, create a custom query in your template and asign this page_template to your static homepage

    here you can read some info about…

    http://codex.wordpress.org/Page_Templates

    http://codex.wordpress.org/Class_Reference/WP_Query

    http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    http://digwp.com/2011/05/loops/

    Take a look on it… it can quite complicated in the first try… But it’s only fiddle with it

    Thread Starter Yeshan Perera

    (@yeshansachithak)

    I read all those before I developed my template. I do the same here. So, Now I want to display cat post on page. As Example, If I change in my Home page [shortcode cat=2] for [shortcode cat=5] then displayed post must be change after refreshing. That’s I want to do.

    Thread Starter Yeshan Perera

    (@yeshansachithak)

    I have five categories in common. So, I want to create short-code for make it easy. I used pages as my Navigation. I also create 5 pages.

    • 1. Put short-code in pages
    • 2. Posting under separate categories.
    • 3. call the_content(); in custom template
    • 4. when it’s view->
    • 5. Display my posts
    • 6. That’s all I want to do here

    Ok, ok… I got it now! I missunderstood

    ¿ Have you tried to create the custom loops in your functions file and define there as shortcodes ? I’m not sure if it’s going to work…

    Maybe using “recent posts“… I tried to do something similar a time ago, but where with custom types and a lot of custom fields… A really big messy

    Thread Starter Yeshan Perera

    (@yeshansachithak)

    <?php
    /*
     *	Template name: HomePage Template
     *	Description: Homepage Template use to create your home page as a default view.
     *
     *
    */
    ?>
    <?php get_header(); ?>
    <!-- ABOUT  -->
    <article class="column about">
    <div class="desc">
    	<div class="content">
    		<h1 class="page-title"><?php the_title(); ?></h1>
    		<div class="grids">
    			<div class="grid-12">
    				<?php//get page content and display ?>
    				<?php if (have_posts()) : ?>
    				<?php while (have_posts()) : the_post(); ?>
    					<?//this will echo the page content ?>
    					<?php the_content(''); ?>
    				<?php endwhile; ?>
    				<?php endif; ?>
    			</div>
    		</div>
    	</div>
    </div>
    </article>
    
    <?//php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    This is my custom template code. So Now I want to get the content using short-code. It’s mean, I’m going to put category short-code in page content area. So, If we create a right short-code for it. Then it will display in page.

    ok… I guess it will work for you… I tried in my “test site” and it works

    First i created this function in my functions.php file

    function loop_category( $atts ) {
    
    	//extract cat passed in shortcode
    
    	extract( shortcode_atts( array(
    		'id_cat' => '',
    	), $atts ) );
    
    	 //use category as value to query posts array or arguments
    
    	$args = array( 'category' => $id_cat ); 
    
    	$recent_posts = wp_get_recent_posts($args);
    
    	foreach( $recent_posts as $recent ) {
    
    	// Dsiplay title 
    
    	echo '<h2>';
    	echo $recent["post_title"];
    	echo '</h2>';	
    
    	//Display excerpt (you can use content if you want)
    
    	echo '<p>';
    	echo $recent["post_excerpt"];
    	echo '</p>';	
    
    	echo '<a href="';
    	echo get_permalink($recent["ID"]);
    	echo '" title="';
    	echo $recent["post_title"];
    	echo '" > read more </a>';
    
    	}

    I only passed cat in $args array, but you can especufy whatever you want for the get_post() function.

    Also you’ll see the markup is extremly poor! It’s only basic function, you can base on it to markup yours

    Then i created the shortcode caller (in functions.php file too)

    add_shortcode( 'view_category', 'loop_category' );

    And… it’s done! Just call the shortcode in your content

    [view_category id_cat="9"]

    Passing the category id… And it should work! Hope it be usefull for you

    Thread Starter Yeshan Perera

    (@yeshansachithak)

    Thanks! I’ll try this. Anyway! Do you know how to use ‘shortcode Exec PHP plugin’? I think it will easy to use. We don’t need to write function so far. Try to use it. And give it to me.

    This shortcode runs PHP code, if you copy / paste the code in your functions.php file and can use it

    Later you can improve the markup as you wish (use in unordered list or whatever) just by editing it

    But right now, this code is fully functional

    You can use as shown up, like a header title, post excerpt and a “read more link

    function loop_category( $atts ) {
    
    	//extract cat passed in shortcode
    
    	extract( shortcode_atts( array(
    		'id_cat' => '',
    	), $atts ) );
    
    	 //use category as value to query posts array or arguments
    
    	$args = array( 'category' => $id_cat ); 
    
    	$recent_posts = wp_get_recent_posts($args);
    
    	foreach( $recent_posts as $recent ) {
    
    	// Dsiplay title 
    
    	echo '<h2>';
    	echo $recent["post_title"];
    	echo '</h2>';	
    
    	//Display excerpt (you can use content if you want)
    
    	echo '<p>';
    	echo $recent["post_excerpt"];
    	echo '</p>';	
    
    	echo '<a href="';
    	echo get_permalink($recent["ID"]);
    	echo '" title="';
    	echo $recent["post_title"];
    	echo '" > read more </a>';
    
    	}
    
    }
    
    add_shortcode( 'view_category', 'loop_category' );

    You can use as an unordered list of post titles linking to single posts (for example)

    function loop_category( $atts ) {
    
    	//extract cat passed in shortcode
    
    	extract( shortcode_atts( array(
    		'id_cat' => '',
    	), $atts ) );
    
    	 //use category as value to query posts array or arguments
    
    	$args = array( 'category' => $id_cat ); 
    
    	$recent_posts = wp_get_recent_posts($args);
    
            echo '<ul>'
    
    	foreach( $recent_posts as $recent ) {
    
    	echo '<li>';
    	echo '<a href="';
    	echo get_permalink($recent["ID"]);
    	echo '" title="';
    	echo $recent["post_title"];
    	echo '" >';
    	echo $recent["post_title"];
    	echo '</a></li>';
    	}
        }
    
    add_shortcode( 'view_category', 'loop_category' );

    If you copy and paste it in functions.php file, should work, just use the shortcode

    [view_category id_cat=”XX”]

    Where “XX” is the identifier of the category you want to show… Ypu don’t need any plugin for it.

    About the plugin… No idea… I dont usually work with plugins…

    i’ve notices there is a mistake in first code, a final } is missing… But i added in the new version

    Thread Starter Yeshan Perera

    (@yeshansachithak)

    Hey! Bro!
    I’m just late to reply! You did ur job great. After all, I’ll hope this will helpful for every finders. We do it good.

    extract(shortcode_atts(array('cat_id' => 'default','display_format' => 'left'), $atts));
    $args = array( 'numberposts' => 100, 'offset'=> 0, 'category' => $atts['cat_id'] );
    $myposts = get_posts( $args );
    $html = '';
    //print count($myposts);
    if(count($myposts)>0){
    	foreach( $myposts as $p ){
    		$feat_image = wp_get_attachment_url(get_post_thumbnail_id($p->ID));
    		$html.= '<div class="blog_set">';
    			$html.= '<div class="description"><img src="'.$feat_image.'" width="150" height="100" align="'.$atts['display_format'].'"/><div class="title">'.$p->post_title.'</div>'.$p->post_content.'</div>';
    		$html.= '</div>';
    	}
    	print $html;
    }

    this my code finally that I develop for my site. If use that plugin, No need to customize function.php. In fact those code for that plugin, Then we can call our shortcode any page or post.
    In my coding we just need to put
    [myblog cat_id="2" display_format="left"]
    OR
    [myblog cat_id="2" display_format="right"]
    we can simply change cat_id, and display_format left or right. It calls for the class align in my code.

    Thanks for help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Can Display specific cat in page content?’ is closed to new replies.