Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom page template for tag list, please advise
Tagged: custom page templates, php, tags, taxonomies
- This topic has 14 replies, 5 voices, and was last updated 10 years, 6 months ago by
Genesis Developer.
-
AuthorPosts
-
August 13, 2014 at 9:21 pm #118694
cookieandkate
MemberHello!
I'm trying to create a custom page template that creates a list of my tags, ordered alphabetically, with labels for each letter. I based my code on this template. It's almost where I want it to be, but not quite. I'm sure that what I want to accomplish is possible, but my PHP knowledge is lacking. I'd really appreciate your help!
Here's how the page looks now: http://i.imgur.com/cG7b9gQ.png
Here's what I want to change:
I want the page content to display before inserting the tags list.
I want to get rid of the random word, "Array," that displays on the page right before the letter, "A".
Unnecessary, but I'd like to automatically capitalize the first letter of the tags, if possible. (Just the first letter, not both.)Here's my code:
<?php /* Template Name: Tag Index */ //* Remove standard post content output remove_action( 'genesis_post_content', 'genesis_do_post_content' ); remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); add_action( 'genesis_entry_content', 'sk_page_archive_content' ); add_action( 'genesis_post_content', 'sk_page_archive_content' ); /** * This function outputs posts grouped by year and then by months in descending order. * */ function sk_page_archive_content() { global $post; echo // Make an array from A to Z. $characters = range('A','Z'); // Retrieve all tags $getTags = get_tags( array( 'order' => 'ASC') ); // Retrieve first letter from tag name $isFirstCharLetter = ctype_alpha(substr($getTags[0]->name, 0, 1)); // Special Character and Number Loop // Run a check to see if the first tag starts with a letter // If it does not, run this if ( $isFirstCharLetter == false ){ // Print a number container $html .= "<div class='tag-group'>"; $html .= "<h3 class='tag-title'>#</h3>"; $html .= "<ul class='tag-list'>"; // Special Character/Number Loop while( $isFirstCharLetter == false ){ // Get the current tag $tag = array_shift($getTags); // Get the current tag link $tag_link = get_tag_link($tag->term_id); // Print List Item $html .= "<li class='tag-item'>"; // Check to see how many tags exist for the current letter then print appropriate code if ( $tag->count > 1 ) { $html .= "<p><a href='{$tag_link}' title='View all {$tag->count} articles with the tag of {$tag->name}' class='{$tag->slug}'>"; } else { $html .= "<p><a href='{$tag_link}' title='View the article tagged {$tag->name}' class='{$tag->slug}'>"; } // Print tag name and count then close the list item $html .= "<span class='tag-name'>{$tag->name}</span></a><span class='tag-count'>({$tag->count})</span></p>"; $html .= "</li>"; // Retrieve first letter from tag name // Need to redefine the global variable since we are shifting the array $isFirstCharLetter = ctype_alpha(substr($getTags[0]->name, 0, 1)); } // Close the containers $html .= "</ul>"; $html .= "</div>"; } // Letter Loop do { // Get the right letter $currentLetter = array_shift($characters); // Print stuff $html .= "<div class='tag-group'>"; $html .= "<div class='tag-title' id='{$currentLetter}'>{$currentLetter}</div>"; $html .= "<ul class='tag-list'>"; // While we have tags, run this loop while($getTags){ // Retrieve first letter from tag name $firstChar = substr($getTags[0]->name, 0, 1); // Does the first letter match the current letter? // Check both upper and lowercase characters for true if ( strcasecmp($currentLetter, $firstChar) == 0 ){ // Get the current tag $tag = array_shift($getTags); // Get the current tag link $tag_link = get_tag_link($tag->term_id); // Print stuff $html .= "<li class='tag-item'>"; // Check to see how many tags exist for the current letter then print appropriate code if ( $tag->count > 1 ) { $html .= "<p><a href='{$tag_link}' title='View all {$tag->count} articles with the tag of {$tag->name}' class='{$tag->slug}'>"; } else { $html .= "<p><a href='{$tag_link}' title='View the article tagged {$tag->name}' class='{$tag->slug}'>"; } // Print more stuff $html .= "<span class='tag-name'>{$tag->name}</span></a><span class='tag-count'>({$tag->count})</span></p>"; $html .= "</li>"; } else { break 1; } } $html .= "</ul>"; $html .= "</div>"; } while ( $characters ); // Will loop over each character in the array // Let's see what we got: echo($html); ?> <?php } remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 ); remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 ); genesis();
August 13, 2014 at 9:30 pm #118696AnitaC
KeymasterWhat exactly do you mean by:
I want the page content to display before inserting the tags list.
Are you trying to accomplish something like this - http://www.web-savvy-marketing.com/live/?theme=Sarah%20Ellen or this - http://www.web-savvy-marketing.com/live/?theme=Carla%20Anna?
Need help with customization or troubleshooting? Reach out to me.
August 13, 2014 at 9:34 pm #118697Brad Dalton
ParticipantThis answers one question http://wpsites.net/web-design/using-php-code-to-change-words-letters-to-uppercase-lowercase/
Rather than hard code it into a tag.php template file you could use get_template_part
August 13, 2014 at 9:41 pm #118698Brad Dalton
ParticipantThis is what i would use and change the code to displays tags http://wpsites.net/web-design/custom-genesis-archives-template-show-all-posts-in-all-categories/
August 13, 2014 at 9:54 pm #118699cookieandkate
MemberThanks, you two.
Anita: No, I don't see anything on those themes like what I want.
I understand that the code I provided strips the page content from the code. I don't want that to happen. I want to be able to add page content like I would for any page, THEN I want the tag list that I've designed to display underneath that content.
Brad: Thank you for sharing the capitalization trick. I don't know where to insert that code into my PHP template, though.
Your category solution is cool, but I have a couple hundred tags that I'm trying to list. I just want the tag names to appear in an alphabetical list, like I've already designed, but I want the page content to appear above it. Do you know how to make that happen?
I'd really love your help here! Thank you,
KateAugust 14, 2014 at 6:26 am #118758cookieandkate
MemberIf it's too complicated to insert the page text before the list, I'd like to be able to insert HTML code to display above the list in my PHP code. I can't figure out how to do that. I still can't figure out how to remove the word "Array" that shows up before my list. (Again, here's my screenshot: http://i.imgur.com/cG7b9gQ.png)
August 14, 2014 at 11:58 am #118831Lauren @ OnceCoupled
MemberTo add the content of the page, just call it at the beginning of your function:
the_content();
I'm not sure where that "Array" is coming from. When you view the page source, what containers is it in? And it's only printed once at the very beginning of all the letters, yes?
We create mobile-first, PageSpeed-optimized, pixel-perfect custom themes! https://www.oncecoupled.com
August 14, 2014 at 12:33 pm #118836Genesis Developer
MemberI want the page content to display before inserting the tags list.
Comment out this code like
//* Remove standard post content output /*remove_action( 'genesis_post_content', 'genesis_do_post_content' ); remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); add_action( 'genesis_entry_content', 'sk_page_archive_content' ); add_action( 'genesis_post_content', 'sk_page_archive_content' );*/
Now filter the
the_content()
function. Then you will get your page content at top.add_filter( 'the_content', 'sk_page_archive_content', ); function sk_page_archive_content($content) { // WRITE YOUR EXISTING CODE HERE // Now remove this line echo($html);and write the following code $content .= $html; return $content; }
August 14, 2014 at 11:15 pm #118902cookieandkate
MemberThank you all so much for your help! Lauren, yes, "Array" is only printed once at the beginning of the letters.
<div class="entry-content" itemprop="text">Array<div class='tag-group'><div class='tag-title' id='A'>A</div>
Genrock, I really appreciate your detailed instructions. I believe I'm implemented them as instructed, but I'm getting a blank page. Do you see what's wrong here? I'm stuck and would so appreciate your help.
<?php /* Template Name: Tag Index */ //* Remove standard post content output /*remove_action( 'genesis_post_content', 'genesis_do_post_content' ); remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); add_action( 'genesis_entry_content', 'sk_page_archive_content' ); add_action( 'genesis_post_content', 'sk_page_archive_content' );*/ add_filter( 'the_content', 'sk_page_archive_content', ); function sk_page_archive_content($content) { /** * This function outputs posts grouped by year and then by months in descending order. * */ function sk_page_archive_content() { global $post; echo // Make an array from A to Z. $characters = range('A','Z'); // Retrieve all tags $getTags = get_tags( array( 'order' => 'ASC') ); // Retrieve first letter from tag name $isFirstCharLetter = ctype_alpha(substr($getTags[0]->name, 0, 1)); // Special Character and Number Loop // Run a check to see if the first tag starts with a letter // If it does not, run this if ( $isFirstCharLetter == false ){ // Print a number container $html .= "<div class='tag-group'>"; $html .= "<h3 class='tag-title'>#</h3>"; $html .= "<ul class='tag-list'>"; // Special Character/Number Loop while( $isFirstCharLetter == false ){ // Get the current tag $tag = array_shift($getTags); // Get the current tag link $tag_link = get_tag_link($tag->term_id); // Print List Item $html .= "<li class='tag-item'>"; // Check to see how many tags exist for the current letter then print appropriate code if ( $tag->count > 1 ) { $html .= "<p><a href='{$tag_link}' title='View all {$tag->count} articles with the tag of {$tag->name}' class='{$tag->slug}'>"; } else { $html .= "<p><a href='{$tag_link}' title='View the article tagged {$tag->name}' class='{$tag->slug}'>"; } // Print tag name and count then close the list item $html .= "<span class='tag-name'>{$tag->name}</span></a><span class='tag-count'>({$tag->count})</span></p>"; $html .= "</li>"; // Retrieve first letter from tag name // Need to redefine the global variable since we are shifting the array $isFirstCharLetter = ctype_alpha(substr($getTags[0]->name, 0, 1)); } // Close the containers $html .= "</ul>"; $html .= "</div>"; } // Letter Loop do { // Get the right letter $currentLetter = array_shift($characters); // Print stuff $html .= "<div class='tag-group'>"; $html .= "<div class='tag-title' id='{$currentLetter}'>{$currentLetter}</div>"; $html .= "<ul class='tag-list'>"; // While we have tags, run this loop while($getTags){ // Retrieve first letter from tag name $firstChar = substr($getTags[0]->name, 0, 1); // Does the first letter match the current letter? // Check both upper and lowercase characters for true if ( strcasecmp($currentLetter, $firstChar) == 0 ){ // Get the current tag $tag = array_shift($getTags); // Get the current tag link $tag_link = get_tag_link($tag->term_id); // Print stuff $html .= "<li class='tag-item'>"; // Check to see how many tags exist for the current letter then print appropriate code if ( $tag->count > 1 ) { $html .= "<p><a href='{$tag_link}' title='View all {$tag->count} articles with the tag of {$tag->name}' class='{$tag->slug}'>"; } else { $html .= "<p><a href='{$tag_link}' title='View the article tagged {$tag->name}' class='{$tag->slug}'>"; } // Print more stuff $html .= "<span class='tag-name'>{$tag->name}</span></a><span class='tag-count'>({$tag->count})</span></p>"; $html .= "</li>"; } else { break 1; } } $html .= "</ul>"; $html .= "</div>"; } while ( $characters ); // Will loop over each character in the array // Let's see what we got: $content .= $html; return $content; } ?> <?php } remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 ); remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 ); genesis();
August 14, 2014 at 11:17 pm #118903cookieandkate
MemberOk, I realize I have repeats of sk_page_archive_content... but removing one or the other doesn't solve the problem.
August 14, 2014 at 11:38 pm #118908Genesis Developer
MemberPlease change this line
add_filter( 'the_content', 'sk_page_archive_content', );
with
add_filter( 'the_content', 'sk_page_archive_content' );
I made mistake
August 14, 2014 at 11:41 pm #118909Genesis Developer
MemberAugust 14, 2014 at 11:49 pm #118911Genesis Developer
MemberCode is working for me. I tested it on my site. You need to add the CSS for design and it'll look very nice.
Replace this line
echo // Make an array from A to Z.
by// Make an array from A to Z.
and Array text will remove from page.
August 15, 2014 at 12:20 am #118916cookieandkate
MemberGenwrock, that code works marvelously! Thank you so very much!!!
August 15, 2014 at 12:30 am #118918Genesis Developer
MemberYou're welcome. Glad to help you.
Please mark the thread as RESOLVED
-
AuthorPosts
- The topic ‘Custom page template for tag list, please advise’ is closed to new replies.