如何创建页面并只显示具有特定自定义字段值的帖子?

时间:2013-06-03 作者:Charitos

我想创建一个页面(让我们把它命名为“Facebook”),在那里我将只显示具有自定义字段“Facebook”的帖子,特别是自定义字段值“Yes”。

P、 第1节。我想包括主题的标题,滑块,页脚,边栏等2。我使用高级自定义字段插件

<?php get_header(); ?>
<div class="content-wrap">
<div class="content">
    <?php tie_breadcrumbs() ?>

    <?php if ( ! have_posts() ) : ?>
        <div id="post-0" class="post not-found post-listing">
            <h1 class="post-title"><?php _e( \'Not Found\', \'tie\' ); ?></h1>
            <div class="entry">
                <p><?php _e( \'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.\', \'tie\' ); ?></p>
                <?php get_search_form(); ?>
            </div>
        </div>
    <?php endif; ?>

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php $get_meta = get_post_custom($post->ID);  ?>
    <?php //Above Post Banner
    if( empty( $get_meta["tie_hide_above"][0] ) ){
        if( !empty( $get_meta["tie_banner_above"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_above"][0]) .\'</div>\';
        else tie_banner(\'banner_above\' , \'<div class="ads-post">\' , \'</div>\' );
    }
    ?>

    <article class="post-listing post">
        <?php tie_include( \'post-head\' ); // Get Post Head template ?>
        <div class="post-inner">
            <div class="clear"></div>
            <div class="entry">
                <?php the_content(); ?>
                <?php wp_link_pages( array( \'before\' => \'<div class="page-link">\' . __( \'Pages:\', \'tie\' ), \'after\' => \'</div>\' ) ); ?>

                <?php edit_post_link( __( \'Edit\', \'tie\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
            </div><!-- .entry /-->  

        </div><!-- .post-inner -->
    </article><!-- .post-listing -->
    <?php endwhile; ?>

    <?php //Below Post Banner
    if( empty( $get_meta["tie_hide_below"][0] ) ){
        if( !empty( $get_meta["tie_banner_below"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_below"][0]) .\'</div>\';
        else tie_banner(\'banner_below\' , \'<div class="ads-post">\' , \'</div>\' );
    }
    ?>

    <?php comments_template( \'\', true ); ?>
</div><!-- .content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

2 个回复
SO网友:Nicolai Grossherr

复制您的page.php 模板,命名副本page-facebook.php, 为其指定模板名称:

/*
Template Name: My Facebook Meta Key Page
*/
更多信息请参见:http://codex.wordpress.org/Page_Templates

要获得正确的帖子,请使用get_posts 使用类似以下内容:

<?php
$args = array(
    \'post_type\' => \'post\',
    \'meta_query\' => array(
        array(
            \'key\' => \'facebook\',
            \'value\' => \'yes\',
        )
    )
 );
$fbposts = get_posts( $args );

foreach($fbposts as $post) : setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; ?>
更多信息请参见:http://codex.wordpress.org/Template_Tags/get_posts

你当然可以用WP_Query 正如GhostToast所建议的那样,它非常相似,请看这里:http://codex.wordpress.org/Class_Reference/WP_Query

使用获取帖子WP_Query 像这样:

<?php
$args = array(
    \'post_type\' => \'post\',
    \'meta_query\' => array(
        array(
            \'key\' => \'facebook\',
            \'value\' => \'yes\',
        )
    )
 );
$fbposts = new WP_Query( $args );

if ( $fbposts->have_posts() ) {
  while ( $fbposts->have_posts() ) {
  $fbposts->the_post();
      echo \'<h2><a href="\'. the_permalink() .\'">\'. the_title() .\'</a></h2>\';
      echo the_content();
  }
} else {

}
wp_reset_postdata(); 
?>

SO网友:GhostToast

你需要学会如何create a custom page template 并使用meta_query. 为了确保您的新页面模板包含您想要的所有其他元素,我建议从复制模板文件开始(例如page.php) 这已经按照您想要的方式工作了(就结构和包含而言)。你会把loop 分区并编写自己的wp_query.

欢迎来到WordPress的授权端!

编辑:

这是您提供的代码的修改版本。请意识到,正如@ngsonst所建议的,您应该将其保存为另一个文件,例如facebook-page.php. 然后,您将从WordPress后端创建一个新页面,并将其分配给该模板,该模板在文件顶部命名。

<?php
/****
Template Name: Facebook Page
****/

get_header(); ?>
<div class="content-wrap">
<div class="content">
    <?php tie_breadcrumbs();

    // define parameters for which posts we want
    $args = array(
        \'post_type\' => \'post\',
        \'meta_query\' => array(
            array(
            \'key\' => \'facebook\',
            \'value\' => \'yes\',
            )
        )
    );

    // create new instance of wp_query, the object which loops through posts
    $loop = new wp_query($args);

    // as long as it has posts, set current post up and do all of the following
    while($loop->have_posts()): $loop->the_post();

        $get_meta = get_post_custom($post->ID);
        //Above Post Banner
        if( empty( $get_meta["tie_hide_above"][0] ) ){
            if( !empty( $get_meta["tie_banner_above"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_above"][0]) .\'</div>\';
            else tie_banner(\'banner_above\' , \'<div class="ads-post">\' , \'</div>\' );
        }
        ?>
        <article class="post-listing post">
            <?php tie_include( \'post-head\' ); // Get Post Head template ?>
            <div class="post-inner">
                <div class="clear"></div>
                <div class="entry">
                    <?php 
                    // get title as permalink
                    echo \'<h2><a href="\'. get_permalink() .\'">\'. get_the_title() .\'</a></h2>\';
                    // get content
                    the_content();
                    wp_link_pages( array( \'before\' => \'<div class="page-link">\' . __( \'Pages:\', \'tie\' ), \'after\' => \'</div>\' ) ); 

                    edit_post_link( __( \'Edit\', \'tie\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
                </div><!-- .entry /-->  

            </div><!-- .post-inner -->
        </article><!-- .post-listing -->

    <?php
    // end our loop
    endwhile; 

    //Below Post Banner
    if( empty( $get_meta["tie_hide_below"][0] ) ){
        if( !empty( $get_meta["tie_banner_below"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_below"][0]) .\'</div>\';
        else tie_banner(\'banner_below\' , \'<div class="ads-post">\' , \'</div>\' );
    }


    comments_template( \'\', true ); ?>
</div><!-- .content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
意识到我不知道tie_breadcrumbs 东西是,而且没有办法知道它是否能与此配合使用。你可以很容易地把所有这些部分都消化掉,但想让你自己去发现。让我们知道这是怎么回事。

结束

相关推荐

将唯一类添加到WP_LINK_PAGES中的下一个和上一个

我正在为wp\\U link\\U页面使用以下函数。It can be viewed in use here.我想在上一个链接中添加一个类,在下一个链接中添加一个类,这样它们就可以单独设置样式。我不是WP函数的专家,我很好奇这样做的正确方法是什么。我总是可以简单地在每个div周围回显div,但似乎有更好的方法可以做到这一点。// WP_LINK_PAGES: Add prev and next links to a numbered link list add_filter(\'wp_link_p

如何创建页面并只显示具有特定自定义字段值的帖子? - 小码农CODE - 行之有效找到问题解决它

如何创建页面并只显示具有特定自定义字段值的帖子?

时间:2013-06-03 作者:Charitos

我想创建一个页面(让我们把它命名为“Facebook”),在那里我将只显示具有自定义字段“Facebook”的帖子,特别是自定义字段值“Yes”。

P、 第1节。我想包括主题的标题,滑块,页脚,边栏等2。我使用高级自定义字段插件

<?php get_header(); ?>
<div class="content-wrap">
<div class="content">
    <?php tie_breadcrumbs() ?>

    <?php if ( ! have_posts() ) : ?>
        <div id="post-0" class="post not-found post-listing">
            <h1 class="post-title"><?php _e( \'Not Found\', \'tie\' ); ?></h1>
            <div class="entry">
                <p><?php _e( \'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.\', \'tie\' ); ?></p>
                <?php get_search_form(); ?>
            </div>
        </div>
    <?php endif; ?>

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php $get_meta = get_post_custom($post->ID);  ?>
    <?php //Above Post Banner
    if( empty( $get_meta["tie_hide_above"][0] ) ){
        if( !empty( $get_meta["tie_banner_above"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_above"][0]) .\'</div>\';
        else tie_banner(\'banner_above\' , \'<div class="ads-post">\' , \'</div>\' );
    }
    ?>

    <article class="post-listing post">
        <?php tie_include( \'post-head\' ); // Get Post Head template ?>
        <div class="post-inner">
            <div class="clear"></div>
            <div class="entry">
                <?php the_content(); ?>
                <?php wp_link_pages( array( \'before\' => \'<div class="page-link">\' . __( \'Pages:\', \'tie\' ), \'after\' => \'</div>\' ) ); ?>

                <?php edit_post_link( __( \'Edit\', \'tie\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
            </div><!-- .entry /-->  

        </div><!-- .post-inner -->
    </article><!-- .post-listing -->
    <?php endwhile; ?>

    <?php //Below Post Banner
    if( empty( $get_meta["tie_hide_below"][0] ) ){
        if( !empty( $get_meta["tie_banner_below"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_below"][0]) .\'</div>\';
        else tie_banner(\'banner_below\' , \'<div class="ads-post">\' , \'</div>\' );
    }
    ?>

    <?php comments_template( \'\', true ); ?>
</div><!-- .content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

2 个回复
SO网友:Nicolai Grossherr

复制您的page.php 模板,命名副本page-facebook.php, 为其指定模板名称:

/*
Template Name: My Facebook Meta Key Page
*/
更多信息请参见:http://codex.wordpress.org/Page_Templates

要获得正确的帖子,请使用get_posts 使用类似以下内容:

<?php
$args = array(
    \'post_type\' => \'post\',
    \'meta_query\' => array(
        array(
            \'key\' => \'facebook\',
            \'value\' => \'yes\',
        )
    )
 );
$fbposts = get_posts( $args );

foreach($fbposts as $post) : setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; ?>
更多信息请参见:http://codex.wordpress.org/Template_Tags/get_posts

你当然可以用WP_Query 正如GhostToast所建议的那样,它非常相似,请看这里:http://codex.wordpress.org/Class_Reference/WP_Query

使用获取帖子WP_Query 像这样:

<?php
$args = array(
    \'post_type\' => \'post\',
    \'meta_query\' => array(
        array(
            \'key\' => \'facebook\',
            \'value\' => \'yes\',
        )
    )
 );
$fbposts = new WP_Query( $args );

if ( $fbposts->have_posts() ) {
  while ( $fbposts->have_posts() ) {
  $fbposts->the_post();
      echo \'<h2><a href="\'. the_permalink() .\'">\'. the_title() .\'</a></h2>\';
      echo the_content();
  }
} else {

}
wp_reset_postdata(); 
?>

SO网友:GhostToast

你需要学会如何create a custom page template 并使用meta_query. 为了确保您的新页面模板包含您想要的所有其他元素,我建议从复制模板文件开始(例如page.php) 这已经按照您想要的方式工作了(就结构和包含而言)。你会把loop 分区并编写自己的wp_query.

欢迎来到WordPress的授权端!

编辑:

这是您提供的代码的修改版本。请意识到,正如@ngsonst所建议的,您应该将其保存为另一个文件,例如facebook-page.php. 然后,您将从WordPress后端创建一个新页面,并将其分配给该模板,该模板在文件顶部命名。

<?php
/****
Template Name: Facebook Page
****/

get_header(); ?>
<div class="content-wrap">
<div class="content">
    <?php tie_breadcrumbs();

    // define parameters for which posts we want
    $args = array(
        \'post_type\' => \'post\',
        \'meta_query\' => array(
            array(
            \'key\' => \'facebook\',
            \'value\' => \'yes\',
            )
        )
    );

    // create new instance of wp_query, the object which loops through posts
    $loop = new wp_query($args);

    // as long as it has posts, set current post up and do all of the following
    while($loop->have_posts()): $loop->the_post();

        $get_meta = get_post_custom($post->ID);
        //Above Post Banner
        if( empty( $get_meta["tie_hide_above"][0] ) ){
            if( !empty( $get_meta["tie_banner_above"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_above"][0]) .\'</div>\';
            else tie_banner(\'banner_above\' , \'<div class="ads-post">\' , \'</div>\' );
        }
        ?>
        <article class="post-listing post">
            <?php tie_include( \'post-head\' ); // Get Post Head template ?>
            <div class="post-inner">
                <div class="clear"></div>
                <div class="entry">
                    <?php 
                    // get title as permalink
                    echo \'<h2><a href="\'. get_permalink() .\'">\'. get_the_title() .\'</a></h2>\';
                    // get content
                    the_content();
                    wp_link_pages( array( \'before\' => \'<div class="page-link">\' . __( \'Pages:\', \'tie\' ), \'after\' => \'</div>\' ) ); 

                    edit_post_link( __( \'Edit\', \'tie\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
                </div><!-- .entry /-->  

            </div><!-- .post-inner -->
        </article><!-- .post-listing -->

    <?php
    // end our loop
    endwhile; 

    //Below Post Banner
    if( empty( $get_meta["tie_hide_below"][0] ) ){
        if( !empty( $get_meta["tie_banner_below"][0] ) ) echo \'<div class="ads-post">\' .htmlspecialchars_decode($get_meta["tie_banner_below"][0]) .\'</div>\';
        else tie_banner(\'banner_below\' , \'<div class="ads-post">\' , \'</div>\' );
    }


    comments_template( \'\', true ); ?>
</div><!-- .content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
意识到我不知道tie_breadcrumbs 东西是,而且没有办法知道它是否能与此配合使用。你可以很容易地把所有这些部分都消化掉,但想让你自己去发现。让我们知道这是怎么回事。

相关推荐

Read_Private_Pages功能不适用于新角色

我已经创建了一个新的用户角色-供应商-能够read_private_pages, 但当我以这样的用户身份登录并转到一个私人页面时,我看不到它。以下是我的插件代码:function fc_add_role($role, $display_name, $capabilities = array()) { if (!empty($role)) { return wp_roles()->add_role( $role, $display_name, $capabili