WordPress.org

Ready to get started?Download WordPress

Forums

Category shortcode question. (10 posts)

  1. jayce103
    Member
    Posted 3 years ago #

    If I wanted a certain category to be included on a page is there a short code that I code use???

    For example

    [category_posts id=123]

    Or even php in the header or html section?

    I know that the code above is not right and way off hence why I am asking.

    Jayce

  2. Dave Redfern
    Member
    Posted 3 years ago #

    If you wish to hard code it into the templates you can us WP_Query (http://codex.wordpress.org/Function_Reference/WP_Query) inside any php file in your themes folder. Here is an example of it in practice:

    <?php
    	$args=array(
    		'cat' => 1
    	);
    	$the_query = new WP_Query($args);
    	while ($the_query->have_posts()) : $the_query->the_post();
    ?>
        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php
    	endwhile;
    ?>

    For a full list of options you can put inside the array i recommend checking out query_posts (http://codex.wordpress.org/Function_Reference/query_posts).

    You can use this code inside the admin but it requires a plugin to enable you to process php inside a post called exec-php (http://wordpress.org/extend/plugins/exec-php/).

    Thanks

    Dave.

  3. jayce103
    Member
    Posted 3 years ago #

    Hi Dave,

    Thanks for that! I have got the plugin installed and the code on the page, and although it works it creates a list of posts in a category rather than show the posts in full.

    Is this due to the php code above of the css styling of a theme?

    Thanks again for your help.

  4. Dave Redfern
    Member
    Posted 3 years ago #

    Hello again,

    In this instance you can modify anything inside the while() to suit what you wish to display. You can using any tags you would see in the template files such as the_permalink, the_title, the_excerpt, the_content, the_date, the_time. In your Instance I think you're looking for something similar to the below.

    <?php
    	$args=array(
    		'cat' => 1
    	);
    	$the_query = new WP_Query($args);
    	while ($the_query->have_posts()) : $the_query->the_post();
    ?>
    	<div class="post">
    		<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    		<?php the_content(); ?>
    	</div>
    <?php
    	endwhile;
    ?>

    You can checkout the following links for more information about templating.

    http://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/Stepping_Into_Templates
    http://codex.wordpress.org/Template_Tags

    Thanks

    Dave.

  5. jayce103
    Member
    Posted 3 years ago #

    Thanks for the quick reply Dave. I will give it ago over the next day or too.

    Cheers buddy,

    Jayce

  6. jayce103
    Member
    Posted 3 years ago #

    Just one little quicky!!!!

    Using the code above works, but it puts the full posts in the section. Is it possible to selct category instead so it shows the overview (don't really know what to call it).

    IE

    If I click on a category in the widget it takes me to a category overview with the posts in a short format below that. I can then click on a post to view the full version.

    Hope this makes sense.

    Jayce

  7. jayce103
    Member
    Posted 3 years ago #

    *UPDATE*

    It does show all posts in a certain category but it shows the entire post instead of the normal way..

    http://phototraining(.)tv/news

    This is the normal styling.

    http://phototraining(.)tv/testpage

    This is using the code Dave supplied

  8. chinmoy
    Member
    Posted 3 years ago #

    use <?php the_excerpt() ?> instead of <?php the_content() ?>

  9. jayce103
    Member
    Posted 3 years ago #

    @DAVE

    Thanks for this buddy, it works a treat!!!

    @Chinmoy

    Thank you for the addition, that also worked.

    Jayce

  10. orgspirit
    Member
    Posted 3 years ago #

    chinmoy29

    Hi!Sorry that I'm writing not related text,but I really need your help!)I didn't know how to contact with you..Can you please reply to my question on my topic at
    http://wordpress.org/support/topic/make-div-on-static-page-for-latest-post?replies=7
    Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic