简体   繁体   English

如何在wordpress的单个页面中显示特定帖子?

[英]How can I display specific posts in a single page in wordpress?

I want to know if it's possible to display specific posts when I click on a link from a specific page to the (ex: gallery page).我想知道当我单击从特定页面到(例如:图库页面)的链接时是否可以显示特定的帖子。

Example: Let's say I have 3 posts with 3 different galleries.示例:假设我有 3 个帖子,其中包含 3 个不同的画廊。 (post 1=wedding pictures / post 2=food pictures / post 3=dogs pictures) and I also have 3 pages. (帖子 1=婚礼图片/帖子 2=食物图片/帖子 3=狗图片)我也有 3 页。

When I'm on Page 1 and click on the gallery page link, I want to only show post 1 content (only wedding pictures).当我在第 1 页并单击图库页面链接时,我只想显示帖子 1 的内容(仅婚礼照片)。 When I'm on Page 2 and click on the gallery page link, I want to display only the post 2 content.当我在第 2 页并单击图库页面链接时,我只想显示帖子 2 的内容。

If It's possible what would be the most simple solution?如果有可能,最简单的解决方案是什么?

Thank you!谢谢!

You can first assign each photo (or post containing a photo) a category eg Wedding, food etc. Once that is done, create a custom menu and add the categories to it.您可以首先为每张照片(或包含照片的帖子)指定一个类别,例如婚礼、食物等。完成后,创建自定义菜单并将类别添加到其中。 Place this menu on an area of your page and give it a try.将此菜单放在页面的某个区域并尝试一下。 It should only show the required images/posts.它应该只显示所需的图像/帖子。

For more info, you can have a look at the link below: https://en.support.wordpress.com/category-pages/有关更多信息,您可以查看以下链接: https : //en.support.wordpress.com/category-pages/

Note: You may need a plugin to allow you to place categories on images.注意:您可能需要一个插件来允许您在图像上放置类别。 Have a look at an example at the link below: https://wordpress.org/plugins/categories-images/查看以下链接中的示例: https : //wordpress.org/plugins/categories-images/

To show specific posts based on type, category, tag or any other property, your best bet is to use a shortcode, which gives you lots of versatility in how to render the items (in a post, page, widget, template file, etc).要根据类型、类别、标签或任何其他属性显示特定的帖子,最好的办法是使用短代码,它为您提供了如何呈现项目(在帖子、页面、小部件、模板文件等中)的多种功能)。

The shortcode could be generated by a plugin such as Bill Erickson's Display Posts , Content Views or 10's of other plugins.短代码可以由插件生成,例如Bill Erickson 的 Display PostsContent Views或 10 个其他插件。

Or do it manually by building your own "show some posts" shortcode and get a much better understanding of WP.或者通过构建自己的“显示一些帖子”短代码手动完成,并更好地了解 WP。 You'll have to build your own html output, filters and paging, but you will end up with less plugins gumming up your installation.您必须构建自己的 html 输出、过滤器和分页,但最终会减少安装的插件。 There's lots of tutorials for this, search for "Wordpress display posts shortcode functions.php".有很多这方面的教程,搜索“Wordpress display posts shortcode functions.php”。

Eg Placing the following in your theme's (ideally, a child theme) functions.php file, here is a way to show a specific number of posts from a specific content type:例如,将以下内容放入您的主题(理想情况下,子主题)functions.php 文件中,这是一种显示来自特定内容类型的特定数量帖子的方法:

Place in functions.php:放在functions.php中:

function show_some_posts($atts) {

  $a = shortcode_atts([
    'post_type' => 'post',
    'posts_per_page' => 3
  ], $atts);

    $the_query = new WP_Query( $a );

    if ( $the_query->have_posts() ) {

        $string .= '<ul>';

        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $string .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';


        }

        wp_reset_postdata();
        $string .= '</ul>';

    } else {
        $string .= 'no posts found';
    }

    return $string;
}

function shortcodes_init()
{
  add_shortcode('get-posts','get_some_posts');
}

add_action('init', 'shortcodes_init');

And to render the actual list, place this shortcode in your page or post:要呈现实际列表,请将此短代码放在您的页面或帖子中:

[get-posts posts_per_page="3"]

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

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