<?php get_sidebar(); ?>,将取代 with the 'get_template_part' cause any problems?)

作者探讨了在WordPress主题中使用get_sidebar()的传统方法与创建自定义模板文件(如sidebar-blog.php等)进行组织的优劣,询问是否会导致潜在问题,以及遵循哪种方式更为长久。
摘要由CSDN通过智能技术生成

将取代 with the 'get_template_part' cause any problems?)

我知道在WordPress主题中直接调用侧边栏的方法是通过使用<?php get_sidebar(); ?> <?php get_sidebar(); ?> , sidebar.php文件保存在主题的根目录下。

为了改善组织,我创建了一个名为“Sidebar”的文件夹,然后在该文件夹中为每个Sidebar创建了一系列模板。

然后我在主题中称这些为:

任何人都可以告诉我,如果我的方法工作,如果它会导致我在以后的任何问题,因此,应坚持<?php get_sidebar(); ?> <?php get_sidebar(); ?>

I know that the straightforward way of calling a Sidebar, within the WordPress Theme, is through the use of <?php get_sidebar(); ?> with the sidebar.php file being saved within the Theme's root.

In the interest of improved organisation, I have created a folder entitled 'Sidebars' and then within this folder, created a series of templates for each Sidebar.

I have then called these within the theme as follows:

Can anyone tell me if whilst my approach works, if it would cause me any problems at a later date and as such, should stick with <?php get_sidebar(); ?>

原文:https://stackoverflow.com/questions/43221577

更新时间:2020-01-14 12:44

最满意答案

你绝对可以使用它。 get_sidebar()函数更传统。 它可以用来为你的模板调用特定的侧边栏。 即:你有这些边栏(作为文件):

sidebar-blog.php, sidebar-home.php, sidebar-page.php, sidebar-with_a_lot_of_ads.php...

你可以分别给他们打电话

get_sidebar('blog'), get_sidebar('home'), get_sidebar('page'), get_sidebar('with_a_lot_of_ads')...

但更重要的是,如果其他人必须维护你的代码,最好使用约定......

You can absolutely use that. The get_sidebar() function is just more conventional. And it can be used to call specific sidebars for your template. ie : You've got these sidebars (as files) :

sidebar-blog.php, sidebar-home.php, sidebar-page.php, sidebar-with_a_lot_of_ads.php...

you can call them respectively with

get_sidebar('blog'), get_sidebar('home'), get_sidebar('page'), get_sidebar('with_a_lot_of_ads')...

But more important, if someone else has to maintain your code, it's better to use conventions...

2017-04-05

相关问答

你的文章的html结构非常糟糕。 你已经将你的article元素包装在strong元素中,这没有意义,因为它用于使文本更大胆。 无论如何,你可以将这个小片段添加到你的wp-content/themes/your-theme/style.css article[class*="post"] { /* only post articles */

float: left;

width: 65%;

}

编辑 仅适用于特定页面: .page-id-231 article[class*="p

...

请参阅此处的代码http://codex.wordpress.org/Function_Reference/get_template_part 只是 如果您在模板中添加“samplename.php”文件 你可以像这样得到这个文件 get_template_part( "samplename" ); Refer code here http://codex.wordpress.org/Function_Reference/get_template_part Simply if you add "s

...

只需更换: <?php get_template_part( 'content', get_post_format() ); ?>

附: <?php the_content(); ?>

前者正在寻找诸如content-status.php或content-aside.php之类的东西,或者最有可能的情况是,在主题根目录中存在普通的旧“post” content.php 。 Simply replace: <?php get_template_part( 'content

...

那么,你只需要将你的变量声明为global变量: global $table_name;

$table_name = "CPEL Implementation";

如果你想在另一个模板中使用它: global $table_name;

echo $table_name;

Well, you simply have to declare your variable as global : global $table_name;

$table_name = "CPEL Implementation

...

我通过在sidebar-blog.php下创建一个全新的侧边栏并将我的模板重定向到它来修复它。 I fixed it by creating a brand new sidebar under sidebar-blog.php and redirecting my templates to it.

您是按名称引用侧边栏,而是通过ID获取它: <?php get_sidebar('sidebar-widget'); ?>

You're referencing your sidebar by name, instead you should get it via id : <?php get_sidebar('sidebar-widget'); ?>

你绝对可以使用它。 get_sidebar()函数更传统。 它可以用来为你的模板调用特定的侧边栏。 即:你有这些边栏(作为文件): sidebar-blog.php, sidebar-home.php, sidebar-page.php, sidebar-with_a_lot_of_ads.php...

你可以分别给他们打电话 get_sidebar('blog'), get_sidebar('home'), get_sidebar('page'), get_sidebar('with_a_lo

...

如果您的get_template_part()不起作用,那么问题可能是主题中没有命名为page.php的文件,请检查此文件。 I ended up copying the default page.php template as page-page.php and loaded it like this: $postName = (locate_template('page-' . $post->post_name . '.php') == '') ? 'page' : $post->post_n

...

您可以通过执行自己的php功能并以您需要的目标为目标来执行此操作 <?php

if ( is_home() ) :

get_sidebar( 'home' );

elseif ( is_portfolio page1() ) :

get_sidebar( 'portfolio page sidebar' );

else :

get_sidebar();

endif;

?>

为您将拥有的页面和侧边栏组合添加更多elseif

...

是的,这是完全合法的。 如您所知,除index.php和style.css之外的所有内容都是可选的。 已经很好地解决了https://wordpress.stackexchange.com/questions/16554/when-is-get-template-part-preferable-to-simply-using-the-template-php-files 。 Yes, it's perfectly legitimate. As you know, everything but in

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值