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(); ?>
Related
I have around 1K posts in my blog. I use Yoast SEO Wordpress Plugin. All my posts doesn't has focus keyword added. Is there any way possible I can add title of the post dynamically to focus keyword field instead of copy paste one by one?
Yoast Focus Keywords is a post meta, you can create a loop that loops through all the posts and then update the post meta with get_the_title();
Sample code:
$title = get_the_title();
$args = array('posts_per_page' => -1, 'post_type' => 'post');
$posts = new WP_Query($args);
foreach($posts as $post){
update_post_meta($post->ID, '_yoast_wpseo_focuskw', $title);
}
wp_reset_postdata();
You can add the code inside your functions.php or create a page template with the code.
Update:
Create a blank page template that doesn't contain a loop, then add the below code into the file and create a page using this page template.
$posts_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => -1
)
);
while($posts_query->have_posts()) : $posts_query->the_post();
$title = get_the_title();
$post_id = get_the_ID();
update_post_meta($post_id, '_yoast_wpseo_focuskw', $title);
echo $title . ' Meta Updated<br />';
endwhile;
Visit the page using the page template and it should go through each post and display "TITLE Meta Updated" each time it goes through a post.
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();
I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );
In the website every product is a post, but when we add new products we want something like a newsletter, mostly like a post so in the sidebar of the home page you can see the new products or events of the month.
I'm using pages because I don't want to re-post a product on every new newsletter so I junt wanna display the posts inside the page.
In the products page I separate every product by category and sub-category but since I want to group specific post to publish them on the sidebar I think that pages was the best way to do it.
Right now I'm using this code:
<?php
$productos = new WP_Query(array(
'post__in'=> array(81, 83),
'orderby'=>'title',
'order'=>'ASC'
)
); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();
?>
It display the posts with the id of 81 and 83, I would like to show post by slug using 'name' as the codex says because is going to take some time to be checking the ids of the new post, instead of using the name of every new product but It doesn't work in array or I'm doing something wrong.
Now I will love to make something like this work
$names = get_post_meta($post->ID, "names", $single = true);
$productos = new WP_Query(array(
'name'=> array($names),
'orderby'=>'title',
'order'=>'ASC'
)
);
So every time I publish a new page I just write the slugs of the posts that I want to include in the page in a custom field, as you can see I'm not very good with php but I trying to learn and I search a lot for something that could work before asking in here.
I try the ggis inline post plugin and although it works I need the id for every post I want to include and I will need to edit the plugin because I want a different order in the output of the post thats why I don't like to depend to much on plugins.
Update:
So I'm now looking if I can make this using shortcodes, right now I have this:
function producto_func($atts) {
extract(shortcode_atts(array(
'nombre' => ''
), $atts));
global $post;
$pieza = get_page_by_title($nombre,OBJECT, 'post');
echo '<h1>'. $pieza->ID . '</h1>';
}
add_shortcode('producto', 'producto_func');
enter code here
So I just enter the shortcode [producto nombre="ff 244"] in the page and it show its ID, and I can add any number of shortcodes depending on the number of post I need.
But how can I show the entire content of the post.
Any idea?
I find I solution using Shortcodes.
So I put this on my functions.php page
function productos($atts, $content = null) {
extract(shortcode_atts(array(
"slug" => '',
"query" => ''
), $atts));
global $wp_query,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(array(
'name'=> $slug,
));
if(!empty($slug)){
$query .= '&name='.$slug;
}
if(!empty($query)){
$query .= $query;
}
$wp_query->query($query);
ob_start();
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content() ?></div>
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("producto", "productos");
And in my page template I just write [producto slug="MY-SLUG"] and that way I can display multiple post just with the slugs. Hope someone find this useful.
From the Wordpress Codex:
Display post by slug:
$query = new WP_Query( 'name=about-my-life' );
Display page by slug:
$query = new WP_Query( 'pagename=contact' );
UPDATE
Try changing this:
'name'=> array($names),
To this:
'name'=> $names,
The 'name' - and 'pagename' - parameter does not take in an array. Only a string. A comma delimited list SHOULD give you what you need from within your Custom Fields titled "names", though I haven't tested this approach.
Also, thank you for using WP_Query instead of query_posts.
How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.