admin管理员组

文章数量:1317898

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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!

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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!

Share Improve this question asked Oct 27, 2020 at 14:29 matt93matt93 1111 bronze badge 3
  • the_post does not return the post, $foo = $query->the_post() resolves to null/undefined, not a post object – Tom J Nowell Commented Oct 27, 2020 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 Commented Oct 27, 2020 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 Commented Oct 27, 2020 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 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?

本文标签: shortcodeNested Code Snippets