简体   繁体   English

在短代码中显示特定类别的帖子标题(wordPress)

[英]Displaying post titles in a specific category in a shortcode (wordPress)

Trying to write my first short-code that displays all post-titles in a specific Category. 尝试编写我的第一个短代码,显示特定类别中的所有后期标题。 But it is not displaying the actual results, just the short code. 但它没有显示实际结果,只显示短代码。 Here is what I have so far in my child theme's functions.php file: 这是我目前在我的孩子主题的functions.php文件中所拥有的:

function posts_in_cat() {
echo '<ul>';
query_posts('cat=3'); while (have_posts()) : the_post();
echo ('<li><a href="' . the_permalink() . '">' . the_title() . '</a></li>');
endwhile;
wp_reset_query();
echo '</ul>';
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );

And then I am calling the short code, like so [display_posts_in_cat] . 然后我调用短代码,就像这样[display_posts_in_cat]

Any help would be greatly appreciated as I try and learn this. 当我尝试学习这个时,任何帮助都会非常感激。

EDIT: I have gotten it to display but the link itself is being echo-ed in front of the title in text. 编辑:我已经让它显示但链接本身正在文本标题前面回显。 Also, it is not displaying more than 10 of the titles and I want it to display them all. 此外,它不会显示超过10个标题,我希望它显示所有标题。 Any ideas...?? 有任何想法吗...?? Thanks. 谢谢。

First and foremost, avoid using query_posts() – it's inefficient. 首先,避免使用query_posts() - 它效率低下。 Check out this SO answer for the skinny. 查看这个瘦小的SO答案

Additionally, shortcodes shouldn't make use of echo statements. 此外,短代码不应使用echo语句。 Shortcodes only return text. 短代码只返回文本。 Put simply, WordPress has PHP internally that says "when this specific shortcode is entered into the editor, replace it with the text returned from this function." 简而言之,WordPress内部有PHP,它说“当这个特定的短代码输入到编辑器中时,将其替换为从该函数返回的文本。” Using echo causes you to immediately print those statements to the screen, rather than returning to WordPress so that it can continue with its regular process. 使用echo会使您立即将这些语句打印到屏幕上,而不是返回到WordPress,以便它可以继续其常规过程。 More on the WP Codex . 关于WP Codex的更多信息。

And, finally, shortcode functions require $atts as a parameter. 最后,短代码函数需要$atts作为参数。

So, with all that said, here's how I would rewrite your function: 所以,尽管如此,这是我如何重写你的功能:

<?php

function posts_in_cat( $atts ) {
    $atts = shortcode_atts( array(
        'cat' => '',
    ), $atts );

    if ( empty( $atts['cat'] ) ) {
        // If category provided, exit early
        return;
    }

    $args = array(
        'category' => $atts['cat'],
        // Disable pagination
        'posts_per_page' => -1
    );

    $posts_list = get_posts( $args );

    if ( empty( $posts_list) ) {
        // If no posts, exit early
        return;
    }

    $opening_tag = '<ul>';
    $closing_tag = '</ul>';
    $post_content = '';

    foreach ( $posts_list as $post_cat ) {
        $post_content .= '<li><a href="' . esc_url( get_permalink( $post_cat->ID ) )  . '">' . esc_html( get_the_title( $post_cat->ID ) ) . '</a></li>';
    }

    return $opening_tag . $post_content . $closing_tag;
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );

I'm just adding all of the content you were echo ing into a handful of variables, and then returning them, concatenated, at the end. 我只是将所有你的内容的echo荷兰国际集团成少数几个变量,然后返回它们,级联,在最后。 Also, I added in an if statement to exit early if there aren't any posts in the category. 另外, if该类别中没有任何帖子,我在if语句中添加了提前退出。 That way you don't have an empty <ul> element cluttering up the page. 这样你就没有一个空的<ul>元素使页面混乱。

I've also escaped the outputs, which you can read about on the Codex. 我也逃过了输出,您可以在Codex上阅读

Please try this: 请试试这个:

 function posts_in_cat() { ?>
<ul class="posts">
<?php query_posts('cat=3&showposts=50'); while (have_posts()) : the_post(); 
 ?>
    <li><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>
<?php endwhile; ?>

<?php wp_reset_query(); ?>
  </ul>

  <?php

    }
    add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM