1

I'm new to wordpress and I'm trying to accomplish the following goal:

I have a custom type albums seeded by a plugin and I would like to display this data in a list.

I installed the Code Snippets Plugin and was able to create a following working shortcode:

function display_albums_shortcode() {
    $query = new WP_Query(
        array(
            'post_type' => 'albums'
        ));
        
    $output = '<section>';

    if ($query->have_posts()):
        while ( $query->have_posts() ):
            $album = $query->the_post();
            $output .= '<div><figure>' . get_the_title($album) . ' </figure></div>';
        endwhile; 
    endif;
    
    $output .= '</section>';

    wp_reset_postdata();

    return $output;
}

add_shortcode('display_albums', 'display_albums_shortcode');

However, I would like to make it modularize it a bit. I would like to have an isolated function (code snippet) called render_album_list_item that takes the album as an argument and returns the HTML:

function render_album_list_item($album) {
    $output = '<div><figure>' . get_the_title($album) . ' </figure></div>';
    return $output;
}

Subsequently, I would like to call render_album_list_item inside of the first code snippet, something like:

function display_albums_shortcode() {
    $query = new WP_Query(
        array(
            'post_type' => 'albums'
        ));

    $output = '<section>';

    if ($query->have_posts()):
        while ( $query->have_posts() ):
            $album = $query->the_post();
            $output .= render_album_list_item($album);
        endwhile; 
    endif;

    $output .= '</section>';

    wp_reset_postdata();

    return $output;
}

add_shortcode('display_albums', 'display_albums_shortcode');

Unfortunately, I cannot make the snippet above work and haven't found anything in the documentation.

Also: is this the only way how to display the data - by manually creating a HTML string? Coming from a ReactJS world, this approach is pretty nasty. Any ideas, advice would be appreciated!

Thanks!

3
  • the_post does not return the post, $foo = $query->the_post() resolves to null/undefined, not a post object – Tom J Nowell Oct 27 '20 at 14:54
  • I've only just realised you're talking about the code snippets pro plugin, 3rd party plugin dev support is offtopic here. I would also strongly advise against plugins that let you put PHP code in the database. It is a development dead end that relies on a major security hole to work. All your code snippets can be copy pasted into PHP files in the plugins folder ( a plugin is just a PHP file with a comment at the top, e.g. this is a valid plugin: <?php /** Plugin name: 1 line plugin */ ) – Tom J Nowell Oct 27 '20 at 14:58
  • great tip @TomJNowell , thanks, I'll try to make it work using the plugin. One more question - could you please give me a tip on how to implement the pagination here? I don't really know where to start. Cheers! – matt93 Oct 27 '20 at 15:21
0

I managed to get it work by creating one code snippet:

function render_album_list_item($album) {
    $output = '<div><figure>' . get_the_title($album) . ' </figure></div>';
    return $output;
}

function display_albums_shortcode() {
    $query = new WP_Query(
        array(
            'post_type' => 'albums'
        ));

    $output = '<section>';

    if ($query->have_posts()):
        while ( $query->have_posts() ):
            $album = $query->the_post();
            $output .= render_album_list_item($album);
        endwhile; 
    endif;

    $output .= '</section>';

    wp_reset_postdata();

    return $output;
}

add_shortcode('display_albums', 'display_albums_shortcode');

Still, is this the best way to achieve this goal?

1
  • Note that this isn't a discussion forum, but a Q&A site, you need to be able to mark an answer not just as the best answer, but as the answer, the canonical answer that completely answers your question in a concrete and factual manner. An answer cannot itself be a new question – Tom J Nowell Oct 27 '20 at 14:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.