【问题标题】:How to show a specific post on a page on my wordpress theme如何在我的 wordpress 主题页面上显示特定帖子
【发布时间】:2016-07-21 02:33:00
【问题描述】:

我正在尝试在我的 wordpress 主题上显示页面上的特定帖子。在这种情况下,我试图在主页上显示帖子,并且我已经尝试了很多至少显示标题,但我得到的只是页面的标题,而不是帖子本身。

我尝试了the_titleget_the_title(),但魔法没有发生。

这里是与我的问题相关的代码。

home.php

<?php

    if ( have_posts() ) :


        /* Start the Loop */
        while ( have_posts() ) : the_post();?>


    <?php
            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            // if ( has_post_format( 'video' )) {
            //   echo 'this is the video format';
            // }
            get_template_part( 'template-parts/content-chat', get_post_format('chat') );

        endwhile;


    else :

        // get_template_part( 'template-parts/content', 'none' );

    endif; ?>

它调用文件(它正在调用正确的文件,仔细检查)

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >

    <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
    <?php the_content(); 
        echo get_post_format();
    ?>


</article><!-- #post-## -->

总之,问题是:

1) 为什么显示页面的标题?
2) 如何在我的页面中正确显示这种帖子格式?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    对于您的第一个问题,wp 根据 query_posts() 抓取主页的标题,这就是 wordpress 如此动态的原因,您实际上可以检查页面或类别 ID,然后使用查询帖子选择要在其上显示的内容。由于您的代码中没有 query_posts,因此 wp 默认为当前的帖子 ID,即您的主页。

    对于你的第二个,我有一个代码 sn-p,但它在家里,你可以在这里查找 https://developer.wordpress.org/reference/functions/query_posts/

    好的,这里是sn-p。抱歉耽搁了,我一直在休假。

    首先你要确定你的主页的ID,或者你可以使用主页功能。如果您走前一条路线,您只需转到主页的后端并用鼠标悬停在它上面。主页应显示在页面底部的链接工具提示中。这将类似于 post=5 这个数字就是你需要的。 接下来,我将为您要在首页上显示的帖子创建一个类别,并从后端的类别页面中获取该 ID。再次,它将类似于 tag_id=12

     <?php 
    
                $currpage = $post -> ID;
                if($currpage == 5) { 
                  query_posts('showposts=1&cat=12');
                }
                while (have_posts()): the_post();  
    

    您可以通过这种方式在一个页面上放置多个帖子,实际上您可以在该页面上放置整个循环,然后在其下动态填充其他帖子,如下例所示: http://testex-ndt.com/products/ect-products/ 顶部是循环工作的页面,底部(推荐)是分配给特定类别的帖子,此处的 showposts 设置为 3,并将显示最新三个帖子的摘录。这是我过去做的一个网站。

    您可能要记住的另一件事是页面模板,如果您希望一个页面以一种方式工作,而您希望另一种类型的页面以另一种方式工作,您将不得不研究模板。

    这是该页面的完整代码。

    <?php
    /*
    Template Name:Main Product
    */
    ?>
    <?php get_header(); ?><!-- BANNER is part of main homepage only -->
        <?php if (have_posts()) : ?>
            <?php while (have_posts()): the_post(); ?>
            <?php $postName = get_the_title($post); ?>
        <!-- title link dynamic -->
            <?php edit_post_link('Edit this item','',' | '); ?>
            <?php the_content('Continue Reading'); ?>
            <?php posts_nav_link(); ?>
            <?php endwhile; ?>
            <?php else : ?>
            <div class="w3-col text">
            <h2>Not Found</h2>
            <p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
            <?php get_search_form(); ?>
            </div>
            <?php endif; ?>
    
        <!-- insert testimonial here -->
        <section class="w3-row cl testimonials">
    
    
        <!-- make code for query based on the page, then get the top three testimonials in excerpt form  This is commented out because I did it with a function I attached to the header I will show below
            $currpage = $post -> ID;
            if($currpage == xx){
                $cat  = x;
            }
                -->
    
            <h2>Testimonials for <?php echo $postName; ?></h2>  
                <?php 
                $currpage = $post -> ID;
                $cat = testimonial($currpage);  //here is the function 
                query_posts('showposts=3&cat=' .$cat ); ?>
            <?php while (have_posts()): the_post(); ?>
            <div class="w3-col excerpt" style="width:30%;"> 
            <?php the_excerpt(''); ?>
                </div>
            <?php endwhile;
             wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS
    
          ?>
        </section>
    
    
        <!-- /content float -->
        </div>
    <!-- /content -->
    </div>
    

    所以函数被设置为一个单独的 php 文件,所以我可以模块化地改变它。我调用了文件 category_help.php

    <?php
    /*This particular block of code is only for determining what category     of testimonial is allowed in a post. */
    
    function testimonial($myPageID){
        //services (all cats)
        $id = "-15,-14,-13";
        //Products 
        //lfet
        if($myPageID == 18) $id = 2;
        //bfet
        if($myPageID == 22) $id = 4;
        //rfet
        if($myPageID == 20) $id = 3;
        //ect
        if($myPageID == 24) $id = 5;
        //ultrasound
        if($myPageID == 26) $id = 8;
        return $id;
    } 
    ?>
    

    然后我在 header.php 的顶部调用了这个

    <?php require 'category_help.php'; ?>
    

    【讨论】:

    • 嗨@Danimal,谢谢你的问题。我不着急,你能给我看看sn-p吗?
    • 我可能误解了您的问题。看起来您是在 while 循环之外调用 content() 。它需要在里面。请参阅我上面的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多