I would like to build a one page website upon the Wordpress CMS. I'm looking for a way too hook my pages on a static frontpage by id, since I have several page types.
I've tried:
$id=6;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
But this returns only the content and not the content with the HTML. Any tips on how to achieve this? or other ways to build a one-page website upon Wordpress?
Thanks in advance
To output the HTML properly, try using PHP's HTML entity encoding/decoding related functions to convert entities back to their applicable characters (e.g. < and >).
WordPress has an internal function called wpautop that converts double linefeeds (\n) into paragraph splits. wp_texturize (or something similarly named) converts some characters to proper variants, such as quote marks and similar.
Try this:
$content = apply_filters( 'the_content', wpautop( wp_texturize( html_entity_decode( $post -> post_content ) ) ) );
// Note: not properly tested, but should probably output properly "HTMLized" contents.
If you want to output something else than the HTML tags that are inside the post contents (and inserted using the post editor in wp-admin), you'll have to work with the theme templates.
EDIT:
To insert whole templates into the homepage to create a single-page website, you'll need to create each "page" as a template file. Assuming you have front-page.php, content-about.php, content-portfolio.php and so on. Additionally you most probably have header.php and footer.php.
In simplified form, each of the content templates that would be inserted to front-end.php could look something like this:
<?php
/**
* content-about.php
*/
// Get the wanted content as WP Post.
$page_obj = get_page_by_title( 'About' );
$page_content = wpautop( $page_obj -> post_content );
?>
<article id="about"> <!-- Begin a new section for the one-page template. -->
<h2><?php echo $page_obj -> post_title; ?></h2>
<?php echo $page_content; ?>
</article>
Additional templates (content-portfolio.php, content-contact.php and so on) should follow a similar structure.
Then in front-page.php you need to include your header.php and footer.php as you normally would. Then inbetween use get_template_part calls to fetch and display the content-abc.php templates within front-page.php:
<?php
/**
* front-page.php
*/
get_header(); ?>
<!-- Add some markup if you want to. -->
<?php get_template_part( 'content', 'about' ); // 'content-about.php' ?>
<?php get_template_part( 'content', 'portfolio' ); // 'content-portfolio.php' ?>
<?php get_template_part( 'awesometemplate' ); // 'awesometemplate.php' ?>
<!-- Add some markup if you want to. -->
<?php get_footer();
Now after WordPress and PHP parse the front-page.php template, the resulting output might look like this (depending on what you insert into each included template):
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<!-- header.php visible contents here -->
<!-- content-about.php: -->
<article id="about">
<h2>About</h2>
<p>Well hello there! This is some <em>nice</em> content from content-about.php.</p>
</article>
<!-- content-portfolio.php: -->
<article id="portfolio">
<h2>Portfolio</h2>
<ul>
<li>
...
</li>
</ul>
</article>
<!-- awesometemplate.php: -->
<article id="awesome">
<h2>Awesome!</h2>
<table>
...
</table>
</article>
<!-- footer.php visible contents here -->
</body>
</html>
Now you've got separated templates that hold content and are embedded into front-page.php to create a single-page master template. The it's up to you to use CSS and JS to make it flashy if you want to.
Note: you can also use PHP's very own include or require functions to insert the sub-templates to front-page.php.
You can also use WP_Query to construct a custom query. You can use the Post and Page parameters to call your pages you need to display on your front page.
Inside the loop you'll have the ability to directly work with the template tags like the_content. You can also load template tags and HTML structures conditionally inside the loop on a per page basis with the use of is_page() or is_page_template()
Here is an example from the codec. Modify to suite your needs
$the_query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Related
I'm using Wordpress latest version, a child theme and Visual Composer WPBakery Page Builder.
I'm getting my blog posts listed into a template php file using WP_Query and in that page I need to:
display the Featured Image (which has caption) - DONE,
the Post Date - DONE,
and just a text block from the Post Content - a specific Row with a Column with a Text Block inside. This Text Block has a class name: bblog-text-row.
I need to echo/display/show just that text, but when I do the_content (); it outputs the VC shortcodes in a row as a raw text with shortcodes and the text. I also need to limit the text for this to 250 chars.
Here is the code I have just for the content part:
<?php
$query = new WP_Query( array( 'post_type' => 'post' ) );
$posts = $query->posts;
foreach($posts as $post) {
?>
<p>
<?php
$char_limit = 250; //character limit
$content = $post->post_content; //contents saved in a variable
echo substr(strip_tags($content), 0, $char_limit);
?>
</p>
<?php
}
?>
Thank you in advanced.
H
I found a small solution to my issue with getting the post content made with WPBakery Page Builder into a php template file listing all posts - like a blog page. Probably there might be clever and better ways to do it, but this solved my problem.
<?php
$the_query = new WP_Query( array( 'post_type' => 'post' ) );
while ($the_query->have_posts() ) {
$the_query->the_post(); ?>
<!-- you may add more post content here
I just want to refer to the content, not meta content or custom fields -->
<p class="bowe-post-content-bloc">
<?php
$conteudo = get_the_content(); //get ALL content for the post
$padrao = array('(\[vc(.?)\])', '(\[/vc(.?)\])','(\[booom_baannng_anypostsharebuttons])'); //create a filter
$reparacao = preg_replace ($padrao, "",$conteudo ); //remove content based on filter
echo wp_trim_words ( $reparacao, 40, '...'); //display only 40 words
?>
</p>
<!-- you may add more post content here
I just want to refer to the content, not meta content or custom fields -->
<?php
}
wp_reset_postdata(); ?>
I want to extract the images contained in a gallery block while maintaining their correct order. Since get_children() and get_attached_media() do not seem to register when the image order is changed in wp-admin, I'm trying to use get_post_gallery() instead.
My problem is that the function returns false, even though the post does have a gallery.
I tried both the example and the plain usage from Codex. Currently, my entire single.php looks like this:
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if ( get_post_gallery() ) :
echo get_post_gallery();
else :
echo (the_ID() . " has no gallery.");
endif;
endwhile;
endif;
?>
… which results in "ID has no gallery" every time.
However, the output of print_r($post->post_content); does include the following, which seems to confirm that there is in a fact a gallery:
<!-- wp:gallery {"ids":[80,81,82]} -->
<figure class="wp-block-gallery columns-3 is-cropped">
<ul class ="blocks-gallery-grid">
<!-- … -->
I'm also attaching a screenshot from wp-admin to make sure I don't misunderstand what constitutes a gallery.
get_post_gallery() works only for native galleries which have been created in classig WYSIWYG editor. You can find more about it there. Hovewer, if you create Gallery via Gutenberg, it create the whole HTML code instead of classic editor gallery which create shortcode like this: [gallery ids="400097,400052,400051"] . Functions get_post_gallery(), get_post_galleries() and get_post_gallery_images() works only on classic galleries added via native shortcode.
Are you only trying to display the gallery block as opposed to all content? If so, maybe try this?
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if (has_block('gallery', $post->post_content)) {
echo 'yes, there is a gallery';
$post_blocks = parse_blocks($post->post_content);
foreach ($post_blocks as $post_block){
if ($post_block['blockName'] == 'core/gallery'){
echo do_shortcode( $post_block['innerHTML'] );
}
}
}
// if there is not a gallery block do this
else {
echo 'no gallery';
}
endwhile;
endif;
get_footer();
?>
I got some inspiration from reading this post btw.
I have got the problem that my WordPress theme ("Sydney") is displaying the search results wrong. Instead of just listing posts I want to display WooCommerce products only.
Those should be ordered in a nice grid as shown in this picture:
.
At the moment the results are listed like this
.
How can I change the way the search results are displayed?
At the moment my search.php is looking like this:
<?php get_header(); ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="post-wrap" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h3><?php printf( __( 'Search Results for: %s', 'sydney' ), '<span>' . get_search_query() . '</span>' ); ?></h31>
</h3>
</header><!-- .page-header -->
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called content-search.php and that will be used instead.
*/
get_template_part( 'content', 'search' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
Thank you guys a lot!
** UPDATE
The solution provided by kashalo partly worked, but search results still look slightly different from the product page and are listed in only one column instead of a grid.
** UPDATE
The solution suggested by Alexander Wigmore looks almost the way I wanted it so look like. The only Problem with copying the page.phpin the search.phpis that the products are still getting displayed wheird, but not when they are displayed in the category they fit in. For example: When searching for saat the results are displaying the products at first with text only, but normaly under the Saatgut category.
my advise is not to directly modify the search file of wp or theme template but is a good practice to work with the child theme. For the design display of the result you can play a little with css to customize it as you want, instead for the results to show only the products you can use any filter builded on the theme you are using (if it have) or create yourself a function to filter the results (also called hooks).
An example for filtering results is like the above code:
add_action( 'pre_get_posts', 'filter_woocommerce_products' ); // the hook
function filter_woocommerce_products( $query ) { // the function
if( ! is_admin() && is_search() && $query->is_main_query() ) {
// verify if can use the search function and the search string is from the search call
$query->set( 'post_type', 'product' ); // filter the posts with type products that corrispond to woocommerce products.
}
}
and this function must be saved on the function.php file of your theme or the child theme.
For more information i suggest you to read more about child theme and the pre_get_posts documentation on the wp page.
Hope it helps and feel free to ask.
You'll want to modify the way search is handled by Wordpress.
This can easily be done by adding an "action", add the below code to your functions.php file.
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('product'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
This will then make the search page only show products. It's worth noting that you can add other post types back in via the array in this line: $query->set('post_type',array('product'));.
What is best practice for making a function call on a wordpress page?
For instance if you want to call my_special_function(); on the home page, where is the proper place to put the function call (ie home-page.php, /template-parts/content-page.php etc.).
<?php if( function_exists( my_special_function ) ) {
my_special_function();
} ?>
FYI Im using Underscores theme.
I've seen a few comments regarding shortcode. Would something like the below WP page template with the shortcode inserted be a best practice over just calling the function on that page?
So if I wanted the function to be called in the page template below I would just insert the shortcode wherever I want it on that page template or just is just calling the function is sufficient or best practice
<?php
/**
* Template Name: Home Page
*
* The template for displaying the home page.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Rollins_Ridge
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
// insert Shortcode
<?php echo do_shortcode('[special_function_shortcode]') ;?>
// or just call the function
my_special_function();
// or something else im not aware of
code here;
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
Don't edit themes core files.
the proper way is using functions.php file in child theme directory.
you can add your function in this file and create shortocde using wordpress hook
like
my_special_function(){
//Your content goes here.
echo "This is proper way";
}
add_shortcode('special_function_shortcode','my_special_function');
and use following shortocde anywhere on site.
in wordpress page use [special_function_shortcode]
and in php use
<?php echo do_shortcode('[special_function_shortcode]') ;?>
I am having some problems trying to get a custom page to display on my site. What I want is to create a page where the client can edit the content in Wordpress. When I add the page using the Wordpress "add new page" it doesn't show, but if I create a page in .php with the same content, it works fine. This is what I did:
First I created a blank .php template called lefthome.php using the following code:
<?php
/*
Template Name: Left Template
*/
?>
<?php get_template_part( 'loop', 'page' ); ?>
I then went into Wordpress, clicked on pages > add new. I gave my page a title and added the content I wanted to appear on the page. Then from the template option in the page attributes I choose the template I had earlier created (Left Template) and clicked update.
I wanted this template to appear on the homepage, so I tried to add it to the homepage template I had created using the following code:
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php include (TEMPLATEPATH . '/lefthome.php'); ?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>
The header, slider.php and footer all show fine. The contents of the lefthome.php do not appear. I do not know how to get it to show. The only way I have got it to show is to paste the contents into the lefthome.php template. I don't want to do this though, as the client will not be able to edit the contents themselves.
I hope someone can help me.
Thanks
You won't be able to display the content of a page just by calling directly its template file. You need to query the page itself. In that matter it won't matter what template you've already assigned to it anymore.
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php
global $post;
$page_id =5; // 5 is the id of the page we want to present with Left Template.
$post = get_post( $page_id );
setup_postdata( $post );
include locate_template( 'lefthome.php' );
wp_reset_postdata(); // we are done with that. reset the query so the rest don't mess up
?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>