使用特定的In-Post选项选择帖子的PHP循环

时间:2011-04-10 作者:웃mauri

我正在尝试构建一个特色帖子滑块,以显示我所看到的任何帖子mark as a "featured" (使用in-post选项)无论类别或标记类型。。。我是wordpress的新手,虽然我懂一点php语言,但我不是一个程序员:(

滑块已在工作。问题是,如何构建带有循环的滑块来显示最后一个7 贴子标记为“特色”,并带有相应的特色图片?

我已经在post edit页面的metabox中设置了in-post选项,并且在我的函数中配置了图像大小。php。。。

我在genesis框架中使用wordpress 3.1。

这是滑块代码

<div class="slider">
    <ul class="main-wapper">
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      </ul>
  </div>
  <div class="navigator-outer">
    <ul class="navigator">
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>

    </ul>
  </div>
</div>
这是滑块的屏幕截图。。Slider Screenshot

更新:在我的函数中。php文件我有这个。。

// Add new image sizes
add_image_size(\'Slider\', 315, 300, TRUE);

require_once(CHILD_DIR . \'/lib/admin/inpost-settings.php\');
wich is the code below..
inpost设置。php

$prefix = \'myslider_\';

$meta_box = array(
    \'id\' => \'slider-meta-box\',
    \'title\' => \'Featured Slider Options\',
    \'page\' => \'post\',
    \'context\' => \'normal\',
    \'priority\' => \'high\',
    \'fields\' => array(
        array(
            \'name\' => \'Show in featured slider\',
            \'id\' => $prefix . \'show_post_slider\',
            \'type\' => \'checkbox\'
        )
    )
);
add_action(\'admin_menu\', \'mytheme_add_box\');

// Add meta box
function mytheme_add_box() {
    global $meta_box;

    add_meta_box($meta_box[\'id\'], $meta_box[\'title\'], \'mytheme_show_box\', $meta_box[\'page\'], $meta_box[\'context\'], $meta_box[\'priority\']);
}

// Callback function to show fields in meta box
function mytheme_show_box() {
    global $meta_box, $post;

    // Use nonce for verification
    echo \'<input type="hidden" name="mytheme_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';

    echo \'<table class="form-table">\';

    foreach ($meta_box[\'fields\'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field[\'id\'], true);

        echo \'<tr>\',
                \'<th style="width:20%"><label for="\', $field[\'id\'], \'">\', $field[\'name\'], \'</label></th>\',
                \'<td>\';
        switch ($field[\'type\']) {
             case \'checkbox\':
                echo \'<input type="checkbox" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \' />\';
                break;
        }
        echo     \'<td>\',
            \'</tr>\';
    }

    echo \'</table>\';
}
add_action(\'save_post\', \'mytheme_save_data\');

// Save data from meta box
function mytheme_save_data($post_id) {
    global $meta_box;

    // verify nonce
    if (!wp_verify_nonce($_POST[\'mytheme_meta_box_nonce\'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    if (\'page\' == $_POST[\'post_type\']) {
        if (!current_user_can(\'edit_page\', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can(\'edit_post\', $post_id)) {
        return $post_id;
    }

    foreach ($meta_box[\'fields\'] as $field) {
        $old = get_post_meta($post_id, $field[\'id\'], true);
        $new = $_POST[$field[\'id\']];

        if ($new && $new != $old) {
            update_post_meta($post_id, $field[\'id\'], $new);
        } elseif (\'\' == $new && $old) {
            delete_post_meta($post_id, $field[\'id\'], $old);
        }
    }
}
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
 return $post_id;
}
提前谢谢。

1 个回复
最合适的回答,由SO网友:t31os 整理而成

您所需要的只是一个简单的查询,并对该查询进行多次迭代,以便构建两个所需的列表。WP_Query 有一个方便的方法来重置posts数组中的指针,因此您可以再次循环它,称为rewind_posts 虽然我相信在自定义循环中,您必须直接引用该方法。。

不管怎样,这就是你要找的东西,只需根据需要进行调整。。

<?php   
    $featured = new WP_Query;
    $featured->query( array( 
        \'meta_query\'  => array( 
            array(
                \'key\'     => \'myslider_show_post_slider\',
                \'value\'   => array(\'on\',\'1\'),
                \'compare\' => \'IN\',
                \'type\'    => \'CHAR\',
            )
        ),
        \'post_type\'       => \'post\',
        \'post_status\'     => \'publish\',
        \'ignore_sticky_posts\' => \'1\',
        \'posts_per_page\' => \'7\' //The number of post in the slider.
    ) );
    if( $featured->have_posts() ) :
    ?>

    <div class="slider">
        <ul class="main-wapper">

        <?php
        while( $featured->have_posts() ) : $featured->the_post();
        ?>

        <li>
            <?php the_post_thumbnail(); ?>
            <div class="main-item-desc">
                <h2><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p>Where\'s this description suppose to be coming from?</p>
            </div>
      </li>

        <?php
        endwhile;
        $featured->rewind_posts();
        ?>

        </ul>
    </div>
    <div class="navigator-outer">
        <ul class="navigator">

        <?php
        while( $featured->have_posts() ) : $featured->the_post();
        ?>

        <li>
            <div>
                 <?php the_post_thumbnail(\'Slider\'); ?>
                <h3><?php the_title(); ?></h3>
                <p><span class="date"><?php the_time( \'d.m.Y\' ); ?></span> | <span class="category"><?php the_category(\',\'); ?></span></p>
            </div>
        </li>

        <?php
        endwhile;
        ?>

        </ul>
    </div>

    <?php
    endif; 
    ?>
首先,您发布的HTML中有一个错误的结束DIV,所以我只是从上面的代码中删除了它。

其次,复选框没有指定值,在这种情况下,复选框没有指定特定值,浏览器会为复选框指定一个值,我不确定是否有所有浏览器都会使用的通用值,但Firefox会生成该值on, 但我考虑了浏览器可能会指定的值1, 理想情况下,应为复选框指定一个隐式值,以确保一致的行为,例如。。

echo \'<input type="checkbox" value="1" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \' />\';
有什么问题,让我知道。。

相关推荐

Increase offset while looping

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp

使用特定的In-Post选项选择帖子的PHP循环 - 小码农CODE - 行之有效找到问题解决它

使用特定的In-Post选项选择帖子的PHP循环

时间:2011-04-10 作者:웃mauri

我正在尝试构建一个特色帖子滑块,以显示我所看到的任何帖子mark as a "featured" (使用in-post选项)无论类别或标记类型。。。我是wordpress的新手,虽然我懂一点php语言,但我不是一个程序员:(

滑块已在工作。问题是,如何构建带有循环的滑块来显示最后一个7 贴子标记为“特色”,并带有相应的特色图片?

我已经在post edit页面的metabox中设置了in-post选项,并且在我的函数中配置了图像大小。php。。。

我在genesis框架中使用wordpress 3.1。

这是滑块代码

<div class="slider">
    <ul class="main-wapper">
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      <li> <img src="path to featured img" title="the post title" height="300" width="315">
        <div class="main-item-desc">
          <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>
          <p>Post Description limited to 150 character</p>
        </div>
      </li>
      </ul>
  </div>
  <div class="navigator-outer">
    <ul class="navigator">
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>
      <li>
        <div> <img src="img src" />
          <h3>Content Title H3</h3>
          <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>
      </li>

    </ul>
  </div>
</div>
这是滑块的屏幕截图。。Slider Screenshot

更新:在我的函数中。php文件我有这个。。

// Add new image sizes
add_image_size(\'Slider\', 315, 300, TRUE);

require_once(CHILD_DIR . \'/lib/admin/inpost-settings.php\');
wich is the code below..
inpost设置。php

$prefix = \'myslider_\';

$meta_box = array(
    \'id\' => \'slider-meta-box\',
    \'title\' => \'Featured Slider Options\',
    \'page\' => \'post\',
    \'context\' => \'normal\',
    \'priority\' => \'high\',
    \'fields\' => array(
        array(
            \'name\' => \'Show in featured slider\',
            \'id\' => $prefix . \'show_post_slider\',
            \'type\' => \'checkbox\'
        )
    )
);
add_action(\'admin_menu\', \'mytheme_add_box\');

// Add meta box
function mytheme_add_box() {
    global $meta_box;

    add_meta_box($meta_box[\'id\'], $meta_box[\'title\'], \'mytheme_show_box\', $meta_box[\'page\'], $meta_box[\'context\'], $meta_box[\'priority\']);
}

// Callback function to show fields in meta box
function mytheme_show_box() {
    global $meta_box, $post;

    // Use nonce for verification
    echo \'<input type="hidden" name="mytheme_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';

    echo \'<table class="form-table">\';

    foreach ($meta_box[\'fields\'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field[\'id\'], true);

        echo \'<tr>\',
                \'<th style="width:20%"><label for="\', $field[\'id\'], \'">\', $field[\'name\'], \'</label></th>\',
                \'<td>\';
        switch ($field[\'type\']) {
             case \'checkbox\':
                echo \'<input type="checkbox" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \' />\';
                break;
        }
        echo     \'<td>\',
            \'</tr>\';
    }

    echo \'</table>\';
}
add_action(\'save_post\', \'mytheme_save_data\');

// Save data from meta box
function mytheme_save_data($post_id) {
    global $meta_box;

    // verify nonce
    if (!wp_verify_nonce($_POST[\'mytheme_meta_box_nonce\'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    if (\'page\' == $_POST[\'post_type\']) {
        if (!current_user_can(\'edit_page\', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can(\'edit_post\', $post_id)) {
        return $post_id;
    }

    foreach ($meta_box[\'fields\'] as $field) {
        $old = get_post_meta($post_id, $field[\'id\'], true);
        $new = $_POST[$field[\'id\']];

        if ($new && $new != $old) {
            update_post_meta($post_id, $field[\'id\'], $new);
        } elseif (\'\' == $new && $old) {
            delete_post_meta($post_id, $field[\'id\'], $old);
        }
    }
}
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
 return $post_id;
}
提前谢谢。

1 个回复
最合适的回答,由SO网友:t31os 整理而成

您所需要的只是一个简单的查询,并对该查询进行多次迭代,以便构建两个所需的列表。WP_Query 有一个方便的方法来重置posts数组中的指针,因此您可以再次循环它,称为rewind_posts 虽然我相信在自定义循环中,您必须直接引用该方法。。

不管怎样,这就是你要找的东西,只需根据需要进行调整。。

<?php   
    $featured = new WP_Query;
    $featured->query( array( 
        \'meta_query\'  => array( 
            array(
                \'key\'     => \'myslider_show_post_slider\',
                \'value\'   => array(\'on\',\'1\'),
                \'compare\' => \'IN\',
                \'type\'    => \'CHAR\',
            )
        ),
        \'post_type\'       => \'post\',
        \'post_status\'     => \'publish\',
        \'ignore_sticky_posts\' => \'1\',
        \'posts_per_page\' => \'7\' //The number of post in the slider.
    ) );
    if( $featured->have_posts() ) :
    ?>

    <div class="slider">
        <ul class="main-wapper">

        <?php
        while( $featured->have_posts() ) : $featured->the_post();
        ?>

        <li>
            <?php the_post_thumbnail(); ?>
            <div class="main-item-desc">
                <h2><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p>Where\'s this description suppose to be coming from?</p>
            </div>
      </li>

        <?php
        endwhile;
        $featured->rewind_posts();
        ?>

        </ul>
    </div>
    <div class="navigator-outer">
        <ul class="navigator">

        <?php
        while( $featured->have_posts() ) : $featured->the_post();
        ?>

        <li>
            <div>
                 <?php the_post_thumbnail(\'Slider\'); ?>
                <h3><?php the_title(); ?></h3>
                <p><span class="date"><?php the_time( \'d.m.Y\' ); ?></span> | <span class="category"><?php the_category(\',\'); ?></span></p>
            </div>
        </li>

        <?php
        endwhile;
        ?>

        </ul>
    </div>

    <?php
    endif; 
    ?>
首先,您发布的HTML中有一个错误的结束DIV,所以我只是从上面的代码中删除了它。

其次,复选框没有指定值,在这种情况下,复选框没有指定特定值,浏览器会为复选框指定一个值,我不确定是否有所有浏览器都会使用的通用值,但Firefox会生成该值on, 但我考虑了浏览器可能会指定的值1, 理想情况下,应为复选框指定一个隐式值,以确保一致的行为,例如。。

echo \'<input type="checkbox" value="1" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \' />\';
有什么问题,让我知道。。

相关推荐

当in_the_loop()为假时,何时以及为什么is_Single(‘my_cpt’)为真?

我正在使用模板系统的示例代码。此地址的页码:http://project.test/my_cpt/hello-post/.无法理解原因is_singular( \'my_cpt\' ) 是true 虽然in_the_loop() 是false.在页面模板中The Loop "E;“工程”:if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>