I m using the metabox plugin for wordpress
I want to get value from one or few metabox in my one page wordpress theme.
Here's how i get all my page
/set some variables
//get all the pages on your site
$contact = get_page_by_title( 'Contact' );
$pages = get_pages(array(
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'exclude' => $contact->ID,
) );
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
$pageid=$page_data->ID;
$couleur=rwmb_meta('twg_couleur');
//Put a container around every page's content
echo "<div class='container-fluid content $pageid' id='$slug'>";
echo "<div class='container'>";
//Heading and Content
echo "<h1>$title</h1>";
echo $content;
echo $couleur;
echo "</div>";
echo "</div>";
}
;
But my var doesn't appears.
She's on the single page if I call her but seems to be empty or not there on the one page template.
How i can do that ?
Thanks a lot for your help
OK !
I just use
$couleurs=rwmb_meta('twg_couleur','type=color' , $pageid);
AND
$pageid=$page_data->ID;
it's working
thanks to me :-)
Related
I'm currently trying to fetch all my pages from different post types and display them on a single page with a WP Query but I wanted to display them in such a template in the HTML instead of it all coming as a big list of course I'm not entirely sure if this is even possible
Parent
Child
Parent
Child
Parent
Parent
Parent
This is what I'm using so far
$args = array(
'post_type'=> 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$result = new WP_Query($args);
?>
<section>
<h2><?php echo $post_type; ?></h2>
<?php
while ($result->have_posts()) {
$result->the_post();
$post = get_post();
?>
<ul>
<li class="">
<?php echo the_title(); ?>
</li>
</ul>
<?php
}
wp_reset_postdata();
?>
</section>
<?php }
I tried using wp_list_pages but I'm not sure how to use that for different post types and also I would like to have limited posts per page not display everything
WP_Query is not the right tool for the job!
Wordpress has special functions for what you're looking for:
get_pagesDocs
wp_list_pagesDocs
"I tried using wp_list_pages but I'm not sure how to use that for different post types"
Well, that's why docs are here for!
For example, wp_list_pages accept an argument called post_type which lets you specify which "post type" you're interested in, and its default value is the wordpress "page" type. If you want to change it to your custom post type then you could use something like this:
$args = array(
'post_type' => 'your_custom_post_type'
);
wp_list_pages($args);
"also I would like to have limited posts per page not display everything"
Again, there is docs for that!
if you use get_pages for example, then there is an argument for limiting the number of pages shown on the page and that's called number. The number argument is an integer and represents the number of pages to return and its default is 0(all pages).
Using get_pages also you could specify your custom post type to query. The argument for that is called post_type and its default value is the wordpress "page" type.
$args = array(
'post_type' => 'your_custom_post_type',
'number' => 20
);
$your_pages = get_pages($args);
foreach ($your_pages as $page) {
echo $page->post_title;
}
If you really need/want to use WP_Qery then you could do it like this:
WP_Qery + get_pages
$args = array(
'post_type' => 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$results = new WP_Query($args);
if ($results) {
while ($results->have_posts()) {
$results->the_post();
echo "<h3>" . get_the_title() . "<h3>";
$sub_pages_args = array(
'child_of' => get_the_ID(),
);
$sub_pages = get_pages($sub_pages_args);
echo "<ul>";
foreach ($sub_pages as $sub_page) {
echo "<li>";
echo $sub_page->post_title;
echo "</li>";
}
echo "</ul>";
}
}
wp_reset_postdata();
Which will output this:
Parent Title
Child Title
Parent Title
Child Title
Parent Title
Parent Title
Parent Title
Fully tested and works on wordpress 5.8.
$pages = get_pages();
if ($pages) {
foreach ($pages as $page) {
if ($page->post_parent == 0) {
echo $page->post_title . ' — ' . $page->ID;
$sub_pages = get_pages(array('child_of' => $page->ID));
foreach ($sub_pages as $sub_page) {
echo ' — ' . $sub_page->post_title . ' — ' . $sub_page->ID;
}
}
}
}
I want to display Posts order by Last updated on the home page, how to right function for it and where to paste that function in wordpress?
Create your plugin and paste this function in plugin file.
function wpb_lastupdated_posts()
{
// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'
);
//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
$string .= '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
$string .= '<li> ' .get_the_title( $lastupdated_loop->post->ID ) . ' ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile;
$string .= '</ul>';
return $string;
wp_reset_postdata();
}
//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');
use this shortcode : [lastupdated-posts]
There are 3 ways to do this
add this code in the functions.php file
function shortcode_latest_homepage_posts(){
$lquery = new WP_Query(array('order' => 'DESC'));
if($lquery->have_posts()){
while($lquery->have_posts()){
$lquery->the_post();
the_title();
the_content();
}
}
wp_reset_postdata();
}
add_shortcode('latest_posts', 'shortcode_latest_homepage_posts');
and add simply this [latest_posts] shortcode in the page editor that you have assign for the front page in cms
OR
add this code in functions.php
function latest_homepage_posts(){
$lquery = new WP_Query(array('order' => 'DESC'));
if($lquery->have_posts()){
while($lquery->have_posts()){
$lquery->the_post();
the_title();
the_content();
}
}
wp_reset_postdata();
}
add_action('latest_post', 'latest_homepage_posts');
and add this code at the place where you want to show the post in the template that assign for or made for a home page like home.php or front-page.php
<?php do_action('latest_post');?>
OR
3. simply add this code at the place where you want to show the post in the template that assign for or made for a home page like home.php or front-page.php
<?php
$lquery = new WP_Query(array('order' => 'DESC'));
if($lquery->have_posts()){
while($lquery->have_posts()){
$lquery->the_post();
the_title();
the_content();
}
}
wp_reset_postdata();
?>
I'm working on a new project and my client needs a site with blog.
But I'm a terrible PHP programmer.. So I created the entire site on HTML/CSS and the blog with wordpress.
OK, sounds good! but how to put the "Recent posts" from the blog(wordpress) in my index html page?
Method 1 : wp_get_recent_posts()
According to WordPress codex: wp_get_recent_posts() will return a list of posts. Different from get_posts which returns an array of post objects.
<?php
include('blog/wp-load.php'); // Blog path
// Get the last 5 posts
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 5,
'post_type' => 'post',
'post_status' => 'publish'
));
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>';
?>
Method 2 : WordPress loop
<?php
define('WP_USE_THEMES', false);
include('blog/wp-load.php'); // Your blog path
//Get 5 posts
query_posts('showposts=5');
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', the_title(), '</li>';
}
echo '</ul>';
?>
What I'm trying to do is have a user select a page from a drop-down menu when writing a post or page in WordPress and then showing that selected page (user_selection) as an extra loop.
What I have is this and it's working fine when used in a theme:
<?php $user_selection = get_post_meta($wp_query->post->ID, 'user_selection', true); ?>
<?php
$id = $user_selection;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
But when used in a plugin and added to wp_head or wp_footer, then the ID won't get passed along. I've read that WordPress doesn't pass variables like that, so I'm (quite) a bit stuck.
Any help on how to get around this would be appreciated.
--------
Update. I got it to work by using:
<?php global $wp_query; $user_selection = get_post_meta($wp_query->post->ID, 'user_selection', true); ?>
<?php $id=$user_selection;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content; ?>
Everything is fine except that on posts that already exist, the slide-out page defaults to the current post/page (should default to none). When the post is saved, then it works, but that would mean re-saving all the posts, which would be nonsense :) What am I missing?
wp_dropdown_pages in the backend is entered like this:
<?php wp_dropdown_pages( array( 'name' => 'user_selection', 'id' => 'user_selection', 'selected' => $selected, 'show_option_none' => '-- no selection --', 'option_none_value' => '-1' ) ); ?>
Thanks!
----------
Update 2. I think I got it:
<?php global $wp_query; $user_selection = get_post_meta($wp_query->post->ID, 'user_selection', true); ?>
<?php $var = 0; if (empty($user_selection)) { ?>
NO SELECTION MADE
<?php } else { ?>
<?php
$id=$user_selection;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
<?php } ?>
Thanks.
In the form that passes the info, add a hidden input field, for example <input type='hidden' id='id' name='id' value='<php echo $post->ID;>?'/>. The you will be able to query the id.
I have an issue with a shortcode that I'm writing for wordpress. I'm trying to use the get_the_content() function but instead of pulling the contents of the custom post type I've created it's pulling the contents of the page the shortcode is sitting on. Other functions are all working fine e.g. get_the_title() and get_the_post_thumbnail(). I pass the ID in the functions and it works great for everything else, just not get_the_content.
get_the_content($testimonial_item->ID);
The shortcode contains pagination and other elements which all work correctly, It's just this one function that causing me grief. The full code is below, any help would be greatly appreciated.
function ncweb_display_testimonial_items($atts, $content = null) {
extract( shortcode_atts( array(
'per_page' => 6
), $atts ) );
/* pagination parameters */
// check what page we are on
if ( isset ($_GET['testimonial-page'] ) ) $page = $_GET['testimonial-page']; else $page = 1;
// default number of pages
$total_pages = 1;
// portfolio offset. Used in the get_posts() query to show only portfolio for the current page
$offset = $per_page * ($page-1);
// setup the portfolio args for retrieving the total number of portfolio items
$testimonial_count_args = array(
'post_type' => 'ncweb-testimonials',
'posts_per_page' => -1
);
$testimonial_count = count(get_posts($testimonial_count_args));
// calculate the total number of pages
$total_pages = ceil($testimonial_count/$per_page);
/* end pagination parameters */
// main image query
$testimonial_args = array(
'post_type' => 'ncweb-testimonials',
'numberposts' => $per_page,
'offset' => $offset
);
$testimonial_items = get_posts($testimonial_args);
// start our output buffer
ob_start();
if($testimonial_items) :
/*** main portfolio loop ***/
$counter = 1;
echo '<div class="testimonial-items" id="testimonial-items">';
foreach($testimonial_items as $testimonial_item) :
$testimonial_company = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_company', true);
$testimonial_client = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_client_name', true);
echo '<aside class="testimonial-list-item row">';
echo '<div class="col-xs-12 testimonial-list-item-info">';
echo '<div class="testimonial-image">'. get_the_post_thumbnail($testimonial_item->ID) .'</div>';
echo '<div class="testimonial-client"><span class="testimonial-client-name">'. $testimonial_client .'</span><br/><span class="testimonial-company">'. $testimonial_company .'</span></div>';
echo '</div>'; //end of testimonial-list-item-info
echo '<div class="col-xs-12 testimonial-item-content">'. get_the_content($testimonial_item->ID); .'</div>';
echo '</aside>';
$counter++;
endforeach;
echo '</div>';
/*** display pagination ***/
// pagination base
echo '<div id="testimonial-pagination">';
$base = get_permalink(get_the_ID()) . '%_%';
echo paginate_links( array(
'base' => $base,
'format' => '?testimonial-page=%#%',
'prev_text' => __('Previous', 'ncweb'),
'next_text' => __('Next', 'ncweb'),
'total' => $total_pages,
'current' => $page,
'end_size' => 1,
'mid_size' => 5
));
echo '</div>';
/*** end pagination display ***/
else :
echo '<p>' . __('No testimonial items found.', 'ncweb') . '</p>';
endif; // end if($images)
return ob_get_clean();
}
add_shortcode( 'testimonial-items', 'ncweb_display_testimonial_items' );
This is because the get_the_content() function is a wrapper and it's only arguments are the Read More text and the stripteaser argument.
What you actually want is.
$post= get_post($testimonial_item->ID);
$testimonial_items = $post->content ;
$testimonial_items = apply_filters('the_content', $testimonial_items);
Then you will have what you are looking for.
You can try this code to get the perfect output when using shortcode in content.
<?php
ob_start();
the_content();
$content_output = ob_get_clean();
echo $content_output;
?>
Cleanest way to output the_content using a shortcode is this:
function my_sc_content() {
ob_start();
$my_content = the_content();
echo $my_content;
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
add_shortcode('my-content','my_sc_content');