Im using "insert pages" plugin for wordpress that allows me to insert pages and posts into other pages and posts from the backend. My problem being that when i i use "insert pages" plugin in my news section and then visit the site it shows the shortcode instead of the content. This seems to be a problem only when making a "post". If i use the plugin to display a page within another page it works fine (shows the content not the shortcode).
Any idea what could be the cause of this problem?
Cheers,
Emil
Update
// Shortcode hook: Replace the [insert ...] shortcode with the inserted page's content
function insertPages_handleShortcode_insert( $atts, $content = null ) {
global $wp_query, $post, $wp_current_filter;
extract( shortcode_atts( array(
'page' => '0',
'display' => 'all',
), $atts ) );
// Validation checks.
if ( $page === '0' ) {
return $content;
}
// Trying to embed same page in itself.
if ( $page == $post->ID || $page == $post->post_name ) {
return $content;
}
// Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
$done = false;
foreach ( $wp_current_filter as $filter ) {
if ( 'the_content' == $filter ) {
if ( $done ) {
return $content;
} else {
$done = true;
}
}
}
// Get page object from slug or id
$temp_query = clone $wp_query; // we're starting a new loop within the main loop, so save the main query
$temp_post = $wp_query->get_queried_object(); // see: http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2
// Convert slugs to page IDs to standardize query_posts() lookup below.
if ( ! is_numeric( $page ) ) {
$page_object = get_page_by_path( $page, OBJECT, get_post_types() );
$page = $page_object ? $page_object->ID : $page;
}
if ( is_numeric( $page ) ) {
$args = array(
'p' => intval( $page ),
'post_type' => get_post_types(),
);
} else {
$args = array(
'name' => esc_attr( $page ),
'post_type' => get_post_types(),
);
}
query_posts( $args );
// Start our new Loop
while ( have_posts() ) {
ob_start(); // Start output buffering so we can save the output to string
// Show either the title, link, content, everything, or everything via a custom template
// Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
// This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
// are only getting called once. The fix here is to disable processing of filters on the_content in
// the inserted page. #see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
switch ( $display ) {
case "title":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php break;
case "link":
the_post(); ?>
<?php the_title(); ?>
<?php break;
case "excerpt":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "excerpt-only":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "content":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_content(); else the_content(); ?>
<?php break;
case "all":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_content(); else the_content(); ?>
<?php the_meta(); ?>
<?php break;
default: // display is either invalid, or contains a template file to use
$template = locate_template( $display );
if ( strlen( $template ) > 0 ) {
include $template; // execute the template code
} else { // Couldn't find template, so fall back to printing a link to the page.
the_post(); ?>
<?php the_title(); ?><?php
}
break;
}
$content = ob_get_contents(); // Save off output buffer
ob_end_clean(); // End output buffering
}
wp_reset_postdata();
$wp_query = clone $temp_query; // Restore main Loop's wp_query
$post = $temp_post;
$content = "<div data-post-id='$page' id='insertPages_Content'>$content</div>";
return $content;
//return do_shortcode($content); // careful: watch for infinite loops with nested inserts
}
This is could be work for you.
while ( have_posts() ) {
ob_start(); // Start output buffering so we can save the output to string
// Show either the title, link, content, everything, or everything via a custom template
// Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
// This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
// are only getting called once. The fix here is to disable processing of filters on the_content in
// the inserted page. #see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
switch ( $display ) {
case "title":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php break;
case "link":
the_post(); ?>
<?php the_title(); ?>
<?php break;
case "excerpt":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "excerpt-only":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "content":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo do_shortcode( get_the_content() ); else the_content(); ?>
<?php break;
case "all":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo do_shortcode( get_the_content() ); else the_content(); ?>
<?php the_meta(); ?>
<?php break;
default: // display is either invalid, or contains a template file to use
$template = locate_template( $display );
if ( strlen( $template ) > 0 ) {
include $template; // execute the template code
} else { // Couldn't find template, so fall back to printing a link to the page.
the_post(); ?>
<?php the_title(); ?><?php
}
break;
}
Related
Just wanted to show the title of a Wordpress post. I've tried with no success. Pretty new to this.
You can try this, I hope this will help you.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
and if you want to add custom post type, in that case you need to add WP_Query(); function to get the data like:
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
for displaying post title in specific pages either you can write:
<?php get_the_title($post_id);?>
into that page's template file or you can make a custom file in templates folder and add this code:
<?php
if (is_page( 'Page Title' ) ):
get_the_title($post_id);
endif;
?>
and include this to all of your pages.. you can check for multiple pages by using OR condition in if statement.
I want to exclude a category from my homepage, but my code didn't work:
<?php if( $category_items_query->have_posts() ): ?>
<div class="row columns-layout content-grid">
<?php if( $layout === 'two' ): ?>
<!-- EXCLUDE CATEGORY 17 -->
<?php query_posts($query_string . '&cat=-17'); ?>
<?php while( $category_items_query->have_posts() ): $category_items_query->the_post(); ?>
<div class="col-lg-6 col-md-6 recent-item two-columns post-box">
<?php get_template_part( 'parts/home-content', 'columns' ); ?>
</div><!-- .two-columns -->
<?php endwhile; ?>
<?php endif; ?>
</div><!-- .row -->
<?php endif; ?>
Please help me.
Thanks
Guido
Please do not use query_posts as it is ineffiecnt and re-run sql queries and will also mess up your pagination..
Instead use pre_get_posts e.g.
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-17' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
First you must define global variable like below , then you can try your code.
global $query_string;
query_posts($query_string . '&cat=-17');
Also there is another way that you can try as well :
In function.php :
function exclude_category( $wp_query ) {
$excluded = array( '-1' );
$wp_query->set('category__not_in', $excluded);
set_query_var( 'category__not_in', $excluded );
}
add_action( 'pre_get_posts', 'demo_exclude_category' );
In your code (you can use in your way) :
class Exclude_Cat_Plugin {
public function __construct() {
add_action( 'pre_get_posts', array( $this,'exclude_first_category_posts') );
}
public function exclude_first_category_posts( $wp_query ) {
if ( ! is_search() && ! is_archive() ) {
$excluded = array( '-1' );
$wp_query->set( 'category__not_in', $excluded );
set_query_var( 'category__not_in', $excluded );
}
}
}
I hope its help you.
I'm using this function to split the content
function split_content() {
global $more;
$more = true;
$content = preg_split('/<span id="more-\d+"><\/span>/i', get_the_content('more'));
for($c = 0, $csize = count($content); $c < $csize; $c++) {
$content[$c] = apply_filters('the_content', $content[$c]);
}
return $content;
}
Now I'm using a shortcode to display an icon, the problem is that no mater where i put it in the content, it always is displayed before the split content function, is the're a way to do the shortcode after the split content function?
The shortcode function
add_shortcode( 'ikona', 'add_device_icon' );
function add_device_icon($attr){
global $post;
$icons = array(0=>'wybierz',1=>'piekarnik',2=>'frytkownica',3=>'mikrofalówka',4=>'patelnia',5=>'garnek',6=>'grill',7=>'opiekacz');
$icon_number = get_post_meta( $post->ID, 'product-icon-type', true );
extract( shortcode_atts( array( 'do' => ''), $atts ) );
$style='';
if($do!=''){
$style = 'style="float:left"';
}
echo "<span class='icon-$icon_number' title='{$icons[$icon_number]}' $style></span>";
}
The content is displayed in the wordpress loop
<div class='holder'>
<h3><?php the_title() ?></h3>
<?php $content = split_content() ?>
<?php echo $content[0] ?>
<?php if(count($content )> 1 ) :?>
zobacz więcej…
<?php endif ?>
</div>
<div class="more">
<?php echo $content[1] ?>
</div>
I have a child page with the slug /en/work/ which I want to use a page template found by slug. I've uploaded both page-work.php and page-en-work.php but when I navigate to the page none of them is used.
Have a look at the following link
http://codex.wordpress.org/Function_Reference/get_template_part
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
And on the custom page template ("my_child.php") include the file like so:
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'my_child' ); ?>
<?php endwhile; // end of the loop. ?>
Now you can include the get_template_part on other pages as well like so:
<?php
global $wp_query;
// is Page a parent page
if ( $post->post_parent == 0 ) {
// on a parent page, get child pages
$pages = get_pages( 'hierarchical=0&parent=' . $post->ID );
// loop through child pages
foreach ( $pages as $post ){
setup_postdata( $post );
// get the template name for the child page
$template_name = get_post_meta( $post->ID, '_wp_page_template', true );
$template_name = ( 'default' == $template_name ) ? 'page.php' : $template_name;
// default page template_part content-page.php
$slug = 'page';
// check if the slug exists for the child page
if ( locate_template( 'content-' . basename( $template_name ) , $load, $require_once ) != '' ) {
$slug = pathinfo( $template_name, PATHINFO_FILENAME );
}
// load the content template for the child page
get_template_part( 'content', $slug );
}
}
?>
I can't get this to show posts from my custom post type named "resources." Any help?
<?php
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
function add_custom_post_type_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'resources' ) );
return $query;
}
?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
This part should go into functions.php :
<?php
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
function add_custom_post_type_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'resources' ) );
return $query;
}
?>
while this part is in template file :
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
Explanation :
The 1st part is activating a filter / action on pre_get_posts() . but when you put it inside a theme file or template file , the filter will fire too late or not fire at all.
Your functions.php file in a theme is the first one to be parsed, and therefore it should contain all the functions that are not DIRECTLY related to rendering or visualization of the page .
The loop however, which is a rendering mechanism, should be in the appropriate theme file.