• Resolved pg73

    (@pg73)


    Hi, I hope someone will be kind enough to help with this, I don’t have a good enough understanding of PHP just yet to figure this out.

    I have this piece of code (see below) that is used to show all my post titles, grouped by categories. The page will be quite long so what I want to do is have a list in the sidebar of all the categories and when one is clicked the page jumps to the start of that category of posts. So basically I need some code added to the last few lines to create anchor points, which will be unique to each category title on the page, such as:

    <a name=category_name>

    so that I can then target those in the side bar list with:

    <a href='#category_name'>

    <?php
    	$cat_args=array(
    		'orderby' => 'name',
    		'order' => 'ASC'
    		);
    	$categories=get_categories($cat_args);
    	foreach($categories as $category) {
    	$args=array(
    		'showposts' => -1,
    		'category__in' => array($category->term_id),
    		'caller_get_posts'=>1
    		);
    	$posts=get_posts($args);
    	if ($posts) {
    	echo '<p class="topic-titles">'. $category->name.'</p> ';
    	foreach($posts as $post) {
    	setup_postdata($post);
    ?>

    Would really appreciate some help with this, thank you.

Viewing 10 replies - 16 through 25 (of 25 total)
  • Thread Starter pg73

    (@pg73)

    If I target what you suggested it would add space between ALL the posts, I just need a space after the LAST post of each category.

    Here is the html of a category title and the post, is it possible to target the a name
    <a name="Photography">
    of each category, except for the first category?

    <a name="Photography"></a><p class="topic-titles">Photography</p><div class="title-spacer"></div>
    	<!-- ARTICLE TEXT -->
    	<div class="articles-article-text">
    		<h2<a href="https://domain.com/another-test-title" rel="bookmark" title="Read the full article: Another Test Title">Another Test Title</a></h2>
    	</div>
    <!-- ARTICLE META DATA -->
    	<div class="articles-articlemetadata">

    Ok i see what you mean, gimme a few mins and i’ll post a pastebin link for a modified version of the code you posted.

    https://wordpress.pastebin.com/DpBmzHr4

    Echoes the spacer if it’s anything but the first listed category…. ??

    Thread Starter pg73

    (@pg73)

    Brilliant, it’s all working as it should now, thank you for all your help – you’ve saved me so much time. I’ll leave you in peace for a while now ??

    Thread Starter pg73

    (@pg73)

    Sorry, one last thing… is it possible to add some code to create hover text for the list, that would say ‘Jump to “Category name”‘ – thank you.

    <?php
    			$get_cats = get_terms( 'category' , array(
    				'exclude' => '4,7' ,
    				'orderby' => 'name' ,
    				'fields' => 'names'
    			) );
    			if( !empty( $get_cats ) ) {
    				$anchors = array();
    				foreach( $get_cats as $key => $data )
    					$anchors[] = '<a href="#' . $data . '">' . $data . '</a>';
    				echo '<li>' . implode( ' </li><li>' , $anchors ) . '</li>';
    			}
    			unset( $get_cats , $key , $data , $anchors ); // Unsets data no longer needed
    		?>

    Not sure what you mean, can you elaborate further.

    Thread Starter pg73

    (@pg73)

    I just need code adding to the sidebar list (see code above) to create the text that appears in the yellow box when you hover over a link, it’s usually this in the HTML:

    title="text-goes-here"

    Is it possible to slip that into the code above so that it says: “Jump to ‘category name automatically added here’ “.

    Thank you.

    Oh i see, yeah sure.. ??

    Change…

    $anchors[] = '<a href="#' . $data . '">' . $data . '</a>';

    For…

    $anchors[] = '<a href="#' . $data . '" title="Jump to ' . $data . '">' . $data . '</a>';

    Thread Starter pg73

    (@pg73)

    Excellent! – works great, thank you ??

    You’re welcome.. ??

Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Create Anchor Points in Category Titles of Loop’ is closed to new replies.