WordPress.org

Support

Support » How-To and Troubleshooting » Titles list of a category in a page

Titles list of a category in a page

  • Hi all, I searched on the forum and unfortunately haven’t found what I’m exactly looking for.

    I would like to modify the single.php file to have a list of only the titles of a specific category (ID = 10) in a single wordpress page.
    I’d like to create something like this:

    • title 1
    • title 2
    • title 3
Viewing 9 replies - 1 through 9 (of 9 total)
  • Michael
    Moderator

    @alchymyth

    consider creating a page template:

    http://codex.wordpress.org/Pages#Page_Templates

    I tried something like this, it prints the right category but 2 times each title:

    <?php
       foreach((get_the_category()) as $category)
        {
    	$postcat= $category->cat_ID;
    	$catname =$category->cat_name;
        }
    ?>
    
    <?php $categories = get_categories("child_of=$postcat");
      foreach ($categories as $cat)
        { ?>
    <?php query_posts("cat=5&posts_per_page=-1"); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    	<ul>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    	<?php the_title(); ?></a>
    	</li>
            </ul>
    <?php endwhile; ?>
    <?php } ?>
    Michael
    Moderator

    @alchymyth

    instead of the fixed category id 5 in here:

    <?php query_posts("cat=5&posts_per_page=-1"); ?>

    possibly try:

    <?php query_posts("cat=".$cat->term_id."&posts_per_page=-1"); ?>

    Ok, but in this way it prints the titles of all the categories, instead I’m trying to have only the title of the category 5.

    Michael
    Moderator

    @alchymyth

    a list of only the titles of a specific category (ID = 10)

    your code does not fit this description;

    what exactly do you want to show?

    I want to print on a page only and all the titles (of the posts) of the category 5.

    Any hint? sorry for the bump…

    Michael
    Moderator

    @alchymyth

    do you have the page template part in place?

    it might help if you could post a link to this page to illustrate what you have so far.

    if you have problems there, please paste the full code of your page template into a http://pastebin.com/ and post the link to it here.

    as the main code for the loop, this is all you should need:

    <?php query_posts("cat=5&posts_per_page=-1"); ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    	<?php the_title(); ?></a>
    	</li>
            <?php endwhile; wp_reset_query(); ?>
    </ul>

    Hey, thank you.
    I copied and pasted your code and……it works like a charm. Thank you very much

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Titles list of a category in a page’ is closed to new replies.