最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

conditional content - A little stumped - first WordPress theme

programmeradmin1浏览0评论

I am going through an online Udemy course alongside it I'm building my own little website.

The website has 4 pages 1 of them a front page that I've created front-page.php and the rest of the pages will be using the page.php template but different content embedded in them.

My plan is to have the page.php call the relevant content based on the page-id for instance for a warranty page the code would look like this.

get_header();?>

get_template_part( 'template-parts/content', get_the_ID());

//getting the content from templates based on ID?>

Or

get_template_part( 'template-parts/content', get_the_title());

//And get the content based on the title of the page

so I guess my question is, is this the correct way of doing this or how else more efficient way would you go about doing this?

Cheers!!!

I am going through an online Udemy course alongside it I'm building my own little website.

The website has 4 pages 1 of them a front page that I've created front-page.php and the rest of the pages will be using the page.php template but different content embedded in them.

My plan is to have the page.php call the relevant content based on the page-id for instance for a warranty page the code would look like this.

get_header();?>

get_template_part( 'template-parts/content', get_the_ID());

//getting the content from templates based on ID?>

Or

get_template_part( 'template-parts/content', get_the_title());

//And get the content based on the title of the page

so I guess my question is, is this the correct way of doing this or how else more efficient way would you go about doing this?

Cheers!!!

Share Improve this question edited Jan 25, 2020 at 5:39 CommunityBot 1 asked Jan 25, 2020 at 4:12 thatrubynoobthatrubynoob 1 1
  • depends on the complexity and differences in content and structure of what you are trying to output per page. you also could work with individual files page-{slug}.php as per developer.wordpress/themes/template-files-section/… .. also depends on how much of the content should be editable from within the normal page text editor. – Michael Commented Jan 25, 2020 at 22:18
Add a comment  | 

1 Answer 1

Reset to default 0

Neither is correct. page.php is a generic template that will be used for any page, automatically. You don't need to manually get the correct page content, you just need to use the correct template tags and WordPress will automatically output the correct values for the current page.

<?php the_title(); ?>
<?php the_content(); ?>

WordPress templates are templates. You don't need a file for every piece of content, or to manually populate them with the correct content. You just need to define the structure of the content with HTML, and use the template tags to indicate where the page-specific content goes.

All that get_template_part( 'template-parts/content' ) does is include the template-parts/content.php template into the current template. This allows you to break down templates into smaller parts, to make them easier to manage. So you only need one template-parts/content.php file, and you would use the template tags I mentioned above in that.

发布评论

评论列表(0)

  1. 暂无评论