【问题标题】:'Themeing' my Slider in WP with PHP使用 PHP 在 WP 中“主题化”我的滑块
【发布时间】:2012-02-14 21:42:33
【问题描述】:

我在我的自定义 Wordpress 主题中的内容上方实现了一个非常基本的“特色列表 - 图像滑块”。我想知道如何在两者之间对 PHP 进行硬编码,以将我的滑块与我的“主题”连接起来。 我正在尝试“主题”滑块,以便通过“最近的帖子”或“类别”拉出内容。以及如何将“精选图片”设置为在滑块中显示为照片并显示在列表区域的大量缩略图部分中?

这是我选择的 jQuery I 插件的屏幕截图;
(他们的演示坏了,所以。)

下面是我实现的标记。

  <div id="featured" >  
    <ul class="ui-tabs-nav">  
        <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-1"><a href="#fragment-1"><img src="images/image1-small.jpg" alt="" /><span>David King – on his True Crime thriller</span></a></li>  
        <li class="ui-tabs-nav-item" id="nav-fragment-2"><a href="#fragment-2"><img src="images/image2-small.jpg" alt="" /><span>Tips from Steve Perry</span></a></li>  
        <li class="ui-tabs-nav-item" id="nav-fragment-3"><a href="#fragment-3"><img src="images/image3-small.jpg" alt="" /><span>Tips from Chuck Berry</span></a></li>  
        <li class="ui-tabs-nav-item" id="nav-fragment-4"><a href="#fragment-4"><img src="images/image4-small.jpg" alt="" /><span>SFIRS</span></a></li>  
     </ul>  




    <!-- First Content -->  
    <div id="fragment-1" class="ui-tabs-panel" style="">  
        <img src="images/image1.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >David King – on his True Crime thriller</a></h2>  
        <p>David King is Lorem Ipsum Lorem Ipsum Lorem Ipsum...<a href="#" >read more</a></p>  
        </div>  
    </div>  
    <!-- Second Content -->  
    <div id="fragment-2" class="ui-tabs-panel ui-tabs-hide" style="">  
        <img src="images/image2.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >Tips from Steve Perry</a></h2>  
        <p>Steve Perry is Lorem Ipsum Lorem Ipsum Lorem Ipsum...<a href="#" >read more</a></p>  
        </div>  
    </div>  
    <!-- Third Content -->  
    <div id="fragment-3" class="ui-tabs-panel ui-tabs-hide" style="">  
        <img src="images/image3.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >Tips from Chuck Berry</a></h2>  
        <p>Chuck Berry is Lorem Ipsum Lorem Ipsum Lorem Ipsum...<a href="#" >read more</a></p>  
        </div>  
    </div>  
    <!-- Fourth Content -->  
    <div id="fragment-4" class="ui-tabs-panel ui-tabs-hide" style="">  
        <img src="images/image4.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >Create a Vintage Photograph in Photoshop</a></h2>  
        <p>Quisque sed orci ut lacus viverra interdum ornare sed est. Donec porta, erat eu pretium luctus, leo augue sodales....<a href="#" >read more</a></p>  
        </div>  
    </div>  
</div>  

Updated Q here, with up to date attempt, still not solved

最新更新: 仍在尝试让图像拉入。我尝试了 Suni 的建议,但仍然无法让它们拉入滑块(他们最终出现在)

我在下面尝试过一些:

<?php
$i = 1;
foreach ($posts_array as $post) :  setup_postdata($post); 
<?php if ( has_post_thumbnail()) { $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >'; the_post_thumbnail('thumbnail'); echo '</a>'; } ?>
?> 


<?php
$i = 1;
foreach ($posts_array as $post) :  setup_postdata($post); 
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');    
?> 


 <?php
    $i = 1;
    foreach ($posts_array as $post) :  setup_postdata($post); 
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); <a  href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >  
?> 

    <?php
        $i = 1;
        foreach ($posts_array as $post) :  setup_postdata($post); ?> 

<?php if ( has_post_thumbnail()) { $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >'; the_post_thumbnail('thumbnail'); echo '</a>'; } ?>

【问题讨论】:

  • 虽然你认为它是基本的,但它比你想象的要多。
  • 几个小时已经证明了这一点!但是,建议?
  • 试试这个开始。 codex.wordpress.org/Writing_a_Plugin
  • 您需要一个自定义循环。 codex.wordpress.org/The_Loop
  • 你能提供一个演示链接吗?你在哪里工作?以便我们可以帮助您

标签: php jquery wordpress


【解决方案1】:

Hi, you can user get_posts to fetch posts.

下面是代码..未测试..

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme'

 */

//get_header(); ?>        
<div id="content"> 

<?php if (have_posts()) : ?>

<!--Your slider code goes here-->

<?php 
$args = array(
              'numberposts'     => 5,
              'orderby'         => 'post_date',
              'order'           => 'DESC'
);
$posts_array = get_posts( $args ); 
?>       

<div id="featured" >  
<ul class="ui-tabs-nav">

<?php
$i = 1;
foreach ($posts_array as $post) :  setup_postdata($post); 
?> 

<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-<?php echo $i; ?>">
  <a href="#fragment-1"><img src="" alt="" style="display:none;"/>
    <span>
      <?php the_title(); ?><br />
      <p class="info" style="padding-left:10px;"><?php the_excerpt(); ?></p>
    </span>
  </a>
</li> 
<?php $i++; 
endforeach; ?>
</ul>
<?php
$i = 1;
foreach ($posts_array as $post) :  setup_postdata($post); 
?>

<!-- First Content -->  
<div id="fragment-<?php echo $i; ?>" class="ui-tabs-panel" style="">
  <img src="<?php the_post_thumbnail('slider_image'); ?>" alt="" />  
  <div class="info">
    <h2><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h2>
    <p><?php the_excerpt(); ?><a href="<?php the_permalink(); ?>" >read more</a></p>  
  </div>  
</div>  
<?php $i++; endforeach; ?>


</div>  

<!--Your slider code goes here-->
<!-- End Featured Lists Image Slider -->        

<?php endif; ?>

【讨论】:

  • 我测试了这个解决方案,它在我可以测试的范围内对我有用,也就是说,我无法测试 javascript 部分。唯一的小错误是第一个列表没有结束 ul 标记。我会编辑它,但我还没有编辑权限。
  • 真棒苏尼尔 - 谢谢!关于获取“featured_image”来填充滑块的任何建议?
  • 嗨,Cam,您正在使用 the_post_thumbnail $large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large'); codex.wordpress.org/Function_Reference/the_post_thumbnail 让我知道它是否清楚..
  • 谢谢苏尼尔;所以添加这个: '; the_post_thumbnail('缩略图');回声''; } ?>
  • 我已经阅读了它的状态以在循环中添加/或者我应该在滑块中添加?
【解决方案2】:

这就是我的做法。

您需要对要使用的帖子进行两次循环 - 一个用于创建选项卡,一个用于创建内容。为避免执行两个数据库查询,请将一个查询的帖子缓存在一个数组中,并在该数组上使用 foreach 循环。

使用自定义查询获取要在滑块中使用的帖子对象:http://codex.wordpress.org/Class_Reference/WP_Query

免责声明:这不会逐字执行,但应该为您提供所需的框架

<?php

$post_data = array();

// Use a custom query to get the posts you want for your slider
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

    $post_data[] = $post;

endwhile;

// Reset Post Data
wp_reset_postdata();

?>

<div id="featured" >

    <ul class="ui-tabs-nav">  

    <?php 
    foreach($post_data as $key => $post){

      // Out put the markup + data from the $post object that you need for your tabs

    } 
    ?>

    </ul>

    <?php 
    foreach($post_data as $key => $post){

      // Out put the markup + data from the $post object that you need for your slider fragments

    } 
    ?>

</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多