【问题标题】:Wordpress Post Pages With Custom HTML includes depending on Category具有自定义 HTML 的 Wordpress 帖子页面包括取决于类别
【发布时间】:2015-09-27 19:13:30
【问题描述】:

我整天都在努力让它工作。我想根据帖子在 Wordpress 中的类别从单独的目录添加一些自定义 HTML,以保持干净。我似乎无法加载 html 文件。

相关代码为:

        <div class="container">
    <?php while ( have_posts() ) : the_post(); ?>
        <div class="row clearfix">  
            <?php if (function_exists('breadcrumbs')) breadcrumbs(); ?>
            <div class="title clearfix">
                <a href="javascript:window.history.back();" class="back-list"></a>
            <?php global $post; $category = get_the_category($post->ID); setPostViews($post->ID);  ?>
                <h1><?php echo $category[0]->name;?></h1>
            </div>
            <div class="detailBlock clearfix">
                <h1><?php the_title()?></h1>
                <div class="detail-inner">
                <?php $featured_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full', true);
                    if($featured_img) {
                        echo "<div class='detail-img'><img src='$featured_img[0]' alt='".get_the_title()."'></div>";
                        echo $category[0]->ID ;
                    }
                    the_content(); 

                ?>
                <?php 
                /* TEST CODE */
                 global $post; $category = get_the_category($post->ID);
                 if($category[0] == '6'){
                 include( get_template_directory_uri() '/call-to-actions/cta-weight-loss-fem.html');
                 }
                            ?>

                </div>


        </div>
        <?php endwhile;?>
    </div>

【问题讨论】:

标签: php html wordpress


【解决方案1】:

变化:

if($category[0] == '6'){

if($category[0]->ID == '6'){

这应该可行。

注意:您不需要上面的行来获取类别,因为您之前已经拥有它。更好的办法是将值存储为变量。

【讨论】:

  • 非常感谢您的快速回复!另一个问题是 include 语句。我无法让它正常工作。我不想将我所有的 HTML 文件放在同一个文件夹中,因为这会造成混乱。我希望能够从他们自己的文件夹中包含它们。但是,我尝试使用 Wordpress 的 get_template_directory_uri() 的方法在 include 语句中无法正常工作。我尝试了很多方法,大多数示例都在脚本标签内。不是以这种特定的方式,我需要它来工作。提前感谢大家的帮助,你们都是超级明星!
  • 您可以使用一些包含路径 - 另请查看 get_stylesheet_directory() 如果使用子主题,您将需要它。如果有疑问 varm_dump 或回显从函数中获得的值并检查路径的其余部分是否正确。如果一切都失败了,只需放入相对路径 '/wp-content/themes/....' 等。
  • 非常感谢西蒙
  • 别担心,格伦 - 如果你能将我的回答标记为正确,那将非常感谢 - 如果你需要任何进一步的帮助,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 2018-08-07
  • 2013-10-10
相关资源
最近更新 更多