Large HTML render/output from a WordPress shortcode - php

I am making custom WP plugin which will have shortcode. This shortcode will have attribute of category slug and according to category slug it will fetch post and show first 5 of them with their featured images into a carousel or custom slider. The output will have a large HTML fragment.
Here the very basic code
function posts_from_category( $atts ) {
ob_start();
global $posts;
$args = array('category_name' => $atts['category_slug'] );
$posts = get_posts( $args );
//Here either I want to include a php file as template and pass the $posts some how which I am not able to do or make string concatenated HTML fragment or heredoc.
return ob_get_clean();
}
add_shortcode("posts_from_category", 'posts_from_category');
I want to use it as
[posts_from_category category_slug="services"]
Please help me how it can be done at best as I am new to php and WP.

Related

Display a specific Text block from all posts

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(); ?>

How to copy title of WP post dynamically to focus keyword field in Yoast SEO?

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_focusk‌​w', $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.

Displaying content of Advanced Custom Fields

I’m having a little problem understanding what (my) PHP is doing when I’m trying to display different Custom Posts and Advanced Custom fields on the same page.
I have added different Advanced Custom Fields to a page, and I have custom posts that I am trying to display using the template.
I’m calling my custom fields throughout the template using:
<?php the_field(‘field-name’) ?>
My custom posts are called through loops like this (somewhere around the middle of the template):
<?php
$args = array(
'post_type' => ‘foo’
);
$foo = new WP_Query( $args );
if( $foo->have_posts() ) {
while( $foo->have_posts() ) {
$foo->the_post();
?>
<?php the_content() ?>
<?php
}
}
else {
// SOME MESSAGE
}
?>
The content of the Advanced Custom Fields is showing fine, above those loops. Below the loops it just doesn’t display.
I can’t figure out why the content is not showing up.
I assume it has to do with the while or if statements of the loops. If I remove the loops, the content of any Advanced Custom Field below does display.
When you use WP_Query(), you are changing the default $post variable on the page each time you loop through a post. You need to call wp_reset_postdata() after your loop to reset that $post variable so it corresponds to the current page again. You can call the function after your 'while' loop -
<?php
$args = array(
'post_type' => ‘foo’
);
$foo = new WP_Query( $args );
if( $foo->have_posts() ) {
while( $foo->have_posts() ) { $foo->the_post();
the_content();
} wp_reset_postdata();
}
else {
// SOME MESSAGE
}
?>

Pulling data for WordPress Jetpack plugin contact form

I have a small form on my website, which I created using Jetpack plugin (it has built-in contact form creator).
I want to pull options for select input from my custom-post-type "artist" (using the titles of the posts). Is there a way to do it?
[contact-field label='Artist' type='select' required='1' options='i want my titles go here separated by commas plus "other" option'/]
The code for the field looks like this. I believe I need to do some php+jquery stuff in this page template, but I can't seem to get it.
With some creativity, yes, it's possible :)
We create another Shortcode to create a "virtual" JetPack shortcode.
I tested this using the default post post_type.
add_shortcode( 'my-jet-form', 'so_14003883_jet_form' );
function so_14003883_jet_form( $atts, $content )
{
// Query our post type, change accordingly
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'post_status' => 'publish'
) );
// Build an array of post titles
$titles = array();
foreach( $posts as $post )
{
$titles[] = $post->post_title;
}
// Convert array into comma sepparated string
$posts_select = implode( ',', $titles );
// Make JetPack shortcode
$return = do_shortcode( '[contact-form][contact-field label="Name" type="name" required="1"/][contact-field label="Artist" type="select" options="' . $posts_select . '"/][/contact-form]' );
return $return;
}
Usage:
adjust the desired post type
adjust the do_shortcode part to suit your original shortcode
put the shortcode [my-jet-form] wherever you want
voilà

Wordpress: include content of one page in another

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.

Categories