【问题标题】:PHP statements, HTML and RSSPHP 语句、HTML 和 RSS
【发布时间】:2010-05-01 00:50:58
【问题描述】:

好的,我还有一小段代码正在努力解决。我正在构建一个有条件的侧边栏。目标是仅在查看“博客”类别中的帖子时显示与博客相关的内容。我已经完成了部分工作,但我试图将类别的 RSS 提要引入侧边栏中以显示为最近的帖子的部分。它不起作用,因为我是一个 php 新手,我不完全确定为什么。非常感谢任何建议或指示。我会先发布问题部分,然后再发布整个 php 文件,这样大家就可以看到我遇到问题的部分的上下文。

问题部分:

echo '<div class="panel iq-news">';
                echo '<h4><span><a href="/category/blog/feed"><img src="/wp-content/themes/iq/images/rss-icon.gif" alt="Subscribe to our feed"/></a></span>IQNavigator Blog</h4>';
    <?php
    query_posts('category_name=Blog&showposts=2');
    if (have_posts()) : ?>      
                echo '<ul>';
        <?php while (have_posts()) : the_post(); ?>
                echo '<li><a href="<?php the_permalink();?>"><?php the_title();?> </a></li>';
        <?php endwhile;?>
                echo '</ul>';
    <?php endif;?>
                echo '<div class="twitter">';
                echo '<p id="twitter-updates">';
            <?php twitter_updates();?>
                echo '</p>';
                echo '<p class="text-center"><a href="http://twitter.com/iqnavigator">Follow us on twitter</a></p>';
                echo '</div>';

                echo '</div>';

整个该死的长声明,出于上下文原因:

        <div class="sidebar">
        <?php
        if (!is_search() && !is_page('Our Clients') && !is_archive()){
            if($post->post_parent) {
                $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1&exclude=85,87,89,181,97,184");
            }
            else {
                $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1&exclude=85,87,89,181,97,184");
            }
            if ($children) { ?>
        <div class="panel links subnav">
            <h3>In This Section</h3>
            <ul class="subnav">
                <?php echo $children; ?>
            </ul>
            <p>&nbsp;</p>
        </div>
        <?php 
            }
        }
        if(is_page('Our Clients') || in_category('Our Clients') || is_category('Our Clients'))
        {
            echo '<div class="panel links subnav">';
            echo '<h3>In This Section</h3>';
            echo '<ul class="subnav">';
            wp_list_categories('child_of=21&title_li=');
            echo '</ul>';
            echo '<p>&nbsp;</p>';
            echo '</div>';

        }

        else if  (in_category('Blog'))
                {
                                    //PUT YOUR CODE HERE                        
                                    // echo get_page_content(34);                                            
                echo '<div class="panel featured-resource">';
                echo '<h4>Blog Contributors</h4>';
                echo '<ul class"subnav">';
                echo '<li><a href="/company/executive-team/john-f-martin/">John Martin</a></li>';
                echo '<li><a href="/company/executive-team/kieran-brady/">Kieran Brady</a></li>';
                echo '<li><a href="/company/executive-team/art-knapp/">Art Knapp</a></li>';
                echo '</ul>';
                echo '</div>';

                echo '<div class="panel iq-news">';
                echo '<h4><span><a href="/category/blog/feed"><img src="/wp-content/themes/iq/images/rss-icon.gif" alt="Subscribe to our feed"/></a></span>IQNavigator Blog</h4>';
    <?php
    query_posts('category_name=Blog&showposts=2');
    if (have_posts()) : ?>      
                echo '<ul>';
        <?php while (have_posts()) : the_post(); ?>
                echo '<li><a href="<?php the_permalink();?>"><?php the_title();?> </a></li>';
        <?php endwhile;?>
                echo '</ul>';
    <?php endif;?>
                echo '<div class="twitter">';
                echo '<p id="twitter-updates">';
            <?php twitter_updates();?>
                echo '</p>';
                echo '<p class="text-center"><a href="http://twitter.com/iqnavigator">Follow us on twitter</a></p>';
                echo '</div>';

                echo '</div>';


                                    //END CODE HERE                 

}

        if (!is_page('Resources'))
                    {


        ?>
        <div class="panel featured-resource">
            <h4>Featured Resource</h4>
            <div class="embed">
                <?php
                $custom_fields = get_post_custom();
                $featured_video_code = $custom_fields['Featured Video Code'];
                if($featured_video_code)
                {
                    foreach ( $featured_video_code as $key => $value )
                    {
                        $the_code = $value;
                    }
                    $featured_video_link = $custom_fields['Featured Video Link'];
                    foreach ( $featured_video_link as $key => $value )
                    {
                       $the_link = $value;
                    }   
                    $featured_video_text = $custom_fields['Featured Video Text'];
                    foreach ( $featured_video_text as $key => $value )
                    {
                        $the_text = $value;
                    }
                    if($the_code)
                    {
                        echo $the_code;
                    }
                    if($the_text)
                    {
                        echo '<ul>';
                        echo '<li>';
                        if($the_link)
                        {
                            echo '<a href="' . $the_link . '" class="video" target="_blank">' . $the_text . '</a>';
                        }
                        else 
                        {
                            echo $the_text;

                                                                                    }
                        echo '</li>';
                        echo '</ul>';

                    }
                }

                ?>  


+ Visit Resource Center

        <div class="clr"></div>
        <div class="blue-bars">
            <a href="<?php bloginfo('template_directory');?>/more-info.php" class="more-info" rel="facebox">Request More Info</a>
            <a href="<?php bloginfo('template_directory');?>/resource-form.php?id=701000000009E" class="view-demos" rel="facebox">Schedule a Demo</a>
        </div>
    </div>      
    <div id="content">

【问题讨论】:

    标签: php html wordpress rss


    【解决方案1】:

    Feed 到底有什么不适合的?如果是 URL,试试这个(使用http://codex.wordpress.org/Template_Tags/bloginfo):

    echo '<h4><span><a href="<?php bloginfo('url'); ?>/category/blog/feed/">
    <img src="/wp-content/themes/iq/images/rss-icon.gif" alt="Subscribe to our feed"/></a>
    

    如果您尝试在侧边栏中列出帖子,最好使用新查询,它可以多次使用(在侧边栏、页面或帖子中)并且不会与主 WP 循环冲突,即:

      <?php $my_query = new WP_Query('category_name=mycategory&showposts=2'); ?>
      <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
      <?php the_title(); ?></a><?php endwhile; ?>
    

    【讨论】:

    • 这比我使用的要好得多,也更简洁。我要问一个(可能)愚蠢的后续问题:我可以调用 $my_query 任何东西还是需要像 $query_posts 这样特定的东西?
    • 无视;我想出了那部分...... :)
    • 到底出了什么问题?那行得通吗?如果是,请接受答案:stackoverflow.com/faq
    • 确实有效;我最初不知道如何接受答案,但现在我知道了。谢谢!
    猜你喜欢
    • 2016-06-11
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多