首页 文章

如何在我的wordpress主题页面上显示特定帖子

提问于
浏览
0

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

我已经尝试了the_titleget_the_title(),但魔法没有发生 .

Example

这是与我的问题相关的代码 .

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)为什么显示页面 Headers ?
2)如何在我的页面中正确显示这种帖子格式?

1 回答

  • 0

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

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

    好的,所以这里是片段 . 抱歉延误了,我一直在度假 .

    首先,您必须确定主页的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/顶部是工作循环和底部的页面(推荐)是分配到特定类别的帖子,此处的展示位置设置为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'; ?>
    

相关问题