php - 我的 wordpress 主题需要哪个功能? query_posts()、get_posts()、get_post()

标签 php mysql wordpress

我正在制作一个wordpress 主题,我的主页由 3 个部分组成(见附图):

  1. 4 个特色页面部分(红色)
  2. 3 个特色帖子部分,“新闻”作为帖子类别(橙色)
  3. 3 个以“博客”作为帖子类别的特色帖子部分(蓝色)

第 3 部分作为侧边栏显示在我网站的所有页面上。

我想知道我是否使用正确的方式在 wordpress 中查询数据,以获取所需的数据/内容,如页面标题、页面缩略图、帖子标题等......

我读到不建议使用 query_post() 所以我想知道正确的方法是什么。 请不要看 html,我的问题只是关于 php。 重要的是要知道我的代码确实有效,所以我没有错误。

我获取数据以在第 1 部分(红色)中使用的代码:

<div id="hiContainer">
                    <?php $i = 1;
                    query_posts( 'posts_per_page=4&cat=3' );
                    if ( have_posts() ) { 
                        while ( have_posts() ) {
                            the_post();
                            if ( $i < 4 ) { 
                                echo '<div class="headerItem">';
                            } 
                            else {
                                echo '<div id="hiLast">';
                            }
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail( 'hiThumb' ); 
                            }
                    ?>
                            <div class="hiTitle">
                                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                            </div>  
                    <?php   echo '</div>';
                            $i += 1;
                        }
                    }
                    else { 
                        echo '<p>';
                        _e( 'Sorry, no posts matched your criteria.' );
                        echo '</p>';
                    }
                    ?>                      
                </div>
                <?php wp_reset_query(); ?>

我获取数据以在第二部分(橙色)中使用的代码:

<div id="niContainer">
                <?php 
                query_posts( 'posts_per_page=3&cat=5' );
                if ( have_posts() ) { 
                    while( have_posts() ) {
                        the_post();             
                        echo '<div class="newsItem">';
                        if ( has_post_thumbnail() ) {
                            echo '<div class="niImage">';
                                the_post_thumbnail( 'niThumb' ); 
                            echo '</div>';
                        }
                ?>
                        <div class="niTitle">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        </div>  
                        <div class="niExcerpt">
                            <p><?php echo get_the_excerpt(); ?></p>
                        </div>
                        <div class="niReadMore">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Lees meer <span style="color:#c73f20;">&#187;</span></a>
                        </div>
                <?php   
                        echo '</div>';
                    }
                }
                ?>  
                </div>

我获取数据以在第 3 部分(蓝色)中使用的代码:

for ( $i = 1; $i < 4; $i++ ) {
                        $post_id = ot_get_option( 'post'.$i );
                        $post = get_post($post_id);
                        $author_id = $post->post_author;
                        $author_first_name = get_the_author_meta( 'first_name', $author_id );
                        $author_last_name = get_the_author_meta( 'last_name', $author_id );
                        echo '<div class="rsbPost">';
                                echo '<div class="rsbpThumb">'; 
                                    echo get_the_post_thumbnail( $post_id ,'rsbpThumb' );
                                echo '</div>';
                            echo '<div class="rsbpTitle">';
                                echo '<a href="'; 
                                echo get_permalink( $post_id ); 
                                echo '"title="'; 
                                echo $post->post_title;
                                echo '">'; 
                                echo $post->post_title;
                                echo'</a>';
                            echo '</div>';
                            echo '<div class="rsbpMeta">';
                                if ( !empty( $author_first_name ) || !empty( $author_last_name ) ) {
                                    echo $author_first_name . " " . $author_last_name;
                                }
                                else {
                                    echo 'Redactie';
                                }                               
                                echo ', ';
                                echo mysql2date('j F Y', $post->post_date);
                            echo '</div>';
                        echo '</div>';          
                }

enter image description here

最佳答案

最好避免使用 query_posts()。在 codex 页面上有关于这一点的警告:http://codex.wordpress.org/Function_Reference/query_posts

那么解决方案是什么?

WP_Query 的新实例。它的工作方式几乎与您一直在做的完全一样。

$section1 = new WP_Query( array(
    'posts_per_page' => 4,
    'cat'            => 3,
) );

您可以在此处找到有关可能参数的更多信息: http://codex.wordpress.org/Class_Reference/WP_Query

然后替换你当前的循环你会这样做:

if ( $section1->have_posts() ) : while ( $section1->have_posts() ) : $section1->the_post();

我将其命名为第 1 节,但使用更合适的名称会更明智。 $header_posts 例如。

为您的三个区域创建 3 个实例。

要更进一步,您可以将其构建到 home.php/index.php/front-page.php 中,而不是将主页设置为静态页面。然后使用 pre_get_posts 过滤器修改主查询(标记为 2 的区域),但这超出了您的问题范围。

关于php - 我的 wordpress 主题需要哪个功能? query_posts()、get_posts()、get_post(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622829/

相关文章:

mysql - 按子模型属性的乘积之和分组

php - Magento 数据库从 1.6 迁移到 1.9 错误

php - 在php中以某种格式排列多维数组

php - MySQL:查询中的条件语句

mysql - 使用 grails 导入文件 CSV 时获取下一行

php - 如何在twitter主页上制作像Top Tweets这样的滚动新闻? (php)

html - Font Awesome 图标未显示 Impreza 主题

php - 在 Woocommerce 中以编程方式创建新的产品属性

php - WordPress 错误 : There has been a critical error on your website. 请检查您的站点管理员电子邮件收件箱以获取说明

php - MySQL 使用 HAVING 子句从多个表中选择