Support

Account

Forum Replies Created

  • Hi, with some help from WP.org I managed to sort this out yesterday. For the record, the final code that we ended up using is the following:

    
    text/x-generic page-10787.php 
    PHP script text
    <?php get_header(); ?>
    
    <section class="content">
    
    	<?php get_template_part('inc/page-title'); ?>
    
           <div class="pad group">
    
    <div class="entry">
    						<div class="entry-inner">
    							<?php the_content(); ?>
    							<?php wp_link_pages(array('before'=>'<div class="post-pages">'.__('Pages:','hueman'),'after'=>'</div>')); ?>
    						</div>
    						<div class="clear"></div>
    					</div><!--/.entry-->
    
    <?php
    
    $posts = get_posts( array(
        'numberposts' => -1,
        'meta_key' => 'elokuvat_l',
        'order'=> 'ASC'
    ) );
    
    if( $posts ) {
    	echo '<ul>';
    		$temporary_list = array();
    		foreach( $posts as $post ) {
    			$film_names = get_field( 'elokuvat_l', $post->ID );
    			$film_names_array = preg_split( '/\r\n|\r|\n/', $film_names );
    			foreach( $film_names_array as $film_name ) {
    				$temporary_list[] = array( 'post_id' => $post->ID, 'film_name' => $film_name );
    			}
    		}
    		// custom sort function
    		usort( $temporary_list, function ( $a, $b ) {
    			return strcasecmp($a['film_name'], $b['film_name']); // compare two strings ignoring case
    		});
    		// loop and display
    		foreach( $temporary_list as $film_name ){
    			echo '<li><a href="' . get_permalink($film_name['post_id']) . '">' . $film_name['film_name'] . '</a></li>';
    		}
    	echo '</ul>';
    }
    
    ?>
    
    	</div><!--/.pad-->
    
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
  • Thank you for the assistance. It totally now splits the entries into two separate ones, however it still doesn’t alphabetically sort them the proper way. I’ll try figuring this out on my own until our annual funding kicks in and then I’ll just get the repeater -add-on since that will certainly help with this matter. Thanks for all the help!

  • Thanks for your hard work!

    Does all this code go into the page template? I attempted to get it to work by putting it there and editing the latter part appropriately but now the page doesn’t show anything. I made the code into this:

    <?php get_header(); ?>
    
    <section class="content">
    
    	<?php get_template_part('inc/page-title'); ?>
    	
           <div class="pad group">
    
    <div class="entry">	
    						<div class="entry-inner">
    							<?php the_content(); ?>
    							<?php wp_link_pages(array('before'=>'<div class="post-pages">'.__('Pages:','hueman'),'after'=>'</div>')); ?>
    						</div>
    						<div class="clear"></div>				
    					</div><!--/.entry-->
    		
    <?php
    
    $temporary_list = array();
    foreach($posts as $post){
        $film_names = get_field('elokuvat_k', $post->ID);
        $film_names_array = preg_split( '/\r\n|\r|\n/', $string );
        foreach($film_names_array as $film_name){
            $temporary_list[] = array('post_id' => $post->ID, 'film_name' => $film_name);
        }
    }
    
    foreach( $temporary_list as $i => $row ) {
    	$order[ $i ] = $row['film_name'];
    }
    
    array_multisort( $order, SORT_ASC, $temporary_list );
    
    if($posts)
    {
    	echo '<ul>';
    
    	foreach($posts as $post)
    	{
    		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_field($temporary_list, $post->ID) . '</a></li>';
    	}
    
    	echo '</ul>';
    }
    
    ?>
    
    	</div><!--/.pad-->
    	
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Where did I go wrong?

  • Sure thing! So, we have a lot of articles. All of them about films. I want to compile a list of those films, ordered by the first letter of each title. Currently I have it set up as follows:

    Underneath the WYSIWYG-editor when writing an article, there’s an ACF for selecting the first letters of each film that appears in the article. When you select the letters, you get text areas into which you type, one row at a time, the names of the films in the text. Example:

    Then I use this data to create a page for each letter on the site that looks like this (the letters at the top link to the other pages but are non-functional for the moment until I lock this down):

    The code for this page looks like this:

    <?php get_header(); ?>
    
    <section class="content">
    
    	<?php get_template_part('inc/page-title'); ?>
    	
           <div class="pad group">
    
    <div class="entry">	
    						<div class="entry-inner">
    							<?php the_content(); ?>
    							<?php wp_link_pages(array('before'=>'<div class="post-pages">'.__('Pages:','hueman'),'after'=>'</div>')); ?>
    						</div>
    						<div class="clear"></div>				
    					</div><!--/.entry-->
    		
    <?php
    
    $posts = get_posts(array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_k',
            'order'=> 'ASC',
            'orderby' => 'elokuvat_k'
    ));
    
    if($posts)
    {
    	echo '<ul>';
    
    	foreach($posts as $post)
    	{
    		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_field($field_name='elokuvat_k', $post->ID) . '</a></li>';
    	}
    
    	echo '</ul>';
    }
    
    ?>
    
    	</div><!--/.pad-->
    	
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    This is all good. However, it becomes a problem if an article has two films in it that begin with the same letter. For example, if I add a film like this:

    Then it will show up like this on the page:

    The problem is that instead of creating two data entries, one of which would be Knock Knock (2015) and the other Kxick Test (2014), it creates a single entity which is known as
    Knock Knock (2015)
    Kxick Test (2014)

    This causes the alphabetical order of that page to become messy, as the lower of these two should be below, in this case, Kummeli V (2014), since it comes later in the alphabet.

    I wanted to know if there is any way to make it so that when entering multiple rows of text into the text area -box, the inputs would create their individual values into the database that the code takes them from, ergo instead of creating this single block of content it would create two separate ones.

Viewing 4 posts - 1 through 4 (of 4 total)